]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
[svn] added support for comments and some doc changes
authorArmin Ronacher <armin.ronacher@active-4.com>
Tue, 6 Mar 2007 16:35:54 +0000 (17:35 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Tue, 6 Mar 2007 16:35:54 +0000 (17:35 +0100)
--HG--
branch : trunk

TODO [new file with mode: 0644]
docs/src/frameworks.txt [new file with mode: 0644]
docs/src/index.txt
jinja/datastructure.py
jinja/filters.py
jinja/parser.py
jinja/translators/python.py

diff --git a/TODO b/TODO
new file mode 100644 (file)
index 0000000..e77d7c8
--- /dev/null
+++ b/TODO
@@ -0,0 +1,8 @@
+===================
+TODO List for Jinja
+===================
+
+-   Requirements in Jinja (blocks and set directives) outside of renderable
+    blocks should become part of the module not the generate function.
+
+-   Improve the context lookup (maybe with an optional C extension)
diff --git a/docs/src/frameworks.txt b/docs/src/frameworks.txt
new file mode 100644 (file)
index 0000000..7a389b2
--- /dev/null
@@ -0,0 +1,33 @@
+=====================
+Framework Integration
+=====================
+
+Jinja registers itself in the baker template plugin system. If your framework
+supports baker (currently `TurboGears`_ and `pylons`_) you can add it very
+easy.
+
+Pylons
+------
+
+Edit ``yourproject/config/middleware.py`` and add the following lines to
+`config.init_app...`:
+
+.. sourcecode:: python
+
+    from jinja import Environment, FileSystemLoader
+
+    config.add_template_engine('jinja', {
+        'jinja.environment': Environment(loader=FileSystemLoader('path/to/templates'))
+    })
+
+To make Jinja the default template engine change the `init_app` call to
+something like this:
+
+.. sourcecode:: python
+
+    config.init_app(global_conf, app_conf, package='yourproject',
+                    template_engine='jinja')
+
+
+.. _TurboGears: http://www.turbogears.org/
+.. _pylons: http://www.pylonshq.com/
index 250a5a3a018d308ccdf33dac0deaaa7dfa955bda..a826c2b764235232f53b0e9c7a557033abbcce92 100644 (file)
@@ -18,6 +18,8 @@ Welcome in the Jinja documentation.
 
   - `Translators <translators.txt>`_
 
+  - `Framework Integration <frameworks.txt>`_
+
 - Template Designer Documentation:
 
   - `Syntax Reference <designerdoc.txt>`_
index 8d2be5cca152d68c33729f450d0bdf7617fedc9d..73d789d4af932ab26cdb8f5dad453bbc2c53d5a0 100644 (file)
@@ -286,7 +286,7 @@ class TokenStream(object):
         return True
 
     eos = property(lambda x: not x.__nonzero__(), doc=__nonzero__.__doc__)
-    
+
     def next(self):
         """Return the next token from the stream."""
         if self._pushed:
@@ -316,6 +316,12 @@ class TokenStream(object):
         except StopIteration:
             raise IndexError('end of stream reached')
 
+    def drop_until(self, test, drop_needle=False):
+        """Fetch tokens until a function matches and drop all
+        tokens."""
+        for token in self.fetch_until(test, drop_needle):
+            pass
+
     def push(self, lineno, token, data):
         """Push an yielded token back to the stream."""
         self._pushed.append((lineno, token, data))
index 6a0bf33691ed20435bfdf46d7c45c939478ed3df..8d758b2512d8c51c59733dd8d3d3124b47afea4f 100644 (file)
@@ -169,8 +169,7 @@ def do_join(d=u''):
             -> 123
     """
     def wrapped(env, context, value):
-        d = env.to_unicode(d)
-        return d.join([env.to_unicode(x) for x in value])
+        return env.to_unicode(d).join([env.to_unicode(x) for x in value])
     return wrapped
 
 
@@ -183,8 +182,8 @@ def do_count():
     def wrapped(env, context, value):
         try:
             if type(value) in (int, float, long):
-                return len(str(var))
-            return len(var)
+                return len(str(value))
+            return len(value)
         except TypeError:
             return 0
     return wrapped
@@ -307,6 +306,7 @@ FILTERS = {
     'default':              do_default,
     'join':                 do_join,
     'count':                do_count,
+    'length':               do_count,
     'reverse':              do_reverse,
     'center':               do_center,
     'title':                do_title,
index 33c8aed9df1e1a343dd56009582daca63bcf29f3..a257985f27606d7868617ef99a0c475a508ae356 100644 (file)
@@ -19,6 +19,7 @@ from jinja.exceptions import TemplateSyntaxError
 # callback functions for the subparse method
 end_of_block = lambda p, t, d: t == 'block_end'
 end_of_variable = lambda p, t, d: t == 'variable_end'
+end_of_comment = lambda p, t, d: t == 'comment_end'
 switch_for = lambda p, t, d: t == 'name' and d in ('else', 'endfor')
 end_of_for = lambda p, t, d: t == 'name' and d == 'endfor'
 switch_if = lambda p, t, d: t == 'name' and d in ('else', 'elif', 'endif')
@@ -299,8 +300,11 @@ class Parser(object):
 
             while True:
                 lineno, token, data = self.tokenstream.next()
+                # comments
+                if token == 'comment_begin':
+                    self.tokenstream.drop_until(end_of_comment, True)
                 # nested variables
-                if token == 'variable_begin':
+                elif token == 'variable_begin':
                     _, variable_token, variable_name = self.tokenstream.next()
                     if variable_token != 'name' or variable_name not in replacements:
                         raise TemplateSyntaxError('unregistered translation '
@@ -410,9 +414,13 @@ class Parser(object):
         lineno = self.tokenstream.last[0]
         result = nodes.NodeList(lineno)
         for lineno, token, data in self.tokenstream:
+            # comments
+            if token == 'comment_begin':
+                self.tokenstream.drop_until(end_of_comment, True)
+
             # this token marks the begin or a variable section.
             # parse everything till the end of it.
-            if token == 'variable_begin':
+            elif token == 'variable_begin':
                 gen = self.tokenstream.fetch_until(end_of_variable, True)
                 result.append(self.directives['print'](lineno, gen))
 
index a4238209192f804ea381b6aada8b9ec264d24b3b..3128822b78580460d501e45298d6da745457c889 100644 (file)
@@ -415,10 +415,12 @@ class PythonTranslator(Translator):
 
         write('def macro(*args):')
         self.indention += 1
+        write('context.push()')
         write('%s = (args + %s[len(args):])' % (_to_tuple(args), _to_tuple(defaults)))
         write('macrobuffer = []')
         write('write = macrobuffer.append')
         buf.append(self.handle_node(node.body))
+        write('context.pop()')
         write('return u\'\'.join(macrobuffer)')
         self.indention -= 1
         buf.append(self.indent('context[%r] = macro' % node.name))