Skip to content

Make possible to pull out template's section from parsed template #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pystache/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def read(path):
f.close()


class _BaseNode(object):
pass


class MissingTags(object):

"""Contains the valid values for Renderer.missing_tags."""
Expand Down
34 changes: 34 additions & 0 deletions pystache/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"""

from pystache.common import _BaseNode


class ParsedTemplate(object):

Expand Down Expand Up @@ -48,3 +50,35 @@ def get_unicode(node):
s = ''.join(parts)

return unicode(s)

def get_node(self, key):
for node in self._parse_tree:
if not isinstance(node, _BaseNode):
continue

_found_key = getattr(node, 'key', None)

if _found_key == key:
# Node with given key is found
return node

parsed = getattr(node, 'parsed', getattr(node, 'parsed_section', None))

if parsed is None:
continue

found_in_nested = parsed.get_node(key)
if found_in_nested is None:
continue
else:
return found_in_nested

return None


def _as_template(self):
template = ParsedTemplate()
template.add(self)
return template

_BaseNode.as_template = _as_template
15 changes: 8 additions & 7 deletions pystache/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pystache import defaults
from pystache.parsed import ParsedTemplate
from pystache.common import _BaseNode


END_OF_LINE_CHARACTERS = [u'\r', u'\n']
Expand Down Expand Up @@ -88,7 +89,7 @@ def _format(obj, exclude=None):
return "%s(%s)" % (obj.__class__.__name__, ", ".join(args))


class _CommentNode(object):
class _CommentNode(_BaseNode):

def __repr__(self):
return _format(self)
Expand All @@ -97,7 +98,7 @@ def render(self, engine, context):
return u''


class _ChangeNode(object):
class _ChangeNode(_BaseNode):

def __init__(self, delimiters):
self.delimiters = delimiters
Expand All @@ -109,7 +110,7 @@ def render(self, engine, context):
return u''


class _EscapeNode(object):
class _EscapeNode(_BaseNode):

def __init__(self, key):
self.key = key
Expand All @@ -122,7 +123,7 @@ def render(self, engine, context):
return engine.escape(s)


class _LiteralNode(object):
class _LiteralNode(_BaseNode):

def __init__(self, key):
self.key = key
Expand All @@ -135,7 +136,7 @@ def render(self, engine, context):
return engine.literal(s)


class _PartialNode(object):
class _PartialNode(_BaseNode):

def __init__(self, key, indent):
self.key = key
Expand All @@ -152,7 +153,7 @@ def render(self, engine, context):
return engine.render(template, context)


class _InvertedNode(object):
class _InvertedNode(_BaseNode):

def __init__(self, key, parsed_section):
self.key = key
Expand All @@ -172,7 +173,7 @@ def render(self, engine, context):
return self.parsed_section.render(engine, context)


class _SectionNode(object):
class _SectionNode(_BaseNode):

# TODO: the template_ and parsed_template_ arguments don't both seem
# to be necessary. Can we remove one of them? For example, if
Expand Down
5 changes: 5 additions & 0 deletions pystache/tests/examples/node_as_template.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#foo}}
<div>
{{#bar}}Hello {{name}}!{{/bar}}
</div>
{{/foo}}
11 changes: 10 additions & 1 deletion pystache/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from examples.unicode_output import UnicodeOutput
from examples.unicode_input import UnicodeInput
from examples.nested_context import NestedContext
from pystache import Renderer
from pystache import Renderer, parse
from pystache.tests.common import EXAMPLES_DIR
from pystache.tests.common import AssertStringMixin

Expand Down Expand Up @@ -102,5 +102,14 @@ def test_partial_in_partial_has_access_to_grand_parent_context(self):
actual = renderer.render(view, {'prop': 'derp'})
self.assertEqual(actual, 'Hi derp!')

def test_node_as_template(self):
renderer = Renderer(search_dirs=EXAMPLES_DIR)

full_template = parse(renderer.load_template('node_as_template'))
node_template = full_template.get_node('bar').as_template()

actual = renderer.render(node_template, {'bar': {'name': 'Chris'}})
self.assertEqual(actual, 'Hello Chris!')

if __name__ == '__main__':
unittest.main()