Skip to content
Myr

[svn] implemented {% raw %} and improved jinja/django highlighter

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 2894f22 · Browse files · 6 files+68 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+68 added−21 removed

  • other:+68 added−21 removed

6 files+68 added−21 removed

modified:docs/src/designerdoc.txt+48 added-4 removed
117 unmodified lines
118These tests are especially useful when used in `if` conditions.119120[[list_of_tests]]121122Global Functions123================124125Test functions and filter functions live in their own namespace. Global126functions not. They behave like normal objects in the context. Beside the127functions added by the application or framewhere there are two functions128available per default:129130`range`131 132 Works like the python `range function`_ just that it doesn't support133 ranges greater than ``1000000``.134135`debug`136137 Function that outputs the contents of the context.138139Loops140=====141142To iterate over a sequence, you can use the `for` loop. It basically looks like a
9 unmodified lines
152 <li><em>no users found</em></li>153 {% endfor %}154 </ul>155139*Important difference from Python:* the optional ``else`` block is only156*Important* Contrary to Python is the optional ``else`` block only157executed if there was no iteration because the sequence was empty.158159Inside of a `for` loop block you can access some special variables:160
435 unmodified lines
596597 {{ undefined.attribute().attribute_too[42] }}598 still returns `undefined`.599600Escaping601========602603Sometimes you might want to add Jinja syntax elements into the template604without executing them. In that case you have quite a few possibilities.605606For small parts this might be a good way:607608.. sourcecode:: jinja609610 {{ "{{ foo }} is variable syntax and {% foo %} is block syntax" }}611612When you have multiple elements you can use the ``raw`` block:613614.. sourcecode:: jinja615616 {% raw %}617 Filtering blocks works like this in Jinja:618 {% filter escape %}619 <html>620 <code>goes here</code>621 </html>622 {% endfilter %}623 {% endraw %}624625Internationalization626====================627628If the application is configured for i18n, you can define translatable blocks
31 unmodified lines
660number that is used to determine the correct singular or plural form. If you661don't have the indicator variable on position 1 you have to tell the `pluralize`662tag the correct variable name.663622Inside translatable blocks you cannot use blocks or expressions. The variable623print syntax (``{{ variablename }}``) can just be used to insert the variables defined624in the ``trans`` header.664Inside translatable blocks you cannot use blocks or expressions (however you can665still use the ``raw`` block which will work as expected). The variable666print syntax (``{{ variablename }}``) is the only way to insert the variables667defined in the ``trans`` header. Filters must be applied in the header.668669.. admonition:: note670671 Please make sure that you always use pluralize blocks where required.
9 unmodified lines
681 one user found.682 {% endif %}683684.. _slicing chapter: http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice685.. _range function: http://docs.python.org/tut/node6.html#SECTION006300000000000000000
modified:jinja/translators/python.py+2 added
171 unmodified lines
172 """173 Return a comment that helds the node informations or None174 if there is no need to add a debug comment.175 """176 if node.filename is None:177 return178 rv = '# DEBUG(filename=%s, lineno=%s)' % (179 node.filename,180 node.lineno181 )
714 unmodified lines
modified:jinja/lexer.py+5 added-1 removed
88 unmodified lines
8990 # global parsing rules91 self.rules = {92 'root': [93 (c('(%s\s*raw\s%s)(.*?)(%s\s*endraw\s*%s)' % (94 (e(environment.block_start_string),95 e(environment.block_end_string)) * 2)),96 (None, 'data', None), None),97 (c('(.*?)(?:%s)' % '|'.join([98 '(?P<%s_begin>%s)' % (n, e(r)) for n, r in root_tag_rules99 ])), ('data', '#bygroup'), '#bygroup'),100 (c('.+'), 'data', None)
52 unmodified lines
153 if isinstance(tokens, tuple):154 for idx, token in enumerate(tokens):155 # hidden group156 if token is None:153 g += m.group(idx)157 g = m.group(idx)158 if g:159 lineno += g.count('\n')160 continue161 # failure group
72 unmodified lines
modified:jinja/parser.py+1 added-1 removed
478 unmodified lines
479480 # here the only token we should get is "data". all other481 # tokens just exist in block or variable sections. (if the482 # tokenizer is not brocken)483 elif token == 'data':483 elif token in 'data':484 result.append(nodes.Text(lineno, data))485486 # so this should be unreachable code487 else:
21 unmodified lines
modified:jinja/utils.py+6 added-14 removed
201 unmodified lines
202 m = _debug_info_re.search(sourcelines[startpos])203 if m is not None:204 filename, lineno = m.groups()205 if filename == 'None':206 filename = ''207 if lineno == 'None':208 lineno = 0209 else:206 filename = None207 if lineno != 'None':208 lineno = int(lineno)209 break210 startpos -= 1211212 # no traceback information found, reraise unchanged215 if filename is None:213 if not filename:214 return traceback215216 return raise_template_exception(exc_value, filename,217 lineno, context)218219
95 unmodified lines
315 else:316 self._queue = []317 pop = self._queue.pop318 self._popleft = lambda: pop(0)319320 # alias all queue methods for faster lookup321 self._pop = self._queue.pop322323 # XXX: Is this good? Didn't find another sollution324 def remove_by_value(value):325 for i, v in enumerate(self._queue):326 if v == value:327 self._queue.__delitem__(i)328 break329330 self._remove = remove_by_value322 self._remove = self._queue.remove323 self._append = self._queue.append324325 def copy(self):326 rv = CacheDict(self.capacity)
63 unmodified lines
modified:jdebug.py+6 added-1 removed
8 unmodified lines
9 :license: BSD, see LICENSE for more details.10"""11from jinja import Environment12from jinja.parser import Parser13from jinja.lexer import Lexer14from jinja.translators.python import PythonTranslator151616__all__ = ['e', 't', 'p']17__all__ = ['e', 't', 'p', 'l']1819e = Environment()20t = e.from_string2122def p(x):23 print PythonTranslator(e, Parser(e, x).parse()).translate()2425def l(x):26 for item in e.lexer.tokenize(x):27 print '%5s %-20s %r' % item