Skip to content
Myr

[svn] added block shortcut syntax for jinja

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 fbcd38c · Browse files · 10 files+91 added−33 removed
This commit

State

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

Diff stats

10 files+91 added−33 removed

  • other:+91 added−33 removed

10 files+91 added−33 removed

modified:artwork/jinjalogo.svg+2 added-2 removed
15 unmodified lines
16 inkscape:version="0.44"17 version="1.0"18 sodipodi:docbase="/home/blackbird/Development/jinja/trunk/artwork"19 sodipodi:docname="jinjalogo.svg"20 inkscape:export-filename="/home/blackbird/Development/jinja/trunk/docs/jinjalogo.png"20 inkscape:export-filename="/home/blackbird/Development/jinja/www/static/jinjabanner.png"21 inkscape:export-xdpi="60"22 inkscape:export-ydpi="60">23 <defs24 id="defs4">
49 unmodified lines
74 width="300px"75 height="120px"76 showguides="true"77 inkscape:guide-bbox="true"78 inkscape:window-width="1400"78 inkscape:window-width="1396"79 inkscape:window-height="975"80 inkscape:window-x="0"81 inkscape:window-y="24" />82 <metadata
29 unmodified lines
modified:docs/src/filters.txt+1 added-1 removed
52 unmodified lines
53The wrapped function is created internally by the decorator, any positional54arguments are forwarded to the filter function. The first argument is always55the value already converted into a string.5657.. _designer documentation: designerdoc.txt57.. _designer documentation: builtins.txt
modified:docs/src/inheritance.txt+15 added
153 unmodified lines
154 the same as above155156 {{ super(2) }}157 return the data of the second parent block158159Block Shortcuts160===============161162With Jinja 1.1 onwards it's possible to have a shortcut syntax for blocks163with few content. The following constructs do the same:164165.. sourcecode:: jinja166167 {% block title %}{{ page_title }}{% endblock %}168169 {% block title page_title %}170171Note that as soon as you specify a second argument it's threated as172short block and Jinja won't look for an closing tag.
modified:docs/src/templatei18n.txt+11 added
14 unmodified lines
1516 {{ _("This is a translatable string") }}1718The latter one is useful if you want translatable arguments for filters etc.19If you want to use the ``_()`` syntax in an expression and have variables in20the string you can add a substituation marker (``%s``) and use the `|format`21filter to fill the slot:2223.. sourcecode:: jinja2425 {{ _('Hello %s!')|format(username) }}2627If you have more than one substitution variable consider using the28``{% trans %}`` tags or the `|dformat` filter, the latter however is new29in Jinja 1.1.3031If you want to have plural forms too, use the `pluralize` block:3233.. sourcecode:: jinja34
49 unmodified lines
modified:docs/src/tests.txt+1 added-1 removed
24 unmodified lines
25.. sourcecode:: python2627 env.tests['even'] = is_even2829.. _designer documentation: designerdoc.txt29.. _designer documentation: builtins.txt30.. _filter documentation: filters.txt
modified:jinja/translators/python.py+2 added-2 removed
483 unmodified lines
484 lines.append('from jinja.exceptions import TemplateRuntimeError')485 if self.used_data_structures:486 lines.append('from jinja.datastructure import %s' % ', '.487 join(self.used_data_structures))488 lines.extend([488 lines.append(489 '\n# Aliases for some speedup\n'490 '%s\n\n'491 '# Name for disabled debugging\n'492 '__name__ = %r\n\n'
8 unmodified lines
501 'undefined_singleton'] if item in self.used_shortcuts502 ]),503 outer_filename504 )505 ])505 )506507 # the template body508 if requirements:509 lines.append(' for item in bootstrap(context): pass')
733 unmodified lines
modified:jinja/datastructure.py+24 added-18 removed
265 unmodified lines
266 return result267268 def translate_func(self):269 """270 Return a translation function for this context. It takes270 The translation function for this context. It takes271 4 parameters. The singular string, the optional plural one,272 the indicator number which is used to select the correct273 plural form and a dict with values which should be inserted.272 The name of the variable in the replacements dict and the273 replacements dict. This is only used by the i18n system274 internally the simplified version (just one argument) is275 available in the template for the user too.276 """277 if self._translate_func is not None:278 return self._translate_func279 translator = self.environment.get_translator(self)275 if self._translate_func is None:276 translator = self.environment.get_translator(self)277 def translate(s, p=None, n=None, r=None):278 if p is None:279 s = translator.gettext(s)280 else:281 s = translator.ngettext(s, p, r[n])282 # apply replacement substitution only if replacements283 # are given. This is the case for {% trans %}...{% endtras %}284 # but for the "_()" syntax and a trans tag without a body.285 if r is not None:286 s %= r287 return s288 self._translate_func = translate289 return self._translate_func280 gettext = translator.gettext281 ngettext = translator.ngettext282 def translate(s, p=None, n=None, r=None):283 if p is None:284 s = gettext(s)285 else:286 s = ngettext(s, p, r[n])287 # apply replacement substitution only if replacements288 # are given. This is the case for {% trans %}...{% endtrans %}289 # but for the "_()" syntax and a trans tag without a body.290 if r is not None:291 return s % r292 return s293 translate.__doc__ = Context.translate_func.__doc__294 self._translate_func = translate295 return translate296 translate_func = property(translate_func, doc=translate_func.__doc__)297298 def __repr__(self):299 """
295 unmodified lines
modified:jinja/filters.py+23 added-1 removed
612 unmodified lines
613 {{ "%s - %s"|format("Hello?", "Foo!") }}614 -> Hello? - Foo!615616 Note that you cannot use the mapping syntax (``%(name)s``)617 like in python.617 like in python. Use `|dformat` for that.618 """619 def wrapped(env, context, value):620 return env.to_unicode(value) % args621 return wrapped622623624def do_dformat(d):625 """626 Apply python mapping string formatting on an object:627628 .. sourcecode:: jinja629630 {{ "Hello %(username)s!"|dformat({'username': 'John Doe'}) }}631 -> Hello John Doe!632633 This is useful when adding variables to translateable634 string expressions.635636 *New in Jinja 1.1*637 """638 if not isinstance(d, dict):639 raise FilterArgumentError('dict required')640 def wrapped(env, context, value):641 return env.to_unicode(value) % d642 return wrapped643644645def do_trim(value):646 """647 Strip leading and trailing whitespace.648 """
220 unmodified lines
869 'float': do_float,870 'string': do_string,871 'urlize': do_urlize,872 'format': do_format,873 'dformat': do_dformat,874 'capture': do_capture,875 'trim': do_trim,876 'striptags': do_striptags,877 'slice': do_slice,
5 unmodified lines
modified:jinja/parser.py+7 added-8 removed
300 unmodified lines
301 raise TemplateSyntaxError('illegal use of keyword %r '302 'as block name.' % block_name[2],303 lineno, self.filename)304 name = block_name[2][:-1]305 if tokens:306 raise TemplateSyntaxError('block got too many arguments, '307 'requires one.', lineno,308 self.filename)309305 # check if this block does not exist by now.306 if name in self.blocks:307 raise TemplateSyntaxError('block %r defined twice' %308 name, lineno, self.filename)309 self.blocks.add(name)310311 if tokens:312 body = nodes.NodeList(lineno, [nodes.Print(lineno,313 self.parse_python(lineno, tokens, '(%s)').expr)])314 else:316 # now parse the body and attach it to the block317 body = self.subparse(end_of_block_tag, True)318 self.close_remaining_block()315 # otherwise parse the body and attach it to the block316 body = self.subparse(end_of_block_tag, True)317 self.close_remaining_block()318 return nodes.Block(lineno, name, body)319320 def handle_extends_directive(self, lineno, gen):321 """
466 unmodified lines
modified:CHANGES+5 added
87 unmodified lines
8889- Translatable strings returned by ``_()`` will leave their string formatting90 signs untouched. Thanks to Stefan Ebner for reporting.9192- ``{% block name "data" %}`` is now an alias for93 ``{% block name %}data{% endblock %}``. Note that the second argument can94 be an expression. As soon as you specify an expression as second argument95 the closing tag has to be omitted.969798Version 1.099-----------100(released Mar 23, 2007)101
1 unmodified line