Skip to content
Myr

[svn] again some jinja updates

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 252f321 · Browse files · 6 files+155 added−34 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+155 added−34 removed

  • tests:+6 added−0 removed
  • other:+149 added−34 removed

6 files+155 added−34 removed

modified:docs/src/frameworks.txt+2 added-28 removed
51 unmodified lines
5253TurboGears54----------5556Integrating Jinja into TurboGears works pretty much the same.56The TurboGears template plugin interface is broken by design and57there is currently no way to embed Jinja templates in TurboGears.5858Basically all you have to do is to edit the application configuration file at59``application/config/app.cfg`` and change `global.tg.defaultview` and60provide the jinja options:5962.. sourcecode:: ini6364 [global]65 tg.defaultview = 'jinja'66 jinja.init_callback = yourapplication.yourmodule.setup_function6768Now you have to edit the file `yourapplication.yourmodule` and add a69`setup_function` callback that creates an environment:7071.. sourcecode:: python7273 from jinja import Environment, FileSystemLoader7475 def setup_function(options):76 return Environment(loader=FileSystemLoader('path/to/templates'))7778This solution isn't the best but currently the only thing you can do. There is79a discussion about improving the plugin interface so that this can be solved80in a more elegant way.8182Also here exists the same limitation regarding dotted notation, see the83snipped and information in the pylons section.848560.. _TurboGears: http://www.turbogears.org/61.. _pylons: http://www.pylonshq.com/
modified:docs/src/recipies.txt+116 added
20 unmodified lines
21``cycle`` can take an unlimited amount of strings. Each time this22tag is encountered the next item from the list is rendered.23If you pass it just one argument it's meant to be a sequence.242526Active Menu Item27================2829Often you want to have a navigation bar with an active navigation
23 unmodified lines
53 .. sourcecode:: jinja5455 {% extends "layout.html" %}56 {% set active_page = "index" %}575859Sitemap60=======6162To create a sitemap you can either use the ``for`` tag or a ``macro``63that calls itself. The datastructures should look like this:6465.. sourcecode:: python6667 {'sitemap': [68 dict(69 caption='Pages',70 children=[71 dict(href='index.html', caption='Index'),72 dict(href='downloads.html', caption='Downloads'),73 dict(74 caption='Users',75 children=[76 dict(href='peter.html',77 caption='Peter'),78 dict(href='max.html',79 caption='Max'),80 dict(href='suzan.html',81 caption='Suzan')82 ]83 ),84 dict(85 caption='Files',86 children=[87 dict(88 caption='Images',89 children=[90 dict(href='vienna.html',91 caption='Vienna'),92 dict(href='roma.html',93 caption='Roma'),94 dict(href='tokyo.html',95 caption='Tokyo')96 ]97 ),98 dict(99 caption='Videos',100 children=[101 dict(href='party.html',102 caption='Party')103 ]104 )105 ]106 )107 ]108 ),109 dict(caption='Foo', href='foo.html')110 dict(caption='About', href='about.html')111 ]}112113Now you can create a sitemap using ``for``:114115.. sourcecode:: html+jinja116117 <ul class="sitemap">118 {% for item in sitemap recursive %}119 <li><a href="{{ item.href|e }}">{{ item.caption|e }}</a>120 {% if item.children %}<ul>{{ loop(item.children) }}</ul>{% endif %}</li>121 {% endfor %}122 </ul>123124Or by using a ``macro`` that calls itself:125126.. sourcecode:: html+jinja127128 {% macro render_sitemap items %}129 {% for item in items %}130 <li><a href="{{ item.href|e }}">{{ item.caption|e }}</a>131 {% if item.children %}<ul>{{ render_sitemap(item.children) }}</ul>{% endif %}</li>132 {% endfor %}133 {% endmacro %}134 <ul class="sitemap">{{ render_sitemap(sitemap) }}</ul>135136137Using A Block Multiple Times138============================139140Blocks have the small disadvantage that they work both ways which is a problem141if you want to render a block two times on a page. Here a nice little142workaround for this limitation:143144.. sourcecode:: html+jinja145146 <html>147 <head>148 <title>{% filter capture('title') %}{%149 block title %}{% endblock %}{%150 endfilter %}</title>151 </head>152 <body>153 <div class="head">{{ title }}</div>154 </body>155 </html>156157Or if you use the `capture` filter in `clean` mode:158159.. sourcecode:: html+jinja160161 {% filter capture('title', True)|trim %}162 {% block title %}{% endblock %}163 {% endfilter %}164 <html>165 <head>166 <title>{{ title }}</title>167 </head>168 <body>169 <div class="head">{{ title }}</div>170 </body>171 </html>
modified:jinja/filters.py+27 added-3 removed
563 unmodified lines
564 return env.to_unicode(value) % args565 return wrapped566567568def do_capture(name='captured'):568def do_trim(value):569 """570 Strip leading and trailing whitespace.571 """572 return value.strip()573do_trim = stringfilter(do_trim)574575576def do_capture(name='captured', clean=False):577 """578 Store the value in a variable called ``captured`` or a variable579 with the name provided. Useful for filter blocks:580581 .. sourcecode:: jinja582583 {% filter capture('foo') %}576 ....584 ...585 {% endfilter %}586 {{ foo }}587588 This will output "..." two times. One time from the filter block589 and one time from the variable. If you don't want the filter to590 output something you can use it in `clean` mode:591592 .. sourcecode:: jinja593594 {% filter capture('foo', True) %}595 ...596 {% endfilter %}597 {{ foo }}598 """599 if not isinstance(name, unicode):600 raise FilterArgumentError('You can only capture into variables')601 def wrapped(env, context, value):602 context[name] = value603 if clean:604 return Undefined605 return value606 return wrapped607608
32 unmodified lines
641 'float': do_float,642 'string': do_string,643 'urlize': do_urlize,644 'format': do_format,622 'capture': do_capture645 'capture': do_capture,646 'trim': do_trim647}
modified:jinja/plugin.py+3 added-2 removed
1 unmodified line
2"""3 jinja.plugin4 ~~~~~~~~~~~~56 Provide a bridge to buffet. Buffet is used by some frameworks (namely7 CherryPy, TurboGears and Pylons) to load templates.6 This module proides a bridge to buffed so that you can use Jinja7 in pylons. Note that this doesn't work with turbogears because their8 buffet implementation is broken.910 :copyright: 2007 by Armin Ronacher.11 :license: BSD, see LICENSE for more details.12"""
58 unmodified lines
modified:tests/test_filters.py+6 added
40 unmodified lines
41RANDOM = '''{{ seq|random }}'''42REVERSE = '''{{ "foobar"|reverse }}|{{ [1, 2, 3]|reverse }}'''43STRING = '''{{ range(10)|string }}'''44TITLE = '''{{ "foo bar"|title }}'''45TRIM = '''{{ " foo "|trim }}'''46TRUNCATE = '''{{ data|truncate(15, true, ">>>") }}|\47{{ data|truncate(15, false, ">>>") }}|\48{{ smalldata|truncate(15) }}'''49UPPER = '''{{ "foo"|upper }}'''
130 unmodified lines
180181182def test_truncate(env):183 tmpl = env.from_string(TRUNCATE)184 assert tmpl.render() == 'foo'185186187def test_truncate(env):188 tmpl = env.from_string(TRUNCATE)189 out = tmpl.render(data='foobar baz bar' * 1000,190 smalldata='foobar baz bar')191 assert out == 'foobar baz barf>>>|foobar baz >>>|foobar baz bar'192
30 unmodified lines
modified:setup.py+1 added-1 removed
31 unmodified lines
32 packages = ['jinja', 'jinja.translators'],33 extras_require = {'plugin': ['setuptools>=0.6a2']},34 entry_points='''35 [python.templating.engines]36 jinja = jinja.bakerplugin:JinjaPlugin[plugin]36 jinja = jinja.plugin:JinjaPlugin[plugin]37 '''38)