1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pyramid_jinja2 — pyramid_jinja2 2.4 documentation</title>
<link rel="stylesheet" href="_static/pylons.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.4',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="pyramid_jinja2 2.4 documentation" href="#" />
<link rel="next" title="pyramid_jinja2 API" href="api.html" />
<link rel="stylesheet" href="http://static.pylonsproject.org/fonts/nobile/stylesheet.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="http://static.pylonsproject.org/fonts/neuton/stylesheet.css" type="text/css" media="screen" charset="utf-8" />
<!--[if lte IE 6]>
<link rel="stylesheet" href="_static/ie6.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->
<link rel="shortcut icon" href="_static/pyramid.ico"/>
</head>
<body role="document">
<div class="header-small">
<div class="logo-small">
<a href="#">
<img class="logo" src="_static/pyramid-small.png" alt="Logo"/>
</a>
</div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="api.html" title="pyramid_jinja2 API"
accesskey="N">next</a> |</li>
<li><a href="#">pyramid_jinja2 2.4 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="pyramid-jinja2">
<h1>pyramid_jinja2<a class="headerlink" href="#pyramid-jinja2" title="Permalink to this headline">¶</a></h1>
<div class="section" id="overview">
<span id="id1"></span><h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="glossary.html#term-pyramid-jinja2"><span class="xref std std-term">pyramid_jinja2</span></a> is a set of bindings that make templates written for the
<a class="reference internal" href="glossary.html#term-jinja2"><span class="xref std std-term">Jinja2</span></a> templating system work under the <a class="reference internal" href="glossary.html#term-pyramid"><span class="xref std std-term">Pyramid</span></a> web framework.</p>
</div>
<div class="section" id="installation">
<span id="instalation"></span><h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>Install using setuptools, e.g. (within a virtualenv):</p>
<div class="highlight-python"><div class="highlight"><pre>$ $VENV/bin/easy_install pyramid_jinja2
</pre></div>
</div>
</div>
<div class="section" id="setup">
<span id="id2"></span><h2>Setup<a class="headerlink" href="#setup" title="Permalink to this headline">¶</a></h2>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you start a project from scratch, consider using the
<a class="reference internal" href="#jinja2-starter-template"><span>project template</span></a> which comes with a
working setup and sensible defaults.</p>
</div>
<p>There are multiple ways to make sure that <code class="docutils literal"><span class="pre">pyramid_jinja2</span></code> is active.
All are completely equivalent:</p>
<ol class="arabic">
<li><p class="first">Use the <a class="reference internal" href="api.html#pyramid_jinja2.includeme" title="pyramid_jinja2.includeme"><code class="xref py py-func docutils literal"><span class="pre">includeme()</span></code></a> function via
<a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html#pyramid.config.Configurator.include" title="(in The Pyramid Web Framework v1.5.4)"><code class="xref py py-meth docutils literal"><span class="pre">include()</span></code></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">config</span> <span class="o">=</span> <span class="n">Configurator</span><span class="p">()</span>
<span class="n">config</span><span class="o">.</span><span class="n">include</span><span class="p">(</span><span class="s">'pyramid_jinja2'</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">Add <code class="docutils literal"><span class="pre">pyramid_jinja2</span></code> to the list of your <code class="docutils literal"><span class="pre">pyramid.includes</span></code> in your
<code class="file docutils literal"><span class="pre">.ini</span></code> settings file:</p>
<div class="highlight-python"><div class="highlight"><pre>pyramid.includes =
pyramid_jinja2
</pre></div>
</div>
</li>
<li><p class="first">If you’re using <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid_zcml/en/latest/">pyramid_zcml</a>
instead of imperative configuration, ensure that some ZCML file with an
analogue of the following contents is executed by your Pyramid
application:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt"><include</span> <span class="na">package=</span><span class="s">"pyramid_jinja2"</span><span class="nt">/></span>
</pre></div>
</div>
</li>
</ol>
<p>Once activated either of these says, the following happens:</p>
<ol class="arabic simple">
<li>Files with the <code class="file docutils literal"><span class="pre">.jinja2</span></code> extension are considered to be
<a class="reference internal" href="glossary.html#term-jinja2"><span class="xref std std-term">Jinja2</span></a> templates and a <a class="reference external" href="http://jinja.pocoo.org/docs/api/#jinja2.Environment" title="(in Jinja2 v2.8-dev)"><code class="xref py py-class docutils literal"><span class="pre">jinja2.Environment</span></code></a> is registered
to handle this extension.</li>
<li>The <a class="reference internal" href="api.html#pyramid_jinja2.add_jinja2_renderer" title="pyramid_jinja2.add_jinja2_renderer"><code class="xref py py-func docutils literal"><span class="pre">pyramid_jinja2.add_jinja2_renderer()</span></code></a> directive is added to the
<a class="reference internal" href="glossary.html#term-configurator"><span class="xref std std-term">Configurator</span></a> instance.</li>
<li>The <a class="reference internal" href="api.html#pyramid_jinja2.add_jinja2_search_path" title="pyramid_jinja2.add_jinja2_search_path"><code class="xref py py-func docutils literal"><span class="pre">pyramid_jinja2.add_jinja2_search_path()</span></code></a> directive is added to
the <a class="reference internal" href="glossary.html#term-configurator"><span class="xref std std-term">Configurator</span></a> instance.</li>
<li>The <a class="reference internal" href="api.html#pyramid_jinja2.add_jinja2_extension" title="pyramid_jinja2.add_jinja2_extension"><code class="xref py py-func docutils literal"><span class="pre">pyramid_jinja2.add_jinja2_extension()</span></code></a> directive is added to the
<a class="reference internal" href="glossary.html#term-configurator"><span class="xref std std-term">Configurator</span></a> instance.</li>
<li>The <a class="reference internal" href="api.html#pyramid_jinja2.get_jinja2_environment" title="pyramid_jinja2.get_jinja2_environment"><code class="xref py py-func docutils literal"><span class="pre">pyramid_jinja2.get_jinja2_environment()</span></code></a> directive is added to the
<a class="reference internal" href="glossary.html#term-configurator"><span class="xref std std-term">Configurator</span></a> instance.</li>
</ol>
<div class="section" id="preparing-for-distribution">
<h3>Preparing for distribution<a class="headerlink" href="#preparing-for-distribution" title="Permalink to this headline">¶</a></h3>
<p>If you want to make sure your <code class="file docutils literal"><span class="pre">.jinja2</span></code> template files are included in
your package’s source distribution (e.g. when using <code class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">sdist</span></code>),
add <code class="docutils literal"><span class="pre">*.jinja2</span></code> to your <code class="file docutils literal"><span class="pre">MANIFEST.in</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre>recursive-include yourapp *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.jinja2 *.js *.html *.xml
</pre></div>
</div>
</div>
</div>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<p>Once <cite>pyramid_jinja2</cite> has been activated, <code class="file docutils literal"><span class="pre">.jinja2</span></code> templates can be
used by the Pyramid rendering system.</p>
<p>When used as the <code class="docutils literal"><span class="pre">renderer</span></code> argument of a view, the view must return a
Python <code class="docutils literal"><span class="pre">dict</span></code> which will be passed into the template as the set of available
variables.</p>
<div class="section" id="template-lookup-mechanisms">
<h3>Template Lookup Mechanisms<a class="headerlink" href="#template-lookup-mechanisms" title="Permalink to this headline">¶</a></h3>
<p>There are several ways to configure <cite>pyramid_jinja2</cite> to find your templates.</p>
<div class="section" id="asset-specifications">
<h4>Asset Specifications<a class="headerlink" href="#asset-specifications" title="Permalink to this headline">¶</a></h4>
<p>Templates may always be defined using an <a class="reference internal" href="glossary.html#term-asset-specification"><span class="xref std std-term">asset specification</span></a>. These
are strings which define an absolute location of the template relative to
some Python package. For example <code class="docutils literal"><span class="pre">myapp.views:templates/home.jinja2</span></code>. These
specifications are supported throughout Pyramid and provide a fool-proof way
to find any supporting assets bundled with your application.</p>
<p>Here’s an example view configuration which uses an <a class="reference internal" href="glossary.html#term-asset-specification"><span class="xref std std-term">asset specification</span></a>:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre><span class="nd">@view_config</span><span class="p">(</span><span class="n">renderer</span><span class="o">=</span><span class="s">'mypackage:templates/foo.jinja2'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">return</span> <span class="p">{</span><span class="s">'a'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}</span>
</pre></div>
</td></tr></table></div>
<p>Asset specifications have some significant benefits in Pyramid, as they are
fully overridable. An addon package can ship with code that renders using
asset specifications. Later another package can externally override the
templates without having to actually modify the addon in any way. See
<a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/assets.html#overriding-assets-section" title="(in The Pyramid Web Framework v1.5.4)"><span>Overriding Assets</span></a> for more information.</p>
</div>
<div class="section" id="caller-relative-template-lookup">
<h4>Caller-Relative Template Lookup<a class="headerlink" href="#caller-relative-template-lookup" title="Permalink to this headline">¶</a></h4>
<p>By default, templates are discovered relative to the caller’s package. This
means that if you define a view in a Python module, the templates would
be found relative to the module’s directory on the filesystem.</p>
<p>Let’s look at an example:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre><span class="nd">@view_config</span><span class="p">(</span><span class="n">renderer</span><span class="o">=</span><span class="s">'templates/mytemplate.jinja2'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">return</span> <span class="p">{</span><span class="s">'foo'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'bar'</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>
</pre></div>
</td></tr></table></div>
<p>Imagine that the above code is in a <code class="docutils literal"><span class="pre">myapp.admin.views</span></code> module. The template
would be relative to that module on the filesystem, as shown below:</p>
<div class="highlight-python"><div class="highlight"><pre>myapp
|- __init__.py
`- admin
|- views.py
`- templates
|- base.jinja2
`- mytemplate.jinja2
</pre></div>
</div>
<p>Caller-relative lookup avoids naming collisions which can be common in a
search path-based approach.</p>
<p>A caller-relative template lookup is converted to a <a class="reference internal" href="glossary.html#term-asset-specification"><span class="xref std std-term">asset specification</span></a>
underneath the hood. This means that it’s almost always possible to override
the actual template in an addon package without having to fork the addon
itself. For example, the full asset spec for the view above would be
<code class="docutils literal"><span class="pre">myapp.admin.views:templates/mytemplate.jinja2</span></code>. This template, or the
entire <code class="docutils literal"><span class="pre">templates</span></code> folder may be overridden.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">config</span><span class="o">.</span><span class="n">override_asset</span><span class="p">(</span>
<span class="n">to_override</span><span class="o">=</span><span class="s">'myapp.admin.views:templates/mytemplate.jinja2'</span><span class="p">,</span>
<span class="n">override_with</span><span class="o">=</span><span class="s">'yourapp:templates/sometemplate.jinja2'</span><span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/assets.html#overriding-assets-section" title="(in The Pyramid Web Framework v1.5.4)"><span>Overriding Assets</span></a> for more information.</p>
</div>
<div class="section" id="search-path-based-template-lookup">
<h4>Search Path-Based Template Lookup<a class="headerlink" href="#search-path-based-template-lookup" title="Permalink to this headline">¶</a></h4>
<p>When used outside of Pyramid, Jinja2’s default lookup mechanism is a search
path. To use a search path within Pyramid, simply define the
<code class="docutils literal"><span class="pre">jinja2.directories</span></code> configuration setting or use the
<a class="reference internal" href="api.html#pyramid_jinja2.add_jinja2_search_path" title="pyramid_jinja2.add_jinja2_search_path"><code class="xref py py-func docutils literal"><span class="pre">add_jinja2_search_path()</span></code></a> configurator directive.</p>
<p>Rendering <a class="reference internal" href="glossary.html#term-jinja2"><span class="xref std std-term">Jinja2</span></a> templates with a search path is typically done as
follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@view_config</span><span class="p">(</span><span class="n">renderer</span><span class="o">=</span><span class="s">'mytemplate.jinja2'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">return</span> <span class="p">{</span><span class="s">'foo'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'bar'</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>
</pre></div>
</div>
<p>If <code class="docutils literal"><span class="pre">mytemplate.jinja2</span></code> is not found in the same directory as the module
then it will be searched for on the search path. We are now dependent on our
configuration settings to tell us where the template may be located. Commonly
a <code class="docutils literal"><span class="pre">templates</span></code> directory is created at the base of the package and the
configuration file will include the following directive:</p>
<div class="highlight-python"><div class="highlight"><pre>jinja2.directories = mypkg:templates
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">It is possible to specify a relative path to the templates folder, such
as <code class="docutils literal"><span class="pre">jinja2.directories</span> <span class="pre">=</span> <span class="pre">templates</span></code>. This folder will be found relative
to the first package that includes <cite>pyramid_jinja2</cite>, which will normally
be the root of your application. It is always better to be explicit when
in doubt.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The package that includes <cite>pyramid_jinja2</cite> will always be added
to the search path (in most cases this is top-level package in your
application). This behavior may be deprecated or removed in the future,
it is always better to specify your search path explicitly.</p>
</div>
</div>
<div class="section" id="templates-including-templates">
<h4>Templates Including Templates<a class="headerlink" href="#templates-including-templates" title="Permalink to this headline">¶</a></h4>
<p><a class="reference internal" href="glossary.html#term-jinja2"><span class="xref std std-term">Jinja2</span></a> allows <a class="reference internal" href="glossary.html#term-template-inheritance"><span class="xref std std-term">template inheritance</span></a> as well as other mechanisms
for templates to load each other. The lookup mechanisms supported in these
cases include asset specifications, template-relative names and normal
template names found on the search path. The search path will always be
consulted if a template cannot be found relative to the parent
template. For example if you had a template named <code class="docutils literal"><span class="pre">templates/child.jinja2</span></code>
that wanted to extend <code class="docutils literal"><span class="pre">templates/base.jinja2</span></code> then it could use
<code class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">'base.jinja2'</span> <span class="pre">%}</span></code> and locate the file relative to itself <strong>or</strong>
it could use <code class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">'templates/base.jinja2'</span> <span class="pre">%}</span></code> to find the template
in a <code class="docutils literal"><span class="pre">templates</span></code> subfolder rooted on the search path. The template-relative
option will always override the search path.</p>
<p>An example:</p>
<div class="highlight-html+django"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11</pre></div></td><td class="code"><div class="highlight"><pre><span class="cp"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"></span>
<span class="c"><!-- templates/layout.jinja2 --></span>
<span class="nt"><html</span> <span class="na">lang=</span><span class="s">"en"</span><span class="nt">></span>
<span class="nt"><html</span> <span class="na">xmlns=</span><span class="s">"http://www.w3.org/1999/xhtml"</span><span class="nt">></span>
<span class="nt"><head></span>
<span class="nt"><link</span> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"style.css"</span> <span class="nt">/></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><div</span> <span class="na">id=</span><span class="s">"content"</span><span class="nt">></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="nt"></div></span>
<span class="nt"></body></span>
</pre></div>
</td></tr></table></div>
<div class="highlight-html+django"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8</pre></div></td><td class="code"><div class="highlight"><pre><span class="c"><!-- templates/root.jinja2 --></span>
<span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"templates/layout.jinja2"</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span>
<span class="nt"><h1></span>Yes<span class="nt"></h1></span>
<span class="nt"><p></span>
Some random paragraph.
<span class="nt"></p></span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</td></tr></table></div>
<p>For further information on <a class="reference internal" href="glossary.html#term-template-inheritance"><span class="xref std std-term">Template Inheritance</span></a> in Jinja2
templates please see <a class="reference external" href="http://jinja.pocoo.org/docs/templates/#template-inheritance" title="(in Jinja2 v2.8-dev)"><span class="xref std std-ref">Template Inheritance</span></a>
in Jinja2 documentation.</p>
</div>
</div>
<div class="section" id="adding-or-overriding-a-renderer">
<h3>Adding or Overriding a Renderer<a class="headerlink" href="#adding-or-overriding-a-renderer" title="Permalink to this headline">¶</a></h3>
<p>By default, only templates ending in the <code class="docutils literal"><span class="pre">.jinja2</span></code> file extension are
supported. However, it is very easy to add support for alternate file
extensions using the <a class="reference internal" href="api.html#pyramid_jinja2.add_jinja2_renderer" title="pyramid_jinja2.add_jinja2_renderer"><code class="xref py py-func docutils literal"><span class="pre">pyramid_jinja2.add_jinja2_renderer()</span></code></a> directive.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">config</span><span class="o">.</span><span class="n">include</span><span class="p">(</span><span class="s">'pyramid_jinja2'</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">add_jinja2_renderer</span><span class="p">(</span><span class="s">'.html'</span><span class="p">)</span>
</pre></div>
</div>
<p>It would now be possible to use templates named <code class="docutils literal"><span class="pre">foo.html</span></code> and
<code class="docutils literal"><span class="pre">foo.jinja2</span></code>. Each renderer extension will use its own
<a class="reference external" href="http://jinja.pocoo.org/docs/api/#jinja2.Environment" title="(in Jinja2 v2.8-dev)"><code class="xref py py-class docutils literal"><span class="pre">jinja2.Environment</span></code></a>. These alternate renderers can be extended at
runtime using the <code class="docutils literal"><span class="pre">name</span></code> parameter to the other directives such as
<a class="reference internal" href="api.html#pyramid_jinja2.get_jinja2_environment" title="pyramid_jinja2.get_jinja2_environment"><code class="xref py py-func docutils literal"><span class="pre">pyramid_jinja2.get_jinja2_environment()</span></code></a>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">config</span><span class="o">.</span><span class="n">include</span><span class="p">(</span><span class="s">'pyramid_jinja2'</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">add_jinja2_renderer</span><span class="p">(</span><span class="s">'.html'</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">add_jinja2_search_path</span><span class="p">(</span><span class="s">'myapp:templates'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'.html'</span><span class="p">)</span>
</pre></div>
</div>
<p>It is also possible to setup different renderers that use different search
paths, configuration settings and environments if necessary. This technique
can come in handy when different defaults are required for rendering templates
with different content types. For example, a plain text email body versus
an html page. For this reason, <a class="reference internal" href="api.html#pyramid_jinja2.add_jinja2_renderer" title="pyramid_jinja2.add_jinja2_renderer"><code class="xref py py-func docutils literal"><span class="pre">pyramid_jinja2.add_jinja2_renderer()</span></code></a>
accepts an optional parameter <code class="docutils literal"><span class="pre">settings_prefix</span></code> which can point a renderer
at a different group of settings.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">settings</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">'jinja2.directories'</span><span class="p">:</span> <span class="s">'myapp:html_templates'</span><span class="p">,</span>
<span class="s">'mail.jinja2.directories'</span><span class="p">:</span> <span class="s">'myapp:email_templates'</span><span class="p">,</span>
<span class="p">}</span>
<span class="n">config</span> <span class="o">=</span> <span class="n">Configurator</span><span class="p">(</span><span class="n">settings</span><span class="o">=</span><span class="n">settings</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">include</span><span class="p">(</span><span class="s">'pyramid_jinja2'</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">add_jinja2_renderer</span><span class="p">(</span><span class="s">'.email'</span><span class="p">,</span> <span class="n">settings_prefix</span><span class="o">=</span><span class="s">'mail.jinja2.'</span><span class="p">)</span>
</pre></div>
</div>
<p>Now <code class="docutils literal"><span class="pre">foo.email</span></code> will be rendered using the <code class="docutils literal"><span class="pre">mail.jinja2.*</span></code> settings.</p>
</div>
<div class="section" id="internalization-i18n">
<h3>Internalization (i18n)<a class="headerlink" href="#internalization-i18n" title="Permalink to this headline">¶</a></h3>
<p>When <a class="reference internal" href="glossary.html#term-pyramid-jinja2"><span class="xref std std-term">pyramid_jinja2</span></a> is included in a Pyramid application,
<a class="reference external" href="http://jinja.pocoo.org/docs/extensions/#i18n-extension" title="(in Jinja2 v2.8-dev)"><span class="xref std std-ref">jinja2.ext.i18n</span></a> is automatically activated.</p>
<p>Be sure to configure <cite>jinja2.i18n.domain</cite> according to <cite>setup.cfg</cite> domain
settings. By default, <cite>jinja2.i18n.domain</cite> is set to the name of the
package that included <cite>pyramid_jinja2</cite>. If no package was found, it will use
<code class="docutils literal"><span class="pre">messages</span></code>.</p>
</div>
</div>
<div class="section" id="settings">
<span id="id3"></span><h2>Settings<a class="headerlink" href="#settings" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="glossary.html#term-jinja2"><span class="xref std std-term">Jinja2</span></a> derives additional settings to configure its template renderer.
Many of these settings are optional and only need to be set if they should be
different from the default. The below values can be present in the
<code class="file docutils literal"><span class="pre">.ini</span></code> file used to configure the Pyramid application (in the <code class="docutils literal"><span class="pre">app</span></code>
section representing your Pyramid app) or they can be passed directly within
the <code class="docutils literal"><span class="pre">settings</span></code> argument passed to a Pyramid Configurator.</p>
<div class="section" id="generic-settings">
<h3>Generic Settings<a class="headerlink" href="#generic-settings" title="Permalink to this headline">¶</a></h3>
<p>These settings correspond to the ones documented in Jinja2.
Set them accordingly.</p>
<p>For reference please see: <a class="reference external" href="http://jinja.pocoo.org/docs/api/#high-level-api">http://jinja.pocoo.org/docs/api/#high-level-api</a></p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For the boolean settings please use <code class="docutils literal"><span class="pre">true</span></code> or <code class="docutils literal"><span class="pre">false</span></code></p>
</div>
<p>jinja2.block_start_string</p>
<p>jinja2.block_end_string</p>
<p>jinja2.variable_start_string</p>
<p>jinja2.variable_end_string</p>
<p>jinja2.comment_start_string</p>
<p>jinja2.comment_end_string</p>
<p>jinja2.line_statement_prefix</p>
<p>jinja2.line_comment_prefix</p>
<p>jinja2.trim_blocks</p>
<p>jinja2.newline_sequence</p>
<p>jinja2.optimized</p>
<p>jinja2.cache_size</p>
</div>
<div class="section" id="jinja2-autoescape">
<h3>jinja2.autoescape<a class="headerlink" href="#jinja2-autoescape" title="Permalink to this headline">¶</a></h3>
<p>Jinja2 autoescape setting.</p>
<p>Possible values: <code class="docutils literal"><span class="pre">true</span></code> or <code class="docutils literal"><span class="pre">false</span></code>.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>By default Jinja2 sets autoescaping to <code class="docutils literal"><span class="pre">False</span></code>.</p>
<p class="last">pyramid_jinja2 sets it to true as it is considered a good security
practice in a web setting where we want to prevent XSS attacks from
rendering unsanitized user-generated content. To turn off escaping
on a case-by-case basis you may use the <code class="docutils literal"><span class="pre">safe</span></code> filter such as
<code class="docutils literal"><span class="pre">{{</span> <span class="pre">html_blob</span> <span class="pre">|</span> <span class="pre">safe</span> <span class="pre">}}</span></code>.</p>
</div>
</div>
<div class="section" id="pyramid-reload-templates">
<span id="setting-reload-templates"></span><h3>pyramid.reload_templates<a class="headerlink" href="#pyramid-reload-templates" title="Permalink to this headline">¶</a></h3>
<p>For usage see <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/templates.html#reload-templates-section" title="(in The Pyramid Web Framework v1.5.4)"><span class="xref std std-ref">Pyramid: Automatically Reloading Templates</span></a>.</p>
<p><code class="docutils literal"><span class="pre">true</span></code> or <code class="docutils literal"><span class="pre">false</span></code> representing whether Jinja2 templates should be
reloaded when they change on disk. Useful for development to be <code class="docutils literal"><span class="pre">true</span></code>.
This setting sets the Jinja2 <code class="docutils literal"><span class="pre">auto_reload</span></code> setting.</p>
</div>
<div class="section" id="reload-templates">
<h3>reload_templates<a class="headerlink" href="#reload-templates" title="Permalink to this headline">¶</a></h3>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Deprecated as of version 1.5, use <a class="reference internal" href="#setting-reload-templates"><span>pyramid.reload_templates</span></a> instead</p>
</div>
</div>
<div class="section" id="jinja2-auto-reload">
<span id="setting-jinja2-autoreload"></span><h3>jinja2.auto_reload<a class="headerlink" href="#jinja2-auto-reload" title="Permalink to this headline">¶</a></h3>
<p>Use Pyramid <a class="reference internal" href="#setting-reload-templates"><span>pyramid.reload_templates</span></a> setting.</p>
</div>
<div class="section" id="jinja2-directories">
<span id="setting-jinja2-directories"></span><h3>jinja2.directories<a class="headerlink" href="#jinja2-directories" title="Permalink to this headline">¶</a></h3>
<p>A list of directory names or a newline-delimited string with each line
representing a directory name. These locations are where Jinja2 will
search for templates. Each can optionally be an absolute resource
specification (e.g. <code class="docutils literal"><span class="pre">package:subdirectory/</span></code>).</p>
</div>
<div class="section" id="jinja2-input-encoding">
<span id="setting-jinja2-input-encoding"></span><h3>jinja2.input_encoding<a class="headerlink" href="#jinja2-input-encoding" title="Permalink to this headline">¶</a></h3>
<p>The input encoding of templates. Defaults to <code class="docutils literal"><span class="pre">utf-8</span></code>.</p>
</div>
<div class="section" id="jinja2-undefined">
<span id="setting-jinja2-undefined"></span><h3>jinja2.undefined<a class="headerlink" href="#jinja2-undefined" title="Permalink to this headline">¶</a></h3>
<p>Changes the undefined types that are used when a variable name lookup fails.
If unset, defaults to <a class="reference external" href="http://jinja.pocoo.org/docs/api/#jinja2.Undefined" title="(in Jinja2 v2.8-dev)"><code class="xref py py-class docutils literal"><span class="pre">Undefined</span></code></a> (silent ignore). Setting
it to <code class="docutils literal"><span class="pre">strict</span></code> will trigger <a class="reference external" href="http://jinja.pocoo.org/docs/api/#jinja2.StrictUndefined" title="(in Jinja2 v2.8-dev)"><code class="xref py py-class docutils literal"><span class="pre">StrictUndefined</span></code></a> behavior
(raising an error, this is recommended for development). Setting it to
<code class="docutils literal"><span class="pre">debug</span></code> will trigger <a class="reference external" href="http://jinja.pocoo.org/docs/api/#jinja2.DebugUndefined" title="(in Jinja2 v2.8-dev)"><code class="xref py py-class docutils literal"><span class="pre">DebugUndefined</span></code></a>, which outputs
debug information in some cases. See <a class="reference external" href="http://jinja.pocoo.org/docs/api/#undefined-types">Undefined Types</a></p>
</div>
<div class="section" id="jinja2-extensions">
<span id="setting-jinja2-extensions"></span><h3>jinja2.extensions<a class="headerlink" href="#jinja2-extensions" title="Permalink to this headline">¶</a></h3>
<p>A list of extension objects or a newline-delimited set of dotted import
locations where each line represents an extension. <a class="reference external" href="http://jinja.pocoo.org/docs/extensions/#i18n-extension" title="(in Jinja2 v2.8-dev)"><span class="xref std std-ref">jinja2.ext.i18n</span></a> is automatically activated.</p>
</div>
<div class="section" id="jinja2-i18n-domain">
<span id="setting-jinja2-i18n-domain"></span><h3>jinja2.i18n.domain<a class="headerlink" href="#jinja2-i18n-domain" title="Permalink to this headline">¶</a></h3>
<p>Pyramid domain for translations. See <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/glossary.html#term-translation-domain" title="(in The Pyramid Web Framework v1.5.4)"><span class="xref std std-term">Translation Domain</span></a> in Pyramid
documentation. Defaults to the name of the package that activated
<cite>pyramid_jinja2</cite> or if that fails it will use <code class="docutils literal"><span class="pre">messages</span></code> as the domain.</p>
</div>
<div class="section" id="jinja2-filters">
<span id="setting-jinja2-filers"></span><h3>jinja2.filters<a class="headerlink" href="#jinja2-filters" title="Permalink to this headline">¶</a></h3>
<p>A dictionary mapping filter name to filter object, or a newline-delimted
string with each line in the format:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">name</span> <span class="o">=</span> <span class="n">dotted</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">filter</span>
</pre></div>
</div>
<p>representing <a class="reference external" href="http://jinja.pocoo.org/docs/api/#writing-filters" title="(in Jinja2 v2.8-dev)"><span class="xref std std-ref">Jinja2 filters</span></a>.</p>
</div>
<div class="section" id="jinja2-globals">
<span id="setting-jinja2-globals"></span><h3>jinja2.globals<a class="headerlink" href="#jinja2-globals" title="Permalink to this headline">¶</a></h3>
<p>A dictionary mapping global name to global template object,
or a newline-delimited string with each line in the format:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">name</span> <span class="o">=</span> <span class="n">dotted</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">globals</span>
</pre></div>
</div>
<p>representing <a class="reference external" href="http://jinja.pocoo.org/docs/api/#global-namespace" title="(in Jinja2 v2.8-dev)"><span class="xref std std-ref">Jinja2 globals</span></a></p>
</div>
<div class="section" id="jinja2-tests">
<span id="setting-jinja2-tests"></span><h3>jinja2.tests<a class="headerlink" href="#jinja2-tests" title="Permalink to this headline">¶</a></h3>
<p>A dictionary mapping test name to test object, or a newline-delimted
string with each line in the format:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">name</span> <span class="o">=</span> <span class="n">dotted</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">test</span>
</pre></div>
</div>
<p>representing <a class="reference external" href="http://jinja.pocoo.org/docs/api/#writing-tests" title="(in Jinja2 v2.8-dev)"><span class="xref std std-ref">Jinja2 tests</span></a>.</p>
</div>
<div class="section" id="jinja2-bytecode-caching">
<span id="setting-jinja2-byte-cache"></span><h3>jinja2.bytecode_caching<a class="headerlink" href="#jinja2-bytecode-caching" title="Permalink to this headline">¶</a></h3>
<p>If set to <code class="docutils literal"><span class="pre">true</span></code>, a filesystem bytecode cache will be configured
(in a directory determined by <a class="reference internal" href="#setting-jinja2-byte-cache-dir"><span>jinja2.bytecode_caching_directory</span></a>.)
To configure other types of bytecode caching, <code class="docutils literal"><span class="pre">jinja2.bytecode_caching</span></code>
may also be set directly to an instance of <a class="reference external" href="http://jinja.pocoo.org/docs/api/#jinja2.BytecodeCache" title="(in Jinja2 v2.8-dev)"><code class="xref py py-class docutils literal"><span class="pre">jinja2.BytecodeCache</span></code></a>
(This can not be done in a paste <code class="docutils literal"><span class="pre">.ini</span></code> file, however, it must be done
programatically.)
By default, no bytecode cache is configured.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.10: </span>Previously, <code class="docutils literal"><span class="pre">jinja2.bytecode_caching</span></code> defaulted to <code class="docutils literal"><span class="pre">true</span></code>.</p>
</div>
<p>Note that configuring a filesystem bytecode cache will (not surprisiningly)
generate files in the cache directory. As templates are changed, some
of these will become stale, pointless wastes of disk space.
You are advised to consider a clean up
strategy (such as a cron job) to check for and remove such files.</p>
<p>See the <a class="reference external" href="http://jinja.pocoo.org/docs/api/#bytecode-cache" title="(in Jinja2 v2.8-dev)"><span class="xref std std-ref">Jinja2 Documentation</span></a>
for more information on bytecode caching.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.10: </span>Previously, an atexit callback which called
<a class="reference external" href="http://jinja.pocoo.org/docs/api/#jinja2.BytecodeCache.clear" title="(in Jinja2 v2.8-dev)"><code class="xref py py-meth docutils literal"><span class="pre">jinja2.BytecodeCache.clear()</span></code></a> was registered in an effort
to delete the cache files. This is no longer done.</p>
</div>
</div>
<div class="section" id="jinja2-bytecode-caching-directory">
<span id="setting-jinja2-byte-cache-dir"></span><h3>jinja2.bytecode_caching_directory<a class="headerlink" href="#jinja2-bytecode-caching-directory" title="Permalink to this headline">¶</a></h3>
<p>Absolute path to directory to store bytecode cache files. Defaults to
the system temporary directory.
This is only used if <code class="docutils literal"><span class="pre">jinja2.bytecode_caching</span></code> is set to <code class="docutils literal"><span class="pre">true</span></code>.</p>
</div>
<div class="section" id="jinja2-newstyle">
<span id="setting-jinja2-newstyle"></span><h3>jinja2.newstyle<a class="headerlink" href="#jinja2-newstyle" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal"><span class="pre">true</span></code> or <code class="docutils literal"><span class="pre">false</span></code> to enable the use of newstyle gettext calls. Defaults to
<code class="docutils literal"><span class="pre">false</span></code>.</p>
<p>See <cite>Newstyle Gettext http://jinja.pocoo.org/docs/extensions/#newstyle-gettext</cite></p>
</div>
<div class="section" id="jinja2-finalize">
<span id="setting-jinja2-finalize"></span><h3>jinja2.finalize<a class="headerlink" href="#jinja2-finalize" title="Permalink to this headline">¶</a></h3>
<p>A callable or a dotted-import string.</p>
</div>
</div>
<div class="section" id="id4">
<span id="id5"></span><h2>Jinja2 Filters<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal"><span class="pre">pyramid_jinja2</span></code> provides following filters.</p>
<dl class="function">
<dt id="pyramid_jinja2.filters.model_url_filter">
<code class="descclassname">pyramid_jinja2.filters.</code><code class="descname">model_url_filter</code><span class="sig-paren">(</span><em>ctx</em>, <em>model</em>, <em>*elements</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#pyramid_jinja2.filters.model_url_filter" title="Permalink to this definition">¶</a></dt>
<dd><p>A filter from <code class="docutils literal"><span class="pre">model</span></code> to a string representing the absolute URL.
This filter calls <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/api/url.html#pyramid.url.resource_url" title="(in The Pyramid Web Framework v1.5.4)"><code class="xref py py-func docutils literal"><span class="pre">pyramid.url.resource_url()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="pyramid_jinja2.filters.route_url_filter">
<code class="descclassname">pyramid_jinja2.filters.</code><code class="descname">route_url_filter</code><span class="sig-paren">(</span><em>ctx</em>, <em>route_name</em>, <em>*elements</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#pyramid_jinja2.filters.route_url_filter" title="Permalink to this definition">¶</a></dt>
<dd><p>A filter from <code class="docutils literal"><span class="pre">route_name</span></code> to a string representing the absolute URL.
This filter calls <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/api/url.html#pyramid.url.route_url" title="(in The Pyramid Web Framework v1.5.4)"><code class="xref py py-func docutils literal"><span class="pre">pyramid.url.route_url()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="pyramid_jinja2.filters.static_url_filter">
<code class="descclassname">pyramid_jinja2.filters.</code><code class="descname">static_url_filter</code><span class="sig-paren">(</span><em>ctx</em>, <em>path</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#pyramid_jinja2.filters.static_url_filter" title="Permalink to this definition">¶</a></dt>
<dd><p>A filter from <code class="docutils literal"><span class="pre">path</span></code> to a string representing the absolute URL.
This filter calls <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/api/url.html#pyramid.url.static_url" title="(in The Pyramid Web Framework v1.5.4)"><code class="xref py py-func docutils literal"><span class="pre">pyramid.url.static_url()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="pyramid_jinja2.filters.model_path_filter">
<code class="descclassname">pyramid_jinja2.filters.</code><code class="descname">model_path_filter</code><span class="sig-paren">(</span><em>ctx</em>, <em>model</em>, <em>*elements</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#pyramid_jinja2.filters.model_path_filter" title="Permalink to this definition">¶</a></dt>
<dd><p>A filter from <code class="docutils literal"><span class="pre">model</span></code> to a string representing the relative URL.
This filter calls <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.resource_path" title="(in The Pyramid Web Framework v1.5.4)"><code class="xref py py-meth docutils literal"><span class="pre">pyramid.request.Request.resource_path()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="pyramid_jinja2.filters.route_path_filter">
<code class="descclassname">pyramid_jinja2.filters.</code><code class="descname">route_path_filter</code><span class="sig-paren">(</span><em>ctx</em>, <em>route_name</em>, <em>*elements</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#pyramid_jinja2.filters.route_path_filter" title="Permalink to this definition">¶</a></dt>
<dd><p>A filter from <code class="docutils literal"><span class="pre">route_name</span></code> to a string representing the relative URL.
This filter calls <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/api/url.html#pyramid.url.route_path" title="(in The Pyramid Web Framework v1.5.4)"><code class="xref py py-func docutils literal"><span class="pre">pyramid.url.route_path()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="pyramid_jinja2.filters.static_path_filter">
<code class="descclassname">pyramid_jinja2.filters.</code><code class="descname">static_path_filter</code><span class="sig-paren">(</span><em>ctx</em>, <em>path</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#pyramid_jinja2.filters.static_path_filter" title="Permalink to this definition">¶</a></dt>
<dd><p>A filter from <code class="docutils literal"><span class="pre">path</span></code> to a string representing the relative URL.
This filter calls <a class="reference external" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/api/url.html#pyramid.url.static_path" title="(in The Pyramid Web Framework v1.5.4)"><code class="xref py py-func docutils literal"><span class="pre">pyramid.url.static_path()</span></code></a>.</p>
</dd></dl>
<p>To use these filters, configure the settings of <code class="docutils literal"><span class="pre">jinja2.filters</span></code>:</p>
<div class="highlight-ini"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">[app:yourapp]</span>
<span class="c1"># ... other stuff ...</span>
<span class="na">jinja2.filters</span> <span class="o">=</span><span class="s"></span>
<span class="s"> model_url = pyramid_jinja2.filters:model_url_filter</span>
<span class="s"> route_url = pyramid_jinja2.filters:route_url_filter</span>
<span class="s"> static_url = pyramid_jinja2.filters:static_url_filter</span>
</pre></div>
</td></tr></table></div>
<p>And use the filters in template.</p>
<div class="highlight-html"><div class="highlight"><pre><span class="nt"><a</span> <span class="na">href=</span><span class="s">"{{context|model_url('edit')}}"</span><span class="nt">></span>Edit<span class="nt"></a></span>
<span class="nt"><a</span> <span class="na">href=</span><span class="s">"{{'top'|route_url}}"</span><span class="nt">></span>Top<span class="nt"></a></span>
<span class="nt"><link</span> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"{{'yourapp:static/css/style.css'|static_url}}"</span> <span class="nt">/></span>
</pre></div>
</div>
</div>
<div class="section" id="creating-a-jinja2-pyramid-project">
<span id="jinja2-starter-template"></span><h2>Creating a Jinja2 <code class="docutils literal"><span class="pre">Pyramid</span></code> Project<a class="headerlink" href="#creating-a-jinja2-pyramid-project" title="Permalink to this headline">¶</a></h2>
<p>After you’ve got <code class="docutils literal"><span class="pre">pyramid_jinja2</span></code> installed, you can invoke one of the
following commands to create a Jinja2-based Pyramid project.</p>
<p>On Pyramid 1.0, 1.1, or 1.2:</p>
<div class="highlight-python"><div class="highlight"><pre>$ $VENV/bin/paster create -t pyramid_jinja2_starter myproject
</pre></div>
</div>
<p>On Pyramid 1.3+:</p>
<div class="highlight-python"><div class="highlight"><pre>$ $VENV/bin/pcreate -s pyramid_jinja2_starter myproject
</pre></div>
</div>
<p>After it’s created, you can visit the <code class="docutils literal"><span class="pre">myproject</span></code> directory and run
<code class="docutils literal"><span class="pre">setup.py</span> <span class="pre">develop</span></code>. At that point you can start the application like any
other Pyramid application.</p>
<p>This is a good way to see a working Pyramid application that uses Jinja2, even
if you wind up not using the result.</p>
<div class="section" id="paster-template-i18n">
<h3>Paster Template I18N<a class="headerlink" href="#paster-template-i18n" title="Permalink to this headline">¶</a></h3>
<p>The paster template automatically sets up pot/po/mo locale files for use
with the generated project.</p>
<p>The usual pattern for working with i18n in pyramid_jinja2 is as follows:</p>
<div class="highlight-text"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10</pre></div></td><td class="code"><div class="highlight"><pre># make sure Babel is installed
easy_install Babel
# extract translatable strings from *.jinja2 / *.py
python setup.py extract_messages
python setup.py update_catalog
# Translate strings in <mypackage>/locale/<mylocale>/LC_MESSAGES/<myproject>.po
# and re-compile *.po files
python setup.py compile_catalog
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="more-information">
<h2>More Information<a class="headerlink" href="#more-information" title="Permalink to this headline">¶</a></h2>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="api.html"><code class="docutils literal"><span class="pre">pyramid_jinja2</span></code> API</a></li>
<li class="toctree-l1"><a class="reference internal" href="changes.html">Changes</a></li>
<li class="toctree-l1"><a class="reference internal" href="glossary.html">Glossary</a></li>
</ul>
</div>
</div>
<div class="section" id="reporting-bugs-development-versions">
<h2>Reporting Bugs / Development Versions<a class="headerlink" href="#reporting-bugs-development-versions" title="Permalink to this headline">¶</a></h2>
<p>Visit <a class="reference external" href="http://github.com/Pylons/pyramid_jinja2">http://github.com/Pylons/pyramid_jinja2</a> to download development or tagged
versions.</p>
<p>Visit <a class="reference external" href="http://github.com/Pylons/pyramid_jinja2/issues">http://github.com/Pylons/pyramid_jinja2/issues</a> to report bugs.</p>
<div class="section" id="indices-and-tables">
<h3>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li><a class="reference internal" href="glossary.html#glossary"><span>Glossary</span></a></li>
<li><a class="reference internal" href="genindex.html"><span>Index</span></a></li>
<li><a class="reference internal" href="py-modindex.html"><span>Module Index</span></a></li>
<li><a class="reference internal" href="search.html"><span>Search Page</span></a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="#">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">pyramid_jinja2</a><ul>
<li><a class="reference internal" href="#overview">Overview</a></li>
<li><a class="reference internal" href="#installation">Installation</a></li>
<li><a class="reference internal" href="#setup">Setup</a><ul>
<li><a class="reference internal" href="#preparing-for-distribution">Preparing for distribution</a></li>
</ul>
</li>
<li><a class="reference internal" href="#usage">Usage</a><ul>
<li><a class="reference internal" href="#template-lookup-mechanisms">Template Lookup Mechanisms</a><ul>
<li><a class="reference internal" href="#asset-specifications">Asset Specifications</a></li>
<li><a class="reference internal" href="#caller-relative-template-lookup">Caller-Relative Template Lookup</a></li>
<li><a class="reference internal" href="#search-path-based-template-lookup">Search Path-Based Template Lookup</a></li>
<li><a class="reference internal" href="#templates-including-templates">Templates Including Templates</a></li>
</ul>
</li>
<li><a class="reference internal" href="#adding-or-overriding-a-renderer">Adding or Overriding a Renderer</a></li>
<li><a class="reference internal" href="#internalization-i18n">Internalization (i18n)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#settings">Settings</a><ul>
<li><a class="reference internal" href="#generic-settings">Generic Settings</a></li>
<li><a class="reference internal" href="#jinja2-autoescape">jinja2.autoescape</a></li>
<li><a class="reference internal" href="#pyramid-reload-templates">pyramid.reload_templates</a></li>
<li><a class="reference internal" href="#reload-templates">reload_templates</a></li>
<li><a class="reference internal" href="#jinja2-auto-reload">jinja2.auto_reload</a></li>
<li><a class="reference internal" href="#jinja2-directories">jinja2.directories</a></li>
<li><a class="reference internal" href="#jinja2-input-encoding">jinja2.input_encoding</a></li>
<li><a class="reference internal" href="#jinja2-undefined">jinja2.undefined</a></li>
<li><a class="reference internal" href="#jinja2-extensions">jinja2.extensions</a></li>
<li><a class="reference internal" href="#jinja2-i18n-domain">jinja2.i18n.domain</a></li>
<li><a class="reference internal" href="#jinja2-filters">jinja2.filters</a></li>
<li><a class="reference internal" href="#jinja2-globals">jinja2.globals</a></li>
<li><a class="reference internal" href="#jinja2-tests">jinja2.tests</a></li>
<li><a class="reference internal" href="#jinja2-bytecode-caching">jinja2.bytecode_caching</a></li>
<li><a class="reference internal" href="#jinja2-bytecode-caching-directory">jinja2.bytecode_caching_directory</a></li>
<li><a class="reference internal" href="#jinja2-newstyle">jinja2.newstyle</a></li>
<li><a class="reference internal" href="#jinja2-finalize">jinja2.finalize</a></li>
</ul>
</li>
<li><a class="reference internal" href="#id4">Jinja2 Filters</a></li>
<li><a class="reference internal" href="#creating-a-jinja2-pyramid-project">Creating a Jinja2 <code class="docutils literal"><span class="pre">Pyramid</span></code> Project</a><ul>
<li><a class="reference internal" href="#paster-template-i18n">Paster Template I18N</a></li>
</ul>
</li>
<li><a class="reference internal" href="#more-information">More Information</a></li>
<li><a class="reference internal" href="#reporting-bugs-development-versions">Reporting Bugs / Development Versions</a><ul>
<li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Next topic</h4>
<p class="topless"><a href="api.html"
title="next chapter"><code class="docutils literal"><span class="pre">pyramid_jinja2</span></code> API</a></p>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="api.html" title="pyramid_jinja2 API"
>next</a> |</li>
<li><a href="#">pyramid_jinja2 2.4 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2011, Agendaless Consulting <chrism@plope.com>.
Last updated on Mar 27, 2015.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
</div>
</body>
</html>
|