Skip to content

Commit

Permalink
source: Implement dynamic include name support.
Browse files Browse the repository at this point in the history
I.e. now include name can be a variable:

{% include {{inc_name}} %}

Such include of course can't be inlined, so instead will be imported
dynamically.
  • Loading branch information
pfalcon committed Jan 9, 2018
1 parent 86836b6 commit 24c5ceb
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions utemplate/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,23 @@ def parse_statement(self, stmt):
# If there was no other output, we still need a header now
self.indent()
tokens = tokens[1].split(None, 1)
args = ""
if len(tokens) > 1:
args = tokens[1]
if tokens[0][0] == "{":
self.indent()
# "1" as fromlist param is uPy hack
self.file_out.write('_ = __import__(%s.replace(".", "_"), None, None, 1)\n' % tokens[0][2:-2])
self.indent()
self.file_out.write("yield from _.render(%s)\n" % args)
return

with self.loader.input_open(tokens[0][1:-1]) as inc:
self.seq += 1
c = Compiler(inc, self.file_out, len(self.stack) + self._indent, self.seq)
inc_id = self.seq
self.seq = c.compile()
self.indent()
args = ""
if len(tokens) > 1:
args = tokens[1]
self.file_out.write("yield from render%d(%s)\n" % (inc_id, args))
elif len(tokens) > 1:
if tokens[0] == "elif":
Expand Down

0 comments on commit 24c5ceb

Please sign in to comment.