- 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
-----------
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">
<defs
height="120px"
showguides="true"
inkscape:guide-bbox="true"
- inkscape:window-width="1400"
+ inkscape:window-width="1396"
inkscape:window-height="975"
inkscape:window-x="0"
inkscape:window-y="24" />
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
{{ 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.
{{ _("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:
env.tests['even'] = is_even
-.. _designer documentation: designerdoc.txt
+.. _designer documentation: builtins.txt
.. _filter documentation: filters.txt
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):
-> 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.
'string': do_string,
'urlize': do_urlize,
'format': do_format,
+ 'dformat': do_dformat,
'capture': do_capture,
'trim': do_trim,
'striptags': do_striptags,
'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):
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'
]),
outer_filename
)
- ])
+ )
# the template body
if requirements: