/usr/share/doc/python-kajiki-doc/html/templating-basics.html is in python-kajiki-doc 0.5.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <!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>Kajiki Templating Basics — Kajiki 0.5.3 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.5.3',
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="shortcut icon" href="_static/favicon.ico"/>
<link rel="top" title="Kajiki 0.5.3 documentation" href="index.html" />
<link rel="next" title="Kajiki XML Templates" href="xml-templates.html" />
<link rel="prev" title="Welcome to Kajiki’s documentation!" href="index.html" />
</head>
<body role="document">
<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="xml-templates.html" title="Kajiki XML Templates"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="index.html" title="Welcome to Kajiki’s documentation!"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Kajiki 0.5.3 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="kajiki-templating-basics">
<h1>Kajiki Templating Basics<a class="headerlink" href="#kajiki-templating-basics" title="Permalink to this headline">¶</a></h1>
<p>Kajiki provides two templating engines, one which is useful for generating
markup (HTML or XML most likely), and one of which is useful for generating
plain text. This document describes the aspects of the two engines that are
similar and the basic API for using them.</p>
<div class="section" id="synopsis">
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Permalink to this headline">¶</a></h2>
<p>A Kajiki <em>xml template</em> is a well-formed XML document that may include one or
more custom tags and attributes prefixed by the namespace ‘py:’. XML templates
also may contain python expressions that will be evaluated at template expansion
time as well as processing instructions that may contain Python code. XML
templates should be used when generating XML or HTML, as they provide awareness
of well-formedness rules and proper escaping.</p>
<p>The following is an example of a simple Kajki markup template:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="cp"><?python</span>
<span class="cp"> title = "A Kajiki Template"</span>
<span class="cp"> fruits = ["apple", "orange", "kiwi"]</span>
<span class="cp">?></span>
<span class="nt"><html></span>
<span class="nt"><head></span>
<span class="nt"><title</span> <span class="na">py:content=</span><span class="s">"title"</span><span class="nt">></span>This is replaced.<span class="nt"></title></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><p></span>These are some of my favorite fruits:<span class="nt"></p></span>
<span class="nt"><ul></span>
<span class="nt"><li</span> <span class="na">py:for=</span><span class="s">"fruit in fruits"</span><span class="nt">></span>
I like ${fruit}s
<span class="nt"></li></span>
<span class="nt"></ul></span>
<span class="nt"></body></span>
<span class="nt"></html></span>
</pre></div>
</div>
<p>This template would generate output similar to this (in X(H)ML mode):</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt"><html></span>
<span class="nt"><head></span>
<span class="nt"><title></span>A Kajiki Template<span class="nt"></title></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><p></span>These are some of my favorite fruits:<span class="nt"></p></span>
<span class="nt"><ul></span>
<span class="nt"><li></span>I like apples<span class="nt"></li></span>
<span class="nt"><li></span>I like oranges<span class="nt"></li></span>
<span class="nt"><li></span>I like kiwis<span class="nt"></li></span>
<span class="nt"></ul></span>
<span class="nt"></body></span>
<span class="nt"></html></span>
</pre></div>
</div>
<p>or this (in HTML mode):</p>
<div class="highlight-html"><div class="highlight"><pre><span class="p"><</span><span class="nt">html</span><span class="p">></span>
<span class="p"><</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">title</span><span class="p">></span>A Kajiki Template<span class="p"></</span><span class="nt">title</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">p</span><span class="p">></span>These are some of my favorite fruits:
<span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span>I like apples
<span class="p"><</span><span class="nt">li</span><span class="p">></span>I like oranges
<span class="p"><</span><span class="nt">li</span><span class="p">></span>I like kiwis
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<dl class="docutils">
<dt><em>Text templates</em>, on the other hand, are plain text documents that can contain</dt>
<dd>embedded Python directives and expressions. Text templates should be used when
generating non-markup text format such as email. Here is a simple text template:</dd>
</dl>
<div class="highlight-none"><div class="highlight"><pre>Dear $name,
These are some of my favorite fruits:
%for fruit in fruts
* $fruit
%end
</pre></div>
</div>
<p>This would generate something similar to the following:</p>
<div class="highlight-none"><div class="highlight"><pre>Dear Rick,
These are some of my favorite fruits:
* Apples
* Bananas
* Pears
</pre></div>
</div>
</div>
<div class="section" id="python-api">
<h2>Python API<a class="headerlink" href="#python-api" title="Permalink to this headline">¶</a></h2>
<p>In order to actually use Kajiki in generating text (either via the XML or the
text-based languages), the pattern is as follows:</p>
<blockquote>
<div><ol class="arabic simple">
<li>Obtain an XMLTemplate or TextTemplate subclass containing the template source. This can either be done directly or via a template loader.</li>
<li>Instantiate the template with one constructor argument, a dict containing all the values that should be made available as global variables to the template.</li>
<li>Render the template instance using its render() method (for rendering to a single string) or iterating through it (for “stream”) rendering.</li>
</ol>
</div></blockquote>
<p>For instance:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Template</span> <span class="o">=</span> <span class="n">kajiki</span><span class="o">.</span><span class="n">XMLTemplate</span><span class="p">(</span><span class="s1">'<h1>Hello, $name!</h1>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'world'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="go">'<h1>Hello, world!</h1>'</span>
</pre></div>
</div>
<p>Using text templates is similar:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Template</span> <span class="o">=</span> <span class="n">kajiki</span><span class="o">.</span><span class="n">TextTemplate</span><span class="p">(</span><span class="s1">'Hello, $name!'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'world'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="go">'Hello, world!'</span>
</pre></div>
</div>
<p>You can also use a template loader to indirectly generate the template classes.
Using a template loader gives two main advantages over directly instantiating
templates:</p>
<blockquote>
<div><ul class="simple">
<li>Compiled templates are cached and only re-parsed when the template changes.</li>
<li>Several template tags such as <cite>extends</cite>, <cite>import</cite>, and <cite>include</cite> that require knowlege of other templates become enabled.</li>
</ul>
</div></blockquote>
<p>Using a template loader would look similar to the following:</p>
<div class="highlight-python"><div class="highlight"><pre>loader = PackageLoader()
Template = loader.import_('my.package.text.template')
t = Template(dict(title='Hello, world!')
print t.render()
</pre></div>
</div>
</div>
<div class="section" id="template-expressions-and-code-blocks">
<h2>Template Expressions and Code Blocks<a class="headerlink" href="#template-expressions-and-code-blocks" title="Permalink to this headline">¶</a></h2>
<p>Python expressions can be used in “plain text” areas of templates, including, in
XML templates, tag attributes. They are also used in some directive arguments.
Whenever a Python expression is used in a “plain text” area, it must be prefixed
by a dollar sign ($) and possibly enclosed in curly braces. If the expression
starts with a letter and contains only letters, digits, dots, and underscores,
then the curly braces may be omitted. In all other cases, they are required.
For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Template</span> <span class="o">=</span> <span class="n">kajiki</span><span class="o">.</span><span class="n">XMLTemplate</span><span class="p">(</span><span class="s1">'<em>${items[0].capitalize()}</em>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">Template</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">items</span><span class="o">=</span><span class="p">[</span><span class="s1">'first'</span><span class="p">,</span> <span class="s1">'second'</span><span class="p">]))</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="go">'<em>First</em>'</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">>>> </span><span class="n">Template</span> <span class="o">=</span> <span class="n">kajiki</span><span class="o">.</span><span class="n">TextTemplate</span><span class="p">(</span><span class="s1">'Maxint is $sys.maxsize'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">Template</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">sys</span><span class="o">=</span><span class="n">sys</span><span class="p">))</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="go">'Maxint is 9223372036854775807'</span>
</pre></div>
</div>
<div class="section" id="escaping">
<h3>Escaping<a class="headerlink" href="#escaping" title="Permalink to this headline">¶</a></h3>
<p>If you need a literal dollar sign where Kajiki would normally detect an
expression, you can simply double the dollar sign:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Template</span> <span class="o">=</span> <span class="n">kajiki</span><span class="o">.</span><span class="n">XMLTemplate</span><span class="p">(</span><span class="s1">'<em>$foo</em>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">Template</span><span class="p">()</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">NameError</span>: <span class="n">global name 'foo' is not defined</span>
<span class="gp">>>> </span><span class="n">Template</span> <span class="o">=</span> <span class="n">kajiki</span><span class="o">.</span><span class="n">XMLTemplate</span><span class="p">(</span><span class="s1">'<em>$$foo</em>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">Template</span><span class="p">()</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="go">'<em>$foo</em>'</span>
</pre></div>
</div>
</div>
<div class="section" id="code-blocks">
<h3>Code Blocks<a class="headerlink" href="#code-blocks" title="Permalink to this headline">¶</a></h3>
<p>Templates also support full Python syntax, using the <?py ?> processing
instruction:</p>
<div class="highlight-xml"><div class="highlight"><pre><div>
<?py import sys>
Maxint is $sys.maxint
</div>
</pre></div>
</div>
<p>This will produce the following output:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt"><div></span>
Maxint is 9223372036854775807
<span class="nt"></div></span>
</pre></div>
</div>
<p>In text blocks, the %py (or {%py%} directive accomplishes the same goal:</p>
<div class="highlight-none"><div class="highlight"><pre>%py import sys
Maxint is $sys.maxint
</pre></div>
</div>
<p>This will produce:</p>
<div class="highlight-none"><div class="highlight"><pre>Maxint is 9223372036854775807
</pre></div>
</div>
<p>In both of the above cases, the Python code runs in the ‘local scope’ of the
template’s main rendering function, so any variables defined there will not be
accessible in functions or blocks defined elsewhere in the template. To force
the python block to run at ‘module-level’ in XML templates, simply prefix the
first line of the Python with a percent (%) sign:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Template</span> <span class="o">=</span> <span class="n">kajiki</span><span class="o">.</span><span class="n">XMLTemplate</span><span class="p">(</span><span class="s1">'''<div</span>
<span class="gp">... </span><span class="s1">><?py </span><span class="si">%i</span><span class="s1">mport os</span>
<span class="gp">... </span><span class="s1">?><py:def function="test()"</span>
<span class="gp">... </span><span class="s1">>${os.path.join('a', 'b', 'c')}</py:def</span>
<span class="gp">... </span><span class="s1">>${test()}</div>'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">Template</span><span class="p">()</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="go">'<div>a/b/c</div>'</span>
</pre></div>
</div>
<p>In text templates, replace the %py directive with %py%:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Template</span> <span class="o">=</span> <span class="n">kajiki</span><span class="o">.</span><span class="n">TextTemplate</span><span class="p">(</span><span class="s1">'''%py</span><span class="si">% i</span><span class="s1">mport os</span>
<span class="gp">... </span><span class="si">%d</span><span class="s1">ef test()</span>
<span class="gp">... </span><span class="s1">${os.path.join('a','b','c')}</span><span class="se">\\</span><span class="s1"></span>
<span class="gp">... </span><span class="si">%e</span><span class="s1">nd</span>
<span class="gp">... </span><span class="s1">${test()}'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">Template</span><span class="p">()</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="go">'a/b/c'</span>
</pre></div>
</div>
</div>
<div class="section" id="built-in-functions-and-variables">
<h3>Built-in Functions and Variables<a class="headerlink" href="#built-in-functions-and-variables" title="Permalink to this headline">¶</a></h3>
<p>All templates have access to the following functions and variables:</p>
<dl class="function">
<dt id="literal">
<code class="descname">literal</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#literal" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrap some user-generated text so that it doesn’t get escaped along with
everything else.</p>
</dd></dl>
<dl class="data">
<dt id="local">
<code class="descname">local</code><a class="headerlink" href="#local" title="Permalink to this definition">¶</a></dt>
<dd><p>The current template being defined</p>
</dd></dl>
<dl class="data">
<dt id="self">
<code class="descname">self</code><a class="headerlink" href="#self" title="Permalink to this definition">¶</a></dt>
<dd><p>The current template being defined, or, if used in the context of a parent
template that is being extended, the final (“child-most”) template in the
inheritance hierarchy.</p>
</dd></dl>
<dl class="data">
<dt id="parent">
<code class="descname">parent</code><a class="headerlink" href="#parent" title="Permalink to this definition">¶</a></dt>
<dd><p>The parent template (via py:extends) of the template being defined</p>
</dd></dl>
<dl class="data">
<dt id="child">
<code class="descname">child</code><a class="headerlink" href="#child" title="Permalink to this definition">¶</a></dt>
<dd><p>The child template (via py:extends) of the template being defined</p>
</dd></dl>
</div>
</div>
<div class="section" id="template-directives">
<h2>Template Directives<a class="headerlink" href="#template-directives" title="Permalink to this headline">¶</a></h2>
<p>Template directives provide control flow and inheritance functionality for
templates. As their syntax depends on whether you’re using XML or text
templates, please refer to <a class="reference internal" href="xml-templates.html"><em>Kajiki XML Templates</em></a> or <a class="reference internal" href="text-templates.html"><em>Kajiki Text Templates</em></a>
for more information.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Kajiki Templating Basics</a><ul>
<li><a class="reference internal" href="#synopsis">Synopsis</a></li>
<li><a class="reference internal" href="#python-api">Python API</a></li>
<li><a class="reference internal" href="#template-expressions-and-code-blocks">Template Expressions and Code Blocks</a><ul>
<li><a class="reference internal" href="#escaping">Escaping</a></li>
<li><a class="reference internal" href="#code-blocks">Code Blocks</a></li>
<li><a class="reference internal" href="#built-in-functions-and-variables">Built-in Functions and Variables</a></li>
</ul>
</li>
<li><a class="reference internal" href="#template-directives">Template Directives</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="index.html"
title="previous chapter">Welcome to Kajiki’s documentation!</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="xml-templates.html"
title="next chapter">Kajiki XML Templates</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/templating-basics.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<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="xml-templates.html" title="Kajiki XML Templates"
>next</a> |</li>
<li class="right" >
<a href="index.html" title="Welcome to Kajiki’s documentation!"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Kajiki 0.5.3 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2010-2013, Rick Copeland.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.5.
</div>
</body>
</html>
|