[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
Provenance
- Commit
4965987- Tree
3b96b1d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
fbcd38c
10 files changed+91 added−33 removed
modified:artwork/jinjalogo.svg+2 added-2 removed
15 unmodified lines16 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 lines74 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 <metadata29 unmodified linesmodified:docs/src/filters.txt+1 added-1 removed
52 unmodified lines53 The wrapped function is created internally by the decorator, any positional54 arguments are forwarded to the filter function. The first argument is always55 the value already converted into a string.56 57-.. _designer documentation: designerdoc.txt57+.. _designer documentation: builtins.txtmodified:docs/src/inheritance.txt+15 added
153 unmodified lines154 the same as above155 156 {{ super(2) }}157 return the data of the second parent block158+159+Block Shortcuts160+===============161+162+With Jinja 1.1 onwards it's possible to have a shortcut syntax for blocks163+with few content. The following constructs do the same:164+165+.. sourcecode:: jinja166+167+ {% block title %}{{ page_title }}{% endblock %}168+169+ {% block title page_title %}170+171+Note that as soon as you specify a second argument it's threated as172+short block and Jinja won't look for an closing tag.modified:docs/src/templatei18n.txt+11 added
14 unmodified lines15 16 {{ _("This is a translatable string") }}17 18 The latter one is useful if you want translatable arguments for filters etc.19+If you want to use the ``_()`` syntax in an expression and have variables in20+the string you can add a substituation marker (``%s``) and use the `|format`21+filter to fill the slot:22 23+.. sourcecode:: jinja24+25+ {{ _('Hello %s!')|format(username) }}26+27+If you have more than one substitution variable consider using the28+``{% trans %}`` tags or the `|dformat` filter, the latter however is new29+in Jinja 1.1.30+31 If you want to have plural forms too, use the `pluralize` block:32 33 .. sourcecode:: jinja34 49 unmodified linesmodified:docs/src/tests.txt+1 added-1 removed
24 unmodified lines25 .. sourcecode:: python26 27 env.tests['even'] = is_even28 29-.. _designer documentation: designerdoc.txt29+.. _designer documentation: builtins.txt30 .. _filter documentation: filters.txtmodified:jinja/translators/python.py+2 added-2 removed
483 unmodified lines484 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 lines501 'undefined_singleton'] if item in self.used_shortcuts502 ]),503 outer_filename504 )505- ])505+ )506 507 # the template body508 if requirements:509 lines.append(' for item in bootstrap(context): pass')733 unmodified linesmodified:jinja/datastructure.py+24 added-18 removed
265 unmodified lines266 return result267 268 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__)297 298 def __repr__(self):299 """295 unmodified linesmodified:jinja/filters.py+23 added-1 removed
612 unmodified lines613 {{ "%s - %s"|format("Hello?", "Foo!") }}614 -> Hello? - Foo!615 616 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 wrapped622 623 624+def do_dformat(d):625+ """626+ Apply python mapping string formatting on an object:627+628+ .. sourcecode:: jinja629+630+ {{ "Hello %(username)s!"|dformat({'username': 'John Doe'}) }}631+ -> Hello John Doe!632+633+ This is useful when adding variables to translateable634+ string expressions.635+636+ *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 wrapped643+644+645 def do_trim(value):646 """647 Strip leading and trailing whitespace.648 """220 unmodified lines869 '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 linesmodified:jinja/parser.py+7 added-8 removed
300 unmodified lines301 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)309-305 # 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)310 311+ 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)319 320 def handle_extends_directive(self, lineno, gen):321 """466 unmodified linesmodified:CHANGES+5 added
87 unmodified lines88 89 - Translatable strings returned by ``_()`` will leave their string formatting90 signs untouched. Thanks to Stefan Ebner for reporting.91 92+- ``{% 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.96 97+98 Version 1.099 -----------100 (released Mar 23, 2007)101 1 unmodified line