# HG changeset patch # User cmlenz # Date 1205752196 0 # Node ID 35e14338870583ec9dd8e10ebe2a06dba27e5ecd # Parent e162edb680cf26e897883654dd01c1c10f49cf33 Enable use of expressions in include directives of text templates. Closes #194. Thanks to Oliver Cope for reporting the issue. diff --git a/doc/text-templates.txt b/doc/text-templates.txt --- a/doc/text-templates.txt +++ b/doc/text-templates.txt @@ -215,7 +215,7 @@ .. code-block:: genshitext - {% include '%s.txt' % filename %} + {% include ${'%s.txt' % filename} %} Note that a ``TemplateNotFound`` exception is raised if an included file can't be found. @@ -349,10 +349,12 @@ comments are lines that start with two ``#`` characters, and a line-break at the end of a directive is removed automatically. -.. note:: If you're using this old syntax, it is strongly recommended to migrate - to the new syntax. Simply replace any references to ``TextTemplate`` - by ``NewTextTemplate``. On the other hand, if you want to stick with - the old syntax for a while longer, replace references to +.. note:: If you're using this old syntax, it is strongly recommended to + migrate to the new syntax. Simply replace any references to + ``TextTemplate`` by ``NewTextTemplate`` (and also change the + text templates, of course). On the other hand, if you want to stick + with the old syntax for a while longer, replace references to ``TextTemplate`` by ``OldTextTemplate``; while ``TextTemplate`` is still an alias for the old language at this point, that will change - in a future release. + in a future release. But also note that the old syntax may be + dropped entirely in a future release. diff --git a/genshi/template/tests/text.py b/genshi/template/tests/text.py --- a/genshi/template/tests/text.py +++ b/genshi/template/tests/text.py @@ -232,6 +232,27 @@ Included ----- Included data above this line -----""", tmpl.generate().render()) + def test_include_expr(self): + file1 = open(os.path.join(self.dirname, 'tmpl1.txt'), 'w') + try: + file1.write("Included") + finally: + file1.close() + + file2 = open(os.path.join(self.dirname, 'tmpl2.txt'), 'w') + try: + file2.write("""----- Included data below this line ----- + {% include ${'%s.txt' % ('tmpl1',)} %} + ----- Included data above this line -----""") + finally: + file2.close() + + loader = TemplateLoader([self.dirname]) + tmpl = loader.load('tmpl2.txt', cls=NewTextTemplate) + self.assertEqual("""----- Included data below this line ----- + Included + ----- Included data above this line -----""", tmpl.generate().render()) + def suite(): suite = unittest.TestSuite() diff --git a/genshi/template/text.py b/genshi/template/text.py --- a/genshi/template/text.py +++ b/genshi/template/text.py @@ -28,6 +28,7 @@ import re +from genshi.core import TEXT from genshi.template.base import BadDirectiveError, Template, \ TemplateSyntaxError, EXEC, INCLUDE, SUB from genshi.template.eval import Suite @@ -188,7 +189,11 @@ if command == 'include': pos = (self.filename, lineno, 0) - stream.append((INCLUDE, (value.strip(), None, []), pos)) + value = list(interpolate(value, self.basedir, self.filename, + lineno, 0, lookup=self.lookup)) + if len(value) == 1 and value[0][0] is TEXT: + value = value[0][1] + stream.append((INCLUDE, (value, None, []), pos)) elif command == 'python': if not self.allow_exec: