From: Armin Ronacher Date: Sat, 12 May 2007 21:29:33 +0000 (+0200) Subject: [svn] added block shortcut syntax for jinja X-Git-Tag: 2.0rc1~329 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=49659872ff55d848e16844c538c0ec351e5fa8e1;p=jinja2.git [svn] added block shortcut syntax for jinja --HG-- branch : trunk --- diff --git a/CHANGES b/CHANGES index 0a7a011..aa8d6c8 100644 --- a/CHANGES +++ b/CHANGES @@ -89,6 +89,11 @@ Version 1.1 - Translatable strings returned by ``_()`` will leave their string formatting signs untouched. Thanks to Stefan Ebner for reporting. +- ``{% block name "data" %}`` is now an alias for + ``{% block name %}data{% endblock %}``. Note that the second argument can + be an expression. As soon as you specify an expression as second argument + the closing tag has to be omitted. + Version 1.0 ----------- diff --git a/artwork/jinjalogo.svg b/artwork/jinjalogo.svg index 3eb485f..b1b4b92 100644 --- a/artwork/jinjalogo.svg +++ b/artwork/jinjalogo.svg @@ -17,7 +17,7 @@ version="1.0" sodipodi:docbase="/home/blackbird/Development/jinja/trunk/artwork" sodipodi:docname="jinjalogo.svg" - inkscape:export-filename="/home/blackbird/Development/jinja/trunk/docs/jinjalogo.png" + inkscape:export-filename="/home/blackbird/Development/jinja/www/static/jinjabanner.png" inkscape:export-xdpi="60" inkscape:export-ydpi="60"> diff --git a/docs/src/filters.txt b/docs/src/filters.txt index fd07e28..b8881e0 100644 --- a/docs/src/filters.txt +++ b/docs/src/filters.txt @@ -54,4 +54,4 @@ The wrapped function is created internally by the decorator, any positional arguments are forwarded to the filter function. The first argument is always the value already converted into a string. -.. _designer documentation: designerdoc.txt +.. _designer documentation: builtins.txt diff --git a/docs/src/inheritance.txt b/docs/src/inheritance.txt index 94bae87..6e7714e 100644 --- a/docs/src/inheritance.txt +++ b/docs/src/inheritance.txt @@ -155,3 +155,18 @@ to get the data of the parent you can give it an offset: {{ super(2) }} return the data of the second parent block + +Block Shortcuts +=============== + +With Jinja 1.1 onwards it's possible to have a shortcut syntax for blocks +with few content. The following constructs do the same: + +.. sourcecode:: jinja + + {% block title %}{{ page_title }}{% endblock %} + + {% block title page_title %} + +Note that as soon as you specify a second argument it's threated as +short block and Jinja won't look for an closing tag. diff --git a/docs/src/templatei18n.txt b/docs/src/templatei18n.txt index 50ed017..debd82b 100644 --- a/docs/src/templatei18n.txt +++ b/docs/src/templatei18n.txt @@ -16,6 +16,17 @@ for translators using the `trans` tag or the special underscore function: {{ _("This is a translatable string") }} The latter one is useful if you want translatable arguments for filters etc. +If you want to use the ``_()`` syntax in an expression and have variables in +the string you can add a substituation marker (``%s``) and use the `|format` +filter to fill the slot: + +.. sourcecode:: jinja + + {{ _('Hello %s!')|format(username) }} + +If you have more than one substitution variable consider using the +``{% trans %}`` tags or the `|dformat` filter, the latter however is new +in Jinja 1.1. If you want to have plural forms too, use the `pluralize` block: diff --git a/docs/src/tests.txt b/docs/src/tests.txt index e2231d6..21ce64f 100644 --- a/docs/src/tests.txt +++ b/docs/src/tests.txt @@ -26,5 +26,5 @@ Now you have to register that test on an environment: env.tests['even'] = is_even -.. _designer documentation: designerdoc.txt +.. _designer documentation: builtins.txt .. _filter documentation: filters.txt diff --git a/jinja/datastructure.py b/jinja/datastructure.py index 5aa6b2c..3b8e448 100644 --- a/jinja/datastructure.py +++ b/jinja/datastructure.py @@ -267,26 +267,32 @@ class Context(BaseContext): def translate_func(self): """ - Return a translation function for this context. It takes + The translation function for this context. It takes 4 parameters. The singular string, the optional plural one, - the indicator number which is used to select the correct - plural form and a dict with values which should be inserted. + The name of the variable in the replacements dict and the + replacements dict. This is only used by the i18n system + internally the simplified version (just one argument) is + available in the template for the user too. """ - if self._translate_func is None: - translator = self.environment.get_translator(self) - def translate(s, p=None, n=None, r=None): - if p is None: - s = translator.gettext(s) - else: - s = translator.ngettext(s, p, r[n]) - # apply replacement substitution only if replacements - # are given. This is the case for {% trans %}...{% endtras %} - # but for the "_()" syntax and a trans tag without a body. - if r is not None: - s %= r - return s - self._translate_func = translate - return self._translate_func + if self._translate_func is not None: + return self._translate_func + translator = self.environment.get_translator(self) + gettext = translator.gettext + ngettext = translator.ngettext + def translate(s, p=None, n=None, r=None): + if p is None: + s = gettext(s) + else: + s = ngettext(s, p, r[n]) + # apply replacement substitution only if replacements + # are given. This is the case for {% trans %}...{% endtrans %} + # but for the "_()" syntax and a trans tag without a body. + if r is not None: + return s % r + return s + translate.__doc__ = Context.translate_func.__doc__ + self._translate_func = translate + return translate translate_func = property(translate_func, doc=translate_func.__doc__) def __repr__(self): diff --git a/jinja/filters.py b/jinja/filters.py index fd5c1d4..17c0ce8 100644 --- a/jinja/filters.py +++ b/jinja/filters.py @@ -614,13 +614,34 @@ def do_format(*args): -> Hello? - Foo! Note that you cannot use the mapping syntax (``%(name)s``) - like in python. + like in python. Use `|dformat` for that. """ def wrapped(env, context, value): return env.to_unicode(value) % args return wrapped +def do_dformat(d): + """ + Apply python mapping string formatting on an object: + + .. sourcecode:: jinja + + {{ "Hello %(username)s!"|dformat({'username': 'John Doe'}) }} + -> Hello John Doe! + + This is useful when adding variables to translateable + string expressions. + + *New in Jinja 1.1* + """ + if not isinstance(d, dict): + raise FilterArgumentError('dict required') + def wrapped(env, context, value): + return env.to_unicode(value) % d + return wrapped + + def do_trim(value): """ Strip leading and trailing whitespace. @@ -849,6 +870,7 @@ FILTERS = { 'string': do_string, 'urlize': do_urlize, 'format': do_format, + 'dformat': do_dformat, 'capture': do_capture, 'trim': do_trim, 'striptags': do_striptags, diff --git a/jinja/parser.py b/jinja/parser.py index 207dd54..5865e3e 100644 --- a/jinja/parser.py +++ b/jinja/parser.py @@ -302,20 +302,19 @@ class Parser(object): 'as block name.' % block_name[2], lineno, self.filename) name = block_name[2][:-1] - if tokens: - raise TemplateSyntaxError('block got too many arguments, ' - 'requires one.', lineno, - self.filename) - # check if this block does not exist by now. if name in self.blocks: raise TemplateSyntaxError('block %r defined twice' % name, lineno, self.filename) self.blocks.add(name) - # now parse the body and attach it to the block - body = self.subparse(end_of_block_tag, True) - self.close_remaining_block() + if tokens: + body = nodes.NodeList(lineno, [nodes.Print(lineno, + self.parse_python(lineno, tokens, '(%s)').expr)]) + else: + # otherwise parse the body and attach it to the block + body = self.subparse(end_of_block_tag, True) + self.close_remaining_block() return nodes.Block(lineno, name, body) def handle_extends_directive(self, lineno, gen): diff --git a/jinja/translators/python.py b/jinja/translators/python.py index 0e5cad8..7b85d78 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -485,7 +485,7 @@ class PythonTranslator(Translator): if self.used_data_structures: lines.append('from jinja.datastructure import %s' % ', '. join(self.used_data_structures)) - lines.extend([ + lines.append( '\n# Aliases for some speedup\n' '%s\n\n' '# Name for disabled debugging\n' @@ -502,7 +502,7 @@ class PythonTranslator(Translator): ]), outer_filename ) - ]) + ) # the template body if requirements: