[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
Provenance
- Commit
566295e- Tree
7bc544d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
252f321
6 files changed+155 added−34 removed
modified:docs/src/frameworks.txt+2 added-28 removed
51 unmodified lines52 53 TurboGears54 ----------55 56-Integrating Jinja into TurboGears works pretty much the same.56+The TurboGears template plugin interface is broken by design and57+there is currently no way to embed Jinja templates in TurboGears.58 58-Basically all you have to do is to edit the application configuration file at59-``application/config/app.cfg`` and change `global.tg.defaultview` and60-provide the jinja options:59 62-.. sourcecode:: ini63-64- [global]65- tg.defaultview = 'jinja'66- jinja.init_callback = yourapplication.yourmodule.setup_function67-68-Now you have to edit the file `yourapplication.yourmodule` and add a69-`setup_function` callback that creates an environment:70-71-.. sourcecode:: python72-73- from jinja import Environment, FileSystemLoader74-75- def setup_function(options):76- return Environment(loader=FileSystemLoader('path/to/templates'))77-78-This solution isn't the best but currently the only thing you can do. There is79-a discussion about improving the plugin interface so that this can be solved80-in a more elegant way.81-82-Also here exists the same limitation regarding dotted notation, see the83-snipped and information in the pylons section.84-85-60 .. _TurboGears: http://www.turbogears.org/61 .. _pylons: http://www.pylonshq.com/modified:docs/src/recipies.txt+116 added
20 unmodified lines21 ``cycle`` can take an unlimited amount of strings. Each time this22 tag is encountered the next item from the list is rendered.23 If you pass it just one argument it's meant to be a sequence.24 25+26 Active Menu Item27 ================28 29 Often you want to have a navigation bar with an active navigation23 unmodified lines53 .. sourcecode:: jinja54 55 {% extends "layout.html" %}56 {% set active_page = "index" %}57+58+59+Sitemap60+=======61+62+To create a sitemap you can either use the ``for`` tag or a ``macro``63+that calls itself. The datastructures should look like this:64+65+.. sourcecode:: python66+67+ {'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+ ]}112+113+Now you can create a sitemap using ``for``:114+115+.. sourcecode:: html+jinja116+117+ <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>123+124+Or by using a ``macro`` that calls itself:125+126+.. sourcecode:: html+jinja127+128+ {% 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>135+136+137+Using A Block Multiple Times138+============================139+140+Blocks have the small disadvantage that they work both ways which is a problem141+if you want to render a block two times on a page. Here a nice little142+workaround for this limitation:143+144+.. sourcecode:: html+jinja145+146+ <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>156+157+Or if you use the `capture` filter in `clean` mode:158+159+.. sourcecode:: html+jinja160+161+ {% 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 lines564 return env.to_unicode(value) % args565 return wrapped566 567 568-def do_capture(name='captured'):568+def do_trim(value):569 """570+ Strip leading and trailing whitespace.571+ """572+ return value.strip()573+do_trim = stringfilter(do_trim)574+575+576+def 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:580 581 .. sourcecode:: jinja582 583 {% filter capture('foo') %}576- ....584+ ...585 {% endfilter %}586 {{ foo }}587+588+ 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:591+592+ .. sourcecode:: jinja593+594+ {% 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 wrapped607 608 32 unmodified lines641 '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 line2 """3 jinja.plugin4 ~~~~~~~~~~~~5 6- 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.9 10 :copyright: 2007 by Armin Ronacher.11 :license: BSD, see LICENSE for more details.12 """58 unmodified linesmodified:tests/test_filters.py+6 added
40 unmodified lines41 RANDOM = '''{{ seq|random }}'''42 REVERSE = '''{{ "foobar"|reverse }}|{{ [1, 2, 3]|reverse }}'''43 STRING = '''{{ range(10)|string }}'''44 TITLE = '''{{ "foo bar"|title }}'''45+TRIM = '''{{ " foo "|trim }}'''46 TRUNCATE = '''{{ data|truncate(15, true, ">>>") }}|\47 {{ data|truncate(15, false, ">>>") }}|\48 {{ smalldata|truncate(15) }}'''49 UPPER = '''{{ "foo"|upper }}'''130 unmodified lines180 181 182 def test_truncate(env):183 tmpl = env.from_string(TRUNCATE)184+ assert tmpl.render() == 'foo'185+186+187+def 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 linesmodified:setup.py+1 added-1 removed
31 unmodified lines32 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 )