Mercurial > genshi > genshi-test
changeset 693:fa8a55fe2d57
Enable use of expressions in include directives of text templates. Closes #194. Thanks to Oliver Cope for reporting the issue.
author | cmlenz |
---|---|
date | Mon, 17 Mar 2008 11:09:56 +0000 |
parents | af86a8e828d2 |
children | 812671b40022 |
files | doc/text-templates.txt genshi/template/tests/text.py genshi/template/text.py |
diffstat | 3 files changed, 35 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- 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.
--- 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()
--- 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: