usage.html 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  3. <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
  4. <head>
  5. <title>
  6. Usage
  7. &mdash;
  8. Mako 0.8.1 Documentation
  9. </title>
  10. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  11. <link rel="stylesheet" href="_static/docs.css" type="text/css" />
  12. <script type="text/javascript">
  13. var DOCUMENTATION_OPTIONS = {
  14. URL_ROOT: '#',
  15. VERSION: '0.8.1',
  16. COLLAPSE_MODINDEX: false,
  17. FILE_SUFFIX: '.html'
  18. };
  19. </script>
  20. <script type="text/javascript" src="_static/jquery.js"></script>
  21. <script type="text/javascript" src="_static/underscore.js"></script>
  22. <script type="text/javascript" src="_static/doctools.js"></script>
  23. <link rel="index" title="Index" href="genindex.html" />
  24. <link rel="search" title="Search" href="search.html" />
  25. <link rel="top" title="Mako 0.8.1 Documentation" href="index.html" />
  26. <link rel="next" title="Syntax" href="syntax.html" />
  27. <link rel="prev" title="Table of Contents" href="index.html" />
  28. <link rel="stylesheet" href="_static/site.css"></link>
  29. </head>
  30. <body>
  31. <div id="wrap">
  32. <div class="rightbar">
  33. <div class="slogan">
  34. Hyperfast and lightweight templating for the Python platform.
  35. </div>
  36. </div>
  37. <a href="http://www.makotemplates.org/"><img src="_static/makoLogo.png" /></a>
  38. <hr/>
  39. <div id="docs-container">
  40. <div id="docs-header">
  41. <h1>Mako 0.8.1 Documentation</h1>
  42. <div id="docs-search">
  43. Search:
  44. <form class="search" action="search.html" method="get">
  45. <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
  46. <input type="hidden" name="check_keywords" value="yes" />
  47. <input type="hidden" name="area" value="default" />
  48. </form>
  49. </div>
  50. <div id="docs-version-header">
  51. Release: <span class="version-num">0.8.1</span>
  52. </div>
  53. </div>
  54. <div id="docs-top-navigation">
  55. <div id="docs-top-page-control" class="docs-navigation-links">
  56. <ul>
  57. <li>Prev:
  58. <a href="index.html" title="previous chapter">Table of Contents</a>
  59. </li>
  60. <li>Next:
  61. <a href="syntax.html" title="next chapter">Syntax</a>
  62. </li>
  63. <li>
  64. <a href="index.html">Table of Contents</a> |
  65. <a href="genindex.html">Index</a>
  66. | <a href="_sources/usage.txt">view source
  67. </li>
  68. </ul>
  69. </div>
  70. <div id="docs-navigation-banner">
  71. <a href="index.html">Mako 0.8.1 Documentation</a>
  72. »
  73. Usage
  74. <h2>
  75. Usage
  76. </h2>
  77. </div>
  78. </div>
  79. <div id="docs-body-container">
  80. <div id="docs-sidebar">
  81. <h3><a href="index.html">Table of Contents</a></h3>
  82. <ul>
  83. <li><a class="reference internal" href="#">Usage</a><ul>
  84. <li><a class="reference internal" href="#basic-usage">Basic Usage</a></li>
  85. <li><a class="reference internal" href="#using-file-based-templates">Using File-Based Templates</a></li>
  86. <li><a class="reference internal" href="#using-templatelookup">Using <tt class="docutils literal"><span class="pre">TemplateLookup</span></tt></a><ul>
  87. <li><a class="reference internal" href="#setting-the-collection-size">Setting the Collection Size</a></li>
  88. <li><a class="reference internal" href="#setting-filesystem-checks">Setting Filesystem Checks</a></li>
  89. </ul>
  90. </li>
  91. <li><a class="reference internal" href="#using-unicode-and-encoding">Using Unicode and Encoding</a></li>
  92. <li><a class="reference internal" href="#handling-exceptions">Handling Exceptions</a></li>
  93. <li><a class="reference internal" href="#common-framework-integrations">Common Framework Integrations</a><ul>
  94. <li><a class="reference internal" href="#wsgi">WSGI</a></li>
  95. <li><a class="reference internal" href="#pygments">Pygments</a></li>
  96. <li><a class="reference internal" href="#babel">Babel</a></li>
  97. </ul>
  98. </li>
  99. <li><a class="reference internal" href="#api-reference">API Reference</a></li>
  100. </ul>
  101. </li>
  102. </ul>
  103. <h4>Previous Topic</h4>
  104. <p>
  105. <a href="index.html" title="previous chapter">Table of Contents</a>
  106. </p>
  107. <h4>Next Topic</h4>
  108. <p>
  109. <a href="syntax.html" title="next chapter">Syntax</a>
  110. </p>
  111. <h4>Quick Search</h4>
  112. <p>
  113. <form class="search" action="search.html" method="get">
  114. <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
  115. <input type="hidden" name="check_keywords" value="yes" />
  116. <input type="hidden" name="area" value="default" />
  117. </form>
  118. </p>
  119. </div>
  120. <div id="docs-body" class="withsidebar" >
  121. <div class="section" id="usage">
  122. <span id="usage-toplevel"></span><h1>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h1>
  123. <div class="section" id="basic-usage">
  124. <h2>Basic Usage<a class="headerlink" href="#basic-usage" title="Permalink to this headline">¶</a></h2>
  125. <p>This section describes the Python API for Mako templates. If you
  126. are using Mako within a web framework such as Pylons, the work
  127. of integrating Mako&#8217;s API is already done for you, in which case
  128. you can skip to the next section, <a class="reference internal" href="syntax.html"><em>Syntax</em></a>.</p>
  129. <p>The most basic way to create a template and render it is through
  130. the <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> class:</p>
  131. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  132. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;hello world!&quot;</span><span class="p">)</span>
  133. <span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  134. </pre></div>
  135. </div>
  136. <p>Above, the text argument to <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> is <strong>compiled</strong> into a
  137. Python module representation. This module contains a function
  138. called <tt class="docutils literal"><span class="pre">render_body()</span></tt>, which produces the output of the
  139. template. When <tt class="docutils literal"><span class="pre">mytemplate.render()</span></tt> is called, Mako sets up a
  140. runtime environment for the template and calls the
  141. <tt class="docutils literal"><span class="pre">render_body()</span></tt> function, capturing the output into a buffer and
  142. returning its string contents.</p>
  143. <p>The code inside the <tt class="docutils literal"><span class="pre">render_body()</span></tt> function has access to a
  144. namespace of variables. You can specify these variables by
  145. sending them as additional keyword arguments to the <a class="reference internal" href="#mako.template.Template.render" title="mako.template.Template.render"><tt class="xref py py-meth docutils literal"><span class="pre">render()</span></tt></a>
  146. method:</p>
  147. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  148. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;hello, ${name}!&quot;</span><span class="p">)</span>
  149. <span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;jack&quot;</span><span class="p">)</span>
  150. </pre></div>
  151. </div>
  152. <p>The <a class="reference internal" href="#mako.template.Template.render" title="mako.template.Template.render"><tt class="xref py py-meth docutils literal"><span class="pre">render()</span></tt></a> method calls upon Mako to create a
  153. <a class="reference internal" href="runtime.html#mako.runtime.Context" title="mako.runtime.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> object, which stores all the variable names accessible
  154. to the template and also stores a buffer used to capture output.
  155. You can create this <a class="reference internal" href="runtime.html#mako.runtime.Context" title="mako.runtime.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> yourself and have the template
  156. render with it, using the <a class="reference internal" href="#mako.template.Template.render_context" title="mako.template.Template.render_context"><tt class="xref py py-meth docutils literal"><span class="pre">render_context()</span></tt></a> method:</p>
  157. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  158. <span class="kn">from</span> <span class="nn">mako.runtime</span> <span class="kn">import</span> <span class="n">Context</span>
  159. <span class="kn">from</span> <span class="nn">StringIO</span> <span class="kn">import</span> <span class="n">StringIO</span>
  160. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;hello, ${name}!&quot;</span><span class="p">)</span>
  161. <span class="n">buf</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
  162. <span class="n">ctx</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;jack&quot;</span><span class="p">)</span>
  163. <span class="n">mytemplate</span><span class="o">.</span><span class="n">render_context</span><span class="p">(</span><span class="n">ctx</span><span class="p">)</span>
  164. <span class="k">print</span> <span class="n">buf</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
  165. </pre></div>
  166. </div>
  167. </div>
  168. <div class="section" id="using-file-based-templates">
  169. <h2>Using File-Based Templates<a class="headerlink" href="#using-file-based-templates" title="Permalink to this headline">¶</a></h2>
  170. <p>A <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> can also load its template source code from a file,
  171. using the <tt class="docutils literal"><span class="pre">filename</span></tt> keyword argument:</p>
  172. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  173. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s">&#39;/docs/mytmpl.txt&#39;</span><span class="p">)</span>
  174. <span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  175. </pre></div>
  176. </div>
  177. <p>For improved performance, a <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> which is loaded from a
  178. file can also cache the source code to its generated module on
  179. the filesystem as a regular Python module file (i.e. a <tt class="docutils literal"><span class="pre">.py</span></tt>
  180. file). To do this, just add the <tt class="docutils literal"><span class="pre">module_directory</span></tt> argument to
  181. the template:</p>
  182. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  183. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s">&#39;/docs/mytmpl.txt&#39;</span><span class="p">,</span> <span class="n">module_directory</span><span class="o">=</span><span class="s">&#39;/tmp/mako_modules&#39;</span><span class="p">)</span>
  184. <span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  185. </pre></div>
  186. </div>
  187. <p>When the above code is rendered, a file
  188. <tt class="docutils literal"><span class="pre">/tmp/mako_modules/docs/mytmpl.txt.py</span></tt> is created containing the
  189. source code for the module. The next time a <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> with the
  190. same arguments is created, this module file will be
  191. automatically re-used.</p>
  192. </div>
  193. <div class="section" id="using-templatelookup">
  194. <span id="usage-templatelookup"></span><h2>Using <tt class="docutils literal"><span class="pre">TemplateLookup</span></tt><a class="headerlink" href="#using-templatelookup" title="Permalink to this headline">¶</a></h2>
  195. <p>All of the examples thus far have dealt with the usage of a
  196. single <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object. If the code within those templates
  197. tries to locate another template resource, it will need some way
  198. to find them, using simple URI strings. For this need, the
  199. resolution of other templates from within a template is
  200. accomplished by the <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> class. This class is
  201. constructed given a list of directories in which to search for
  202. templates, as well as keyword arguments that will be passed to
  203. the <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> objects it creates:</p>
  204. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  205. <span class="kn">from</span> <span class="nn">mako.lookup</span> <span class="kn">import</span> <span class="n">TemplateLookup</span>
  206. <span class="n">mylookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;/docs&#39;</span><span class="p">])</span>
  207. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;&quot;&quot;&lt;</span><span class="si">%i</span><span class="s">nclude file=&quot;header.txt&quot;/&gt; hello world!&quot;&quot;&quot;</span><span class="p">,</span> <span class="n">lookup</span><span class="o">=</span><span class="n">mylookup</span><span class="p">)</span>
  208. </pre></div>
  209. </div>
  210. <p>Above, we created a textual template which includes the file
  211. <tt class="docutils literal"><span class="pre">&quot;header.txt&quot;</span></tt>. In order for it to have somewhere to look for
  212. <tt class="docutils literal"><span class="pre">&quot;header.txt&quot;</span></tt>, we passed a <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> object to it, which
  213. will search in the directory <tt class="docutils literal"><span class="pre">/docs</span></tt> for the file <tt class="docutils literal"><span class="pre">&quot;header.txt&quot;</span></tt>.</p>
  214. <p>Usually, an application will store most or all of its templates
  215. as text files on the filesystem. So far, all of our examples
  216. have been a little bit contrived in order to illustrate the
  217. basic concepts. But a real application would get most or all of
  218. its templates directly from the <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>, using the
  219. aptly named <a class="reference internal" href="#mako.lookup.TemplateLookup.get_template" title="mako.lookup.TemplateLookup.get_template"><tt class="xref py py-meth docutils literal"><span class="pre">get_template()</span></tt></a> method, which accepts the URI of the
  220. desired template:</p>
  221. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  222. <span class="kn">from</span> <span class="nn">mako.lookup</span> <span class="kn">import</span> <span class="n">TemplateLookup</span>
  223. <span class="n">mylookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;/docs&#39;</span><span class="p">],</span> <span class="n">module_directory</span><span class="o">=</span><span class="s">&#39;/tmp/mako_modules&#39;</span><span class="p">)</span>
  224. <span class="k">def</span> <span class="nf">serve_template</span><span class="p">(</span><span class="n">templatename</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
  225. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">mylookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">templatename</span><span class="p">)</span>
  226. <span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
  227. </pre></div>
  228. </div>
  229. <p>In the example above, we create a <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> which will
  230. look for templates in the <tt class="docutils literal"><span class="pre">/docs</span></tt> directory, and will store
  231. generated module files in the <tt class="docutils literal"><span class="pre">/tmp/mako_modules</span></tt> directory. The
  232. lookup locates templates by appending the given URI to each of
  233. its search directories; so if you gave it a URI of
  234. <tt class="docutils literal"><span class="pre">/etc/beans/info.txt</span></tt>, it would search for the file
  235. <tt class="docutils literal"><span class="pre">/docs/etc/beans/info.txt</span></tt>, else raise a <tt class="xref py py-class docutils literal"><span class="pre">TopLevelNotFound</span></tt>
  236. exception, which is a custom Mako exception.</p>
  237. <p>When the lookup locates templates, it will also assign a <tt class="docutils literal"><span class="pre">uri</span></tt>
  238. property to the <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> which is the URI passed to the
  239. <a class="reference internal" href="#mako.lookup.TemplateLookup.get_template" title="mako.lookup.TemplateLookup.get_template"><tt class="xref py py-meth docutils literal"><span class="pre">get_template()</span></tt></a> call. <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> uses this URI to calculate the
  240. name of its module file. So in the above example, a
  241. <tt class="docutils literal"><span class="pre">templatename</span></tt> argument of <tt class="docutils literal"><span class="pre">/etc/beans/info.txt</span></tt> will create a
  242. module file <tt class="docutils literal"><span class="pre">/tmp/mako_modules/etc/beans/info.txt.py</span></tt>.</p>
  243. <div class="section" id="setting-the-collection-size">
  244. <h3>Setting the Collection Size<a class="headerlink" href="#setting-the-collection-size" title="Permalink to this headline">¶</a></h3>
  245. <p>The <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> also serves the important need of caching a
  246. fixed set of templates in memory at a given time, so that
  247. successive URI lookups do not result in full template
  248. compilations and/or module reloads on each request. By default,
  249. the <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> size is unbounded. You can specify a fixed
  250. size using the <tt class="docutils literal"><span class="pre">collection_size</span></tt> argument:</p>
  251. <div class="highlight-python"><div class="highlight"><pre><span class="n">mylookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;/docs&#39;</span><span class="p">],</span>
  252. <span class="n">module_directory</span><span class="o">=</span><span class="s">&#39;/tmp/mako_modules&#39;</span><span class="p">,</span> <span class="n">collection_size</span><span class="o">=</span><span class="mi">500</span><span class="p">)</span>
  253. </pre></div>
  254. </div>
  255. <p>The above lookup will continue to load templates into memory
  256. until it reaches a count of around 500. At that point, it will
  257. clean out a certain percentage of templates using a least
  258. recently used scheme.</p>
  259. </div>
  260. <div class="section" id="setting-filesystem-checks">
  261. <h3>Setting Filesystem Checks<a class="headerlink" href="#setting-filesystem-checks" title="Permalink to this headline">¶</a></h3>
  262. <p>Another important flag on <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> is
  263. <tt class="docutils literal"><span class="pre">filesystem_checks</span></tt>. This defaults to <tt class="docutils literal"><span class="pre">True</span></tt>, and says that each
  264. time a template is returned by the <a class="reference internal" href="#mako.lookup.TemplateLookup.get_template" title="mako.lookup.TemplateLookup.get_template"><tt class="xref py py-meth docutils literal"><span class="pre">get_template()</span></tt></a> method, the
  265. revision time of the original template file is checked against
  266. the last time the template was loaded, and if the file is newer
  267. will reload its contents and recompile the template. On a
  268. production system, setting <tt class="docutils literal"><span class="pre">filesystem_checks</span></tt> to <tt class="docutils literal"><span class="pre">False</span></tt> can
  269. afford a small to moderate performance increase (depending on
  270. the type of filesystem used).</p>
  271. </div>
  272. </div>
  273. <div class="section" id="using-unicode-and-encoding">
  274. <span id="usage-unicode"></span><h2>Using Unicode and Encoding<a class="headerlink" href="#using-unicode-and-encoding" title="Permalink to this headline">¶</a></h2>
  275. <p>Both <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> and <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> accept <tt class="docutils literal"><span class="pre">output_encoding</span></tt>
  276. and <tt class="docutils literal"><span class="pre">encoding_errors</span></tt> parameters which can be used to encode the
  277. output in any Python supported codec:</p>
  278. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  279. <span class="kn">from</span> <span class="nn">mako.lookup</span> <span class="kn">import</span> <span class="n">TemplateLookup</span>
  280. <span class="n">mylookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;/docs&#39;</span><span class="p">],</span> <span class="n">output_encoding</span><span class="o">=</span><span class="s">&#39;utf-8&#39;</span><span class="p">,</span> <span class="n">encoding_errors</span><span class="o">=</span><span class="s">&#39;replace&#39;</span><span class="p">)</span>
  281. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">mylookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">&quot;foo.txt&quot;</span><span class="p">)</span>
  282. <span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  283. </pre></div>
  284. </div>
  285. <p>When using Python 3, the <a class="reference internal" href="#mako.template.Template.render" title="mako.template.Template.render"><tt class="xref py py-meth docutils literal"><span class="pre">render()</span></tt></a> method will return a <tt class="docutils literal"><span class="pre">bytes</span></tt>
  286. object, <strong>if</strong> <tt class="docutils literal"><span class="pre">output_encoding</span></tt> is set. Otherwise it returns a
  287. <tt class="docutils literal"><span class="pre">string</span></tt>.</p>
  288. <p>Additionally, the <a class="reference internal" href="#mako.template.Template.render_unicode" title="mako.template.Template.render_unicode"><tt class="xref py py-meth docutils literal"><span class="pre">render_unicode()</span></tt></a> method exists which will
  289. return the template output as a Python <tt class="docutils literal"><span class="pre">unicode</span></tt> object, or in
  290. Python 3 a <tt class="docutils literal"><span class="pre">string</span></tt>:</p>
  291. <div class="highlight-python"><div class="highlight"><pre><span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render_unicode</span><span class="p">()</span>
  292. </pre></div>
  293. </div>
  294. <p>The above method disregards the output encoding keyword
  295. argument; you can encode yourself by saying:</p>
  296. <div class="highlight-python"><div class="highlight"><pre><span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render_unicode</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">,</span> <span class="s">&#39;replace&#39;</span><span class="p">)</span>
  297. </pre></div>
  298. </div>
  299. <p>Note that Mako&#8217;s ability to return data in any encoding and/or
  300. <tt class="docutils literal"><span class="pre">unicode</span></tt> implies that the underlying output stream of the
  301. template is a Python unicode object. This behavior is described
  302. fully in <a class="reference internal" href="unicode.html"><em>The Unicode Chapter</em></a>.</p>
  303. </div>
  304. <div class="section" id="handling-exceptions">
  305. <span id="id1"></span><h2>Handling Exceptions<a class="headerlink" href="#handling-exceptions" title="Permalink to this headline">¶</a></h2>
  306. <p>Template exceptions can occur in two distinct places. One is
  307. when you <strong>lookup, parse and compile</strong> the template, the other
  308. is when you <strong>run</strong> the template. Within the running of a
  309. template, exceptions are thrown normally from whatever Python
  310. code originated the issue. Mako has its own set of exception
  311. classes which mostly apply to the lookup and lexer/compiler
  312. stages of template construction. Mako provides some library
  313. routines that can be used to help provide Mako-specific
  314. information about any exception&#8217;s stack trace, as well as
  315. formatting the exception within textual or HTML format. In all
  316. cases, the main value of these handlers is that of converting
  317. Python filenames, line numbers, and code samples into Mako
  318. template filenames, line numbers, and code samples. All lines
  319. within a stack trace which correspond to a Mako template module
  320. will be converted to be against the originating template file.</p>
  321. <p>To format exception traces, the <a class="reference internal" href="#mako.exceptions.text_error_template" title="mako.exceptions.text_error_template"><tt class="xref py py-func docutils literal"><span class="pre">text_error_template()</span></tt></a> and
  322. <a class="reference internal" href="#mako.exceptions.html_error_template" title="mako.exceptions.html_error_template"><tt class="xref py py-func docutils literal"><span class="pre">html_error_template()</span></tt></a> functions are provided. They make usage of
  323. <tt class="docutils literal"><span class="pre">sys.exc_info()</span></tt> to get at the most recently thrown exception.
  324. Usage of these handlers usually looks like:</p>
  325. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako</span> <span class="kn">import</span> <span class="n">exceptions</span>
  326. <span class="k">try</span><span class="p">:</span>
  327. <span class="n">template</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
  328. <span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  329. <span class="k">except</span><span class="p">:</span>
  330. <span class="k">print</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">text_error_template</span><span class="p">()</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  331. </pre></div>
  332. </div>
  333. <p>Or for the HTML render function:</p>
  334. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako</span> <span class="kn">import</span> <span class="n">exceptions</span>
  335. <span class="k">try</span><span class="p">:</span>
  336. <span class="n">template</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
  337. <span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  338. <span class="k">except</span><span class="p">:</span>
  339. <span class="k">print</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">html_error_template</span><span class="p">()</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  340. </pre></div>
  341. </div>
  342. <p>The <a class="reference internal" href="#mako.exceptions.html_error_template" title="mako.exceptions.html_error_template"><tt class="xref py py-func docutils literal"><span class="pre">html_error_template()</span></tt></a> template accepts two options:
  343. specifying <tt class="docutils literal"><span class="pre">full=False</span></tt> causes only a section of an HTML
  344. document to be rendered. Specifying <tt class="docutils literal"><span class="pre">css=False</span></tt> will disable the
  345. default stylesheet from being rendered.</p>
  346. <p>E.g.:</p>
  347. <div class="highlight-python"><div class="highlight"><pre><span class="k">print</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">html_error_template</span><span class="p">()</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">full</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
  348. </pre></div>
  349. </div>
  350. <p>The HTML render function is also available built-in to
  351. <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> using the <tt class="docutils literal"><span class="pre">format_exceptions</span></tt> flag. In this case, any
  352. exceptions raised within the <strong>render</strong> stage of the template
  353. will result in the output being substituted with the output of
  354. <a class="reference internal" href="#mako.exceptions.html_error_template" title="mako.exceptions.html_error_template"><tt class="xref py py-func docutils literal"><span class="pre">html_error_template()</span></tt></a>:</p>
  355. <div class="highlight-python"><div class="highlight"><pre><span class="n">template</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s">&quot;/foo/bar&quot;</span><span class="p">,</span> <span class="n">format_exceptions</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  356. <span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  357. </pre></div>
  358. </div>
  359. <p>Note that the compile stage of the above template occurs when
  360. you construct the <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> itself, and no output stream is
  361. defined. Therefore exceptions which occur within the
  362. lookup/parse/compile stage will not be handled and will
  363. propagate normally. While the pre-render traceback usually will
  364. not include any Mako-specific lines anyway, it will mean that
  365. exceptions which occur previous to rendering and those which
  366. occur within rendering will be handled differently... so the
  367. <tt class="docutils literal"><span class="pre">try</span></tt>/<tt class="docutils literal"><span class="pre">except</span></tt> patterns described previously are probably of more
  368. general use.</p>
  369. <p>The underlying object used by the error template functions is
  370. the <a class="reference internal" href="#mako.exceptions.RichTraceback" title="mako.exceptions.RichTraceback"><tt class="xref py py-class docutils literal"><span class="pre">RichTraceback</span></tt></a> object. This object can also be used
  371. directly to provide custom error views. Here&#8217;s an example usage
  372. which describes its general API:</p>
  373. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.exceptions</span> <span class="kn">import</span> <span class="n">RichTraceback</span>
  374. <span class="k">try</span><span class="p">:</span>
  375. <span class="n">template</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
  376. <span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
  377. <span class="k">except</span><span class="p">:</span>
  378. <span class="n">traceback</span> <span class="o">=</span> <span class="n">RichTraceback</span><span class="p">()</span>
  379. <span class="k">for</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">lineno</span><span class="p">,</span> <span class="n">function</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span> <span class="ow">in</span> <span class="n">traceback</span><span class="o">.</span><span class="n">traceback</span><span class="p">:</span>
  380. <span class="k">print</span> <span class="s">&quot;File </span><span class="si">%s</span><span class="s">, line </span><span class="si">%s</span><span class="s">, in </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">lineno</span><span class="p">,</span> <span class="n">function</span><span class="p">)</span>
  381. <span class="k">print</span> <span class="n">line</span><span class="p">,</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span>
  382. <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">),</span> <span class="n">traceback</span><span class="o">.</span><span class="n">error</span><span class="p">)</span>
  383. </pre></div>
  384. </div>
  385. </div>
  386. <div class="section" id="common-framework-integrations">
  387. <h2>Common Framework Integrations<a class="headerlink" href="#common-framework-integrations" title="Permalink to this headline">¶</a></h2>
  388. <p>The Mako distribution includes a little bit of helper code for
  389. the purpose of using Mako in some popular web framework
  390. scenarios. This is a brief description of what&#8217;s included.</p>
  391. <div class="section" id="wsgi">
  392. <h3>WSGI<a class="headerlink" href="#wsgi" title="Permalink to this headline">¶</a></h3>
  393. <p>A sample WSGI application is included in the distribution in the
  394. file <tt class="docutils literal"><span class="pre">examples/wsgi/run_wsgi.py</span></tt>. This runner is set up to pull
  395. files from a <cite>templates</cite> as well as an <cite>htdocs</cite> directory and
  396. includes a rudimental two-file layout. The WSGI runner acts as a
  397. fully functional standalone web server, using <tt class="docutils literal"><span class="pre">wsgiutils</span></tt> to run
  398. itself, and propagates GET and POST arguments from the request
  399. into the <a class="reference internal" href="runtime.html#mako.runtime.Context" title="mako.runtime.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a>, can serve images, CSS files and other kinds
  400. of files, and also displays errors using Mako&#8217;s included
  401. exception-handling utilities.</p>
  402. </div>
  403. <div class="section" id="pygments">
  404. <h3>Pygments<a class="headerlink" href="#pygments" title="Permalink to this headline">¶</a></h3>
  405. <p>A <a class="reference external" href="http://pygments.pocoo.org">Pygments</a>-compatible syntax
  406. highlighting module is included under <tt class="xref py py-mod docutils literal"><span class="pre">mako.ext.pygmentplugin</span></tt>.
  407. This module is used in the generation of Mako documentation and
  408. also contains various <cite>setuptools</cite> entry points under the heading
  409. <tt class="docutils literal"><span class="pre">pygments.lexers</span></tt>, including <tt class="docutils literal"><span class="pre">mako</span></tt>, <tt class="docutils literal"><span class="pre">html+mako</span></tt>, <tt class="docutils literal"><span class="pre">xml+mako</span></tt>
  410. (see the <tt class="docutils literal"><span class="pre">setup.py</span></tt> file for all the entry points).</p>
  411. </div>
  412. <div class="section" id="babel">
  413. <h3>Babel<a class="headerlink" href="#babel" title="Permalink to this headline">¶</a></h3>
  414. <p>Mako provides support for extracting <cite>gettext</cite> messages from
  415. templates via a <a class="reference external" href="http://babel.edgewall.org/">Babel</a> extractor
  416. entry point under <tt class="docutils literal"><span class="pre">mako.ext.babelplugin</span></tt>.</p>
  417. <p><cite>Gettext</cite> messages are extracted from all Python code sections,
  418. including those of control lines and expressions embedded
  419. in tags.</p>
  420. <p><a class="reference external" href="http://babel.edgewall.org/wiki/Documentation/messages.html#comments-tags-and-translator-comments-explanation">Translator
  421. comments</a>
  422. may also be extracted from Mako templates when a comment tag is
  423. specified to <a class="reference external" href="http://babel.edgewall.org/">Babel</a> (such as with
  424. the <tt class="docutils literal"><span class="pre">-c</span></tt> option).</p>
  425. <p>For example, a project <tt class="docutils literal"><span class="pre">&quot;myproj&quot;</span></tt> contains the following Mako
  426. template at <tt class="docutils literal"><span class="pre">myproj/myproj/templates/name.html</span></tt>:</p>
  427. <div class="highlight-mako"><div class="highlight"><pre><span class="x">&lt;div id=&quot;name&quot;&gt;</span>
  428. <span class="x"> Name:</span>
  429. <span class="x"> ## TRANSLATORS: This is a proper name. See the gettext</span>
  430. <span class="x"> ## manual, section Names.</span>
  431. <span class="x"> </span><span class="cp">${</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Francois Pinard&#39;</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  432. <span class="x">&lt;/div&gt;</span>
  433. </pre></div>
  434. </div>
  435. <p>To extract gettext messages from this template the project needs
  436. a Mako section in its <a class="reference external" href="http://babel.edgewall.org/wiki/Documentation/messages.html#extraction-method-mapping-and-configuration">Babel Extraction Method Mapping
  437. file</a>
  438. (typically located at <tt class="docutils literal"><span class="pre">myproj/babel.cfg</span></tt>):</p>
  439. <div class="highlight-cfg"><div class="highlight"><pre><span class="c"># Extraction from Python source files</span>
  440. <span class="k">[python: myproj/**.py]</span>
  441. <span class="c"># Extraction from Mako templates</span>
  442. <span class="k">[mako: myproj/templates/**.html]</span>
  443. <span class="na">input_encoding</span> <span class="o">=</span> <span class="s">utf-8</span>
  444. </pre></div>
  445. </div>
  446. <p>The Mako extractor supports an optional <tt class="docutils literal"><span class="pre">input_encoding</span></tt>
  447. parameter specifying the encoding of the templates (identical to
  448. <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>/<a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>&#8216;s <tt class="docutils literal"><span class="pre">input_encoding</span></tt> parameter).</p>
  449. <p>Invoking <a class="reference external" href="http://babel.edgewall.org/">Babel</a>&#8216;s extractor at the
  450. command line in the project&#8217;s root directory:</p>
  451. <div class="highlight-sh"><div class="highlight"><pre>myproj<span class="nv">$ </span>pybabel extract -F babel.cfg -c <span class="s2">&quot;TRANSLATORS:&quot;</span> .
  452. </pre></div>
  453. </div>
  454. <p>will output a <cite>gettext</cite> catalog to <cite>stdout</cite> including the following:</p>
  455. <div class="highlight-pot"><div class="highlight"><pre><span class="c1">#. TRANSLATORS: This is a proper name. See the gettext</span>
  456. <span class="c1">#. manual, section Names.</span>
  457. <span class="kd">#: myproj/templates/name.html:5</span>
  458. <span class="nv">msgid</span> <span class="s">&quot;Francois Pinard&quot;</span>
  459. <span class="nv">msgstr</span> <span class="s">&quot;&quot;</span>
  460. </pre></div>
  461. </div>
  462. <p>This is only a basic example:
  463. <a class="reference external" href="http://babel.edgewall.org/">Babel</a> can be invoked from <tt class="docutils literal"><span class="pre">setup.py</span></tt>
  464. and its command line options specified in the accompanying
  465. <tt class="docutils literal"><span class="pre">setup.cfg</span></tt> via <a class="reference external" href="http://babel.edgewall.org/wiki/Documentation/setup.html">Babel Distutils/Setuptools
  466. Integration</a>.</p>
  467. <p>Comments must immediately precede a <cite>gettext</cite> message to be
  468. extracted. In the following case the <tt class="docutils literal"><span class="pre">TRANSLATORS:</span></tt> comment would
  469. not have been extracted:</p>
  470. <div class="highlight-mako"><div class="highlight"><pre><span class="x">&lt;div id=&quot;name&quot;&gt;</span>
  471. <span class="x"> ## TRANSLATORS: This is a proper name. See the gettext</span>
  472. <span class="x"> ## manual, section Names.</span>
  473. <span class="x"> Name: </span><span class="cp">${</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Francois Pinard&#39;</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  474. <span class="x">&lt;/div&gt;</span>
  475. </pre></div>
  476. </div>
  477. <p>See the <a class="reference external" href="http://babel.edgewall.org/wiki/Documentation/index.html">Babel User
  478. Guide</a>
  479. for more information.</p>
  480. </div>
  481. </div>
  482. <div class="section" id="api-reference">
  483. <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h2>
  484. <dl class="class">
  485. <dt id="mako.template.Template">
  486. <em class="property">class </em><tt class="descclassname">mako.template.</tt><tt class="descname">Template</tt><big>(</big><em>text=None</em>, <em>filename=None</em>, <em>uri=None</em>, <em>format_exceptions=False</em>, <em>error_handler=None</em>, <em>lookup=None</em>, <em>output_encoding=None</em>, <em>encoding_errors='strict'</em>, <em>module_directory=None</em>, <em>cache_args=None</em>, <em>cache_impl='beaker'</em>, <em>cache_enabled=True</em>, <em>cache_type=None</em>, <em>cache_dir=None</em>, <em>cache_url=None</em>, <em>module_filename=None</em>, <em>input_encoding=None</em>, <em>disable_unicode=False</em>, <em>module_writer=None</em>, <em>bytestring_passthrough=False</em>, <em>default_filters=None</em>, <em>buffer_filters=()</em>, <em>strict_undefined=False</em>, <em>imports=None</em>, <em>future_imports=None</em>, <em>enable_loop=True</em>, <em>preprocessor=None</em>, <em>lexer_cls=None</em><big>)</big><a class="headerlink" href="#mako.template.Template" title="Permalink to this definition">¶</a></dt>
  487. <dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
  488. <p>Represents a compiled template.</p>
  489. <p><a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> includes a reference to the original
  490. template source (via the <a class="reference internal" href="#RichTraceback.source" title="RichTraceback.source"><tt class="xref py py-attr docutils literal"><span class="pre">source</span></tt></a> attribute)
  491. as well as the source code of the
  492. generated Python module (i.e. the <a class="reference internal" href="#mako.template.Template.code" title="mako.template.Template.code"><tt class="xref py py-attr docutils literal"><span class="pre">code</span></tt></a> attribute),
  493. as well as a reference to an actual Python module.</p>
  494. <p><a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> is constructed using either a literal string
  495. representing the template text, or a filename representing a filesystem
  496. path to a source file.</p>
  497. <table class="docutils field-list" frame="void" rules="none">
  498. <col class="field-name" />
  499. <col class="field-body" />
  500. <tbody valign="top">
  501. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  502. <li><strong>text</strong> &#8211; textual template source. This argument is mutually
  503. exclusive versus the <tt class="docutils literal"><span class="pre">filename</span></tt> parameter.</li>
  504. <li><strong>filename</strong> &#8211; filename of the source template. This argument is
  505. mutually exclusive versus the <tt class="docutils literal"><span class="pre">text</span></tt> parameter.</li>
  506. <li><strong>buffer_filters</strong> &#8211; string list of filters to be applied
  507. to the output of <tt class="docutils literal"><span class="pre">%def</span></tt>s which are buffered, cached, or otherwise
  508. filtered, after all filters
  509. defined with the <tt class="docutils literal"><span class="pre">%def</span></tt> itself have been applied. Allows the
  510. creation of default expression filters that let the output
  511. of return-valued <tt class="docutils literal"><span class="pre">%def</span></tt>s &#8220;opt out&#8221; of that filtering via
  512. passing special attributes or objects.</li>
  513. <li><strong>bytestring_passthrough</strong> &#8211; <p>When <tt class="docutils literal"><span class="pre">True</span></tt>, and <tt class="docutils literal"><span class="pre">output_encoding</span></tt> is
  514. set to <tt class="docutils literal"><span class="pre">None</span></tt>, and <a class="reference internal" href="#mako.template.Template.render" title="mako.template.Template.render"><tt class="xref py py-meth docutils literal"><span class="pre">Template.render()</span></tt></a> is used to render,
  515. the <cite>StringIO</cite> or <cite>cStringIO</cite> buffer will be used instead of the
  516. default &#8220;fast&#8221; buffer. This allows raw bytestrings in the
  517. output stream, such as in expressions, to pass straight
  518. through to the buffer. This flag is forced
  519. to <tt class="docutils literal"><span class="pre">True</span></tt> if <tt class="docutils literal"><span class="pre">disable_unicode</span></tt> is also configured.</p>
  520. <p class="versionadded">
  521. <span class="versionmodified">New in version 0.4: </span>Added to provide the same behavior as that of the previous series.</p>
  522. </li>
  523. <li><strong>cache_args</strong> &#8211; Dictionary of cache configuration arguments that
  524. will be passed to the <a class="reference internal" href="caching.html#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a>. See <a class="reference internal" href="caching.html"><em>Caching</em></a>.</li>
  525. <li><strong>cache_dir</strong> &#8211; <p class="deprecated">
  526. <span class="versionmodified">Deprecated since version 0.6: </span>Use the <tt class="docutils literal"><span class="pre">'dir'</span></tt> argument in the <tt class="docutils literal"><span class="pre">cache_args</span></tt> dictionary.
  527. See <a class="reference internal" href="caching.html"><em>Caching</em></a>.</p>
  528. </li>
  529. <li><strong>cache_enabled</strong> &#8211; Boolean flag which enables caching of this
  530. template. See <a class="reference internal" href="caching.html"><em>Caching</em></a>.</li>
  531. <li><strong>cache_impl</strong> &#8211; String name of a <a class="reference internal" href="caching.html#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> caching
  532. implementation to use. Defaults to <tt class="docutils literal"><span class="pre">'beaker'</span></tt>.</li>
  533. <li><strong>cache_type</strong> &#8211; <p class="deprecated">
  534. <span class="versionmodified">Deprecated since version 0.6: </span>Use the <tt class="docutils literal"><span class="pre">'type'</span></tt> argument in the <tt class="docutils literal"><span class="pre">cache_args</span></tt> dictionary.
  535. See <a class="reference internal" href="caching.html"><em>Caching</em></a>.</p>
  536. </li>
  537. <li><strong>cache_url</strong> &#8211; <p class="deprecated">
  538. <span class="versionmodified">Deprecated since version 0.6: </span>Use the <tt class="docutils literal"><span class="pre">'url'</span></tt> argument in the <tt class="docutils literal"><span class="pre">cache_args</span></tt> dictionary.
  539. See <a class="reference internal" href="caching.html"><em>Caching</em></a>.</p>
  540. </li>
  541. <li><strong>default_filters</strong> &#8211; List of string filter names that will
  542. be applied to all expressions. See <a class="reference internal" href="filtering.html#filtering-default-filters"><em>The default_filters Argument</em></a>.</li>
  543. <li><strong>disable_unicode</strong> &#8211; Disables all awareness of Python Unicode
  544. objects. See <a class="reference internal" href="unicode.html#unicode-disabled"><em>Saying to Heck with It: Disabling the Usage of Unicode Entirely</em></a>.</li>
  545. <li><strong>enable_loop</strong> &#8211; When <tt class="docutils literal"><span class="pre">True</span></tt>, enable the <tt class="docutils literal"><span class="pre">loop</span></tt> context variable.
  546. This can be set to <tt class="docutils literal"><span class="pre">False</span></tt> to support templates that may
  547. be making usage of the name &#8220;<tt class="docutils literal"><span class="pre">loop</span></tt>&#8221;. Individual templates can
  548. re-enable the &#8220;loop&#8221; context by placing the directive
  549. <tt class="docutils literal"><span class="pre">enable_loop=&quot;True&quot;</span></tt> inside the <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> tag &#8211; see
  550. <a class="reference internal" href="runtime.html#migrating-loop"><em>Migrating Legacy Templates that Use the Word &#8220;loop&#8221;</em></a>.</li>
  551. <li><strong>encoding_errors</strong> &#8211; Error parameter passed to <tt class="docutils literal"><span class="pre">encode()</span></tt> when
  552. string encoding is performed. See <a class="reference internal" href="#usage-unicode"><em>Using Unicode and Encoding</em></a>.</li>
  553. <li><strong>error_handler</strong> &#8211; Python callable which is called whenever
  554. compile or runtime exceptions occur. The callable is passed
  555. the current context as well as the exception. If the
  556. callable returns <tt class="docutils literal"><span class="pre">True</span></tt>, the exception is considered to
  557. be handled, else it is re-raised after the function
  558. completes. Is used to provide custom error-rendering
  559. functions.</li>
  560. <li><strong>format_exceptions</strong> &#8211; if <tt class="docutils literal"><span class="pre">True</span></tt>, exceptions which occur during
  561. the render phase of this template will be caught and
  562. formatted into an HTML error page, which then becomes the
  563. rendered result of the <a class="reference internal" href="#mako.template.Template.render" title="mako.template.Template.render"><tt class="xref py py-meth docutils literal"><span class="pre">render()</span></tt></a> call. Otherwise,
  564. runtime exceptions are propagated outwards.</li>
  565. <li><strong>imports</strong> &#8211; String list of Python statements, typically individual
  566. &#8220;import&#8221; lines, which will be placed into the module level
  567. preamble of all generated Python modules. See the example
  568. in <a class="reference internal" href="filtering.html#filtering-default-filters"><em>The default_filters Argument</em></a>.</li>
  569. <li><strong>future_imports</strong> &#8211; String list of names to import from <cite>__future__</cite>.
  570. These will be concatenated into a comma-separated string and inserted
  571. into the beginning of the template, e.g. <tt class="docutils literal"><span class="pre">futures_imports=['FOO',</span>
  572. <span class="pre">'BAR']</span></tt> results in <tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">FOO,</span> <span class="pre">BAR</span></tt>. If you&#8217;re
  573. interested in using features like the new division operator, you must
  574. use future_imports to convey that to the renderer, as otherwise the
  575. import will not appear as the first executed statement in the generated
  576. code and will therefore not have the desired effect.</li>
  577. <li><strong>input_encoding</strong> &#8211; Encoding of the template&#8217;s source code. Can
  578. be used in lieu of the coding comment. See
  579. <a class="reference internal" href="#usage-unicode"><em>Using Unicode and Encoding</em></a> as well as <a class="reference internal" href="unicode.html"><em>The Unicode Chapter</em></a> for
  580. details on source encoding.</li>
  581. <li><strong>lookup</strong> &#8211; a <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> instance that will be used
  582. for all file lookups via the <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt>,
  583. <tt class="docutils literal"><span class="pre">&lt;%include&gt;</span></tt>, and <tt class="docutils literal"><span class="pre">&lt;%inherit&gt;</span></tt> tags. See
  584. <a class="reference internal" href="#usage-templatelookup"><em>Using TemplateLookup</em></a>.</li>
  585. <li><strong>module_directory</strong> &#8211; Filesystem location where generated
  586. Python module files will be placed.</li>
  587. <li><strong>module_filename</strong> &#8211; Overrides the filename of the generated
  588. Python module file. For advanced usage only.</li>
  589. <li><strong>module_writer</strong> &#8211; <p>A callable which overrides how the Python
  590. module is written entirely. The callable is passed the
  591. encoded source content of the module and the destination
  592. path to be written to. The default behavior of module writing
  593. uses a tempfile in conjunction with a file move in order
  594. to make the operation atomic. So a user-defined module
  595. writing function that mimics the default behavior would be:</p>
  596. <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">tempfile</span>
  597. <span class="kn">import</span> <span class="nn">os</span>
  598. <span class="kn">import</span> <span class="nn">shutil</span>
  599. <span class="k">def</span> <span class="nf">module_writer</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">outputpath</span><span class="p">):</span>
  600. <span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> <span class="o">=</span> \
  601. <span class="n">tempfile</span><span class="o">.</span><span class="n">mkstemp</span><span class="p">(</span>
  602. <span class="nb">dir</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">outputpath</span><span class="p">)</span>
  603. <span class="p">)</span>
  604. <span class="n">os</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span>
  605. <span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
  606. <span class="n">shutil</span><span class="o">.</span><span class="n">move</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">outputpath</span><span class="p">)</span>
  607. <span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  608. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span>
  609. <span class="n">filename</span><span class="o">=</span><span class="s">&quot;index.html&quot;</span><span class="p">,</span>
  610. <span class="n">module_directory</span><span class="o">=</span><span class="s">&quot;/path/to/modules&quot;</span><span class="p">,</span>
  611. <span class="n">module_writer</span><span class="o">=</span><span class="n">module_writer</span>
  612. <span class="p">)</span>
  613. </pre></div>
  614. </div>
  615. <p>The function is provided for unusual configurations where
  616. certain platform-specific permissions or other special
  617. steps are needed.</p>
  618. </li>
  619. <li><strong>output_encoding</strong> &#8211; The encoding to use when <a class="reference internal" href="#mako.template.Template.render" title="mako.template.Template.render"><tt class="xref py py-meth docutils literal"><span class="pre">render()</span></tt></a>
  620. is called.
  621. See <a class="reference internal" href="#usage-unicode"><em>Using Unicode and Encoding</em></a> as well as <a class="reference internal" href="unicode.html"><em>The Unicode Chapter</em></a>.</li>
  622. <li><strong>preprocessor</strong> &#8211; Python callable which will be passed
  623. the full template source before it is parsed. The return
  624. result of the callable will be used as the template source
  625. code.</li>
  626. <li><strong>lexer_cls</strong> &#8211; <p>A <tt class="xref py py-class docutils literal"><span class="pre">Lexer</span></tt> class used to parse
  627. the template. The <tt class="xref py py-class docutils literal"><span class="pre">Lexer</span></tt> class is used by
  628. default.</p>
  629. <p class="versionadded">
  630. <span class="versionmodified">New in version 0.7.4.</span></p>
  631. </li>
  632. <li><strong>strict_undefined</strong> &#8211; <p>Replaces the automatic usage of
  633. <tt class="docutils literal"><span class="pre">UNDEFINED</span></tt> for any undeclared variables not located in
  634. the <a class="reference internal" href="runtime.html#mako.runtime.Context" title="mako.runtime.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> with an immediate raise of
  635. <tt class="docutils literal"><span class="pre">NameError</span></tt>. The advantage is immediate reporting of
  636. missing variables which include the name.</p>
  637. <p class="versionadded">
  638. <span class="versionmodified">New in version 0.3.6.</span></p>
  639. </li>
  640. <li><strong>uri</strong> &#8211; string URI or other identifier for this template.
  641. If not provided, the <tt class="docutils literal"><span class="pre">uri</span></tt> is generated from the filesystem
  642. path, or from the in-memory identity of a non-file-based
  643. template. The primary usage of the <tt class="docutils literal"><span class="pre">uri</span></tt> is to provide a key
  644. within <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>, as well as to generate the
  645. file path of the generated Python module file, if
  646. <tt class="docutils literal"><span class="pre">module_directory</span></tt> is specified.</li>
  647. </ul>
  648. </td>
  649. </tr>
  650. </tbody>
  651. </table>
  652. <dl class="attribute">
  653. <dt id="mako.template.Template.code">
  654. <tt class="descname">code</tt><a class="headerlink" href="#mako.template.Template.code" title="Permalink to this definition">¶</a></dt>
  655. <dd><p>Return the module source code for this <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>.</p>
  656. </dd></dl>
  657. <dl class="method">
  658. <dt id="mako.template.Template.get_def">
  659. <tt class="descname">get_def</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#mako.template.Template.get_def" title="Permalink to this definition">¶</a></dt>
  660. <dd><p>Return a def of this template as a <a class="reference internal" href="#mako.template.DefTemplate" title="mako.template.DefTemplate"><tt class="xref py py-class docutils literal"><span class="pre">DefTemplate</span></tt></a>.</p>
  661. </dd></dl>
  662. <dl class="method">
  663. <dt id="mako.template.Template.render">
  664. <tt class="descname">render</tt><big>(</big><em>*args</em>, <em>**data</em><big>)</big><a class="headerlink" href="#mako.template.Template.render" title="Permalink to this definition">¶</a></dt>
  665. <dd><p>Render the output of this template as a string.</p>
  666. <p>If the template specifies an output encoding, the string
  667. will be encoded accordingly, else the output is raw (raw
  668. output uses <cite>cStringIO</cite> and can&#8217;t handle multibyte
  669. characters). A <a class="reference internal" href="runtime.html#mako.runtime.Context" title="mako.runtime.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> object is created corresponding
  670. to the given data. Arguments that are explicitly declared
  671. by this template&#8217;s internal rendering method are also
  672. pulled from the given <tt class="docutils literal"><span class="pre">*args</span></tt>, <tt class="docutils literal"><span class="pre">**data</span></tt> members.</p>
  673. </dd></dl>
  674. <dl class="method">
  675. <dt id="mako.template.Template.render_context">
  676. <tt class="descname">render_context</tt><big>(</big><em>context</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mako.template.Template.render_context" title="Permalink to this definition">¶</a></dt>
  677. <dd><p>Render this <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> with the given context.</p>
  678. <p>The data is written to the context&#8217;s buffer.</p>
  679. </dd></dl>
  680. <dl class="method">
  681. <dt id="mako.template.Template.render_unicode">
  682. <tt class="descname">render_unicode</tt><big>(</big><em>*args</em>, <em>**data</em><big>)</big><a class="headerlink" href="#mako.template.Template.render_unicode" title="Permalink to this definition">¶</a></dt>
  683. <dd><p>Render the output of this template as a unicode object.</p>
  684. </dd></dl>
  685. <dl class="attribute">
  686. <dt id="mako.template.Template.source">
  687. <tt class="descname">source</tt><a class="headerlink" href="#mako.template.Template.source" title="Permalink to this definition">¶</a></dt>
  688. <dd><p>Return the template source code for this <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>.</p>
  689. </dd></dl>
  690. </dd></dl>
  691. <dl class="class">
  692. <dt id="mako.template.DefTemplate">
  693. <em class="property">class </em><tt class="descclassname">mako.template.</tt><tt class="descname">DefTemplate</tt><big>(</big><em>parent</em>, <em>callable_</em><big>)</big><a class="headerlink" href="#mako.template.DefTemplate" title="Permalink to this definition">¶</a></dt>
  694. <dd><p>Bases: <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">mako.template.Template</span></tt></a></p>
  695. <p>A <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> which represents a callable def in a parent
  696. template.</p>
  697. </dd></dl>
  698. <dl class="class">
  699. <dt id="mako.lookup.TemplateCollection">
  700. <em class="property">class </em><tt class="descclassname">mako.lookup.</tt><tt class="descname">TemplateCollection</tt><a class="headerlink" href="#mako.lookup.TemplateCollection" title="Permalink to this definition">¶</a></dt>
  701. <dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
  702. <p>Represent a collection of <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> objects,
  703. identifiable via URI.</p>
  704. <p>A <a class="reference internal" href="#mako.lookup.TemplateCollection" title="mako.lookup.TemplateCollection"><tt class="xref py py-class docutils literal"><span class="pre">TemplateCollection</span></tt></a> is linked to the usage of
  705. all template tags that address other templates, such
  706. as <tt class="docutils literal"><span class="pre">&lt;%include&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt>, and <tt class="docutils literal"><span class="pre">&lt;%inherit&gt;</span></tt>.
  707. The <tt class="docutils literal"><span class="pre">file</span></tt> attribute of each of those tags refers
  708. to a string URI that is passed to that <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>
  709. object&#8217;s <a class="reference internal" href="#mako.lookup.TemplateCollection" title="mako.lookup.TemplateCollection"><tt class="xref py py-class docutils literal"><span class="pre">TemplateCollection</span></tt></a> for resolution.</p>
  710. <p><a class="reference internal" href="#mako.lookup.TemplateCollection" title="mako.lookup.TemplateCollection"><tt class="xref py py-class docutils literal"><span class="pre">TemplateCollection</span></tt></a> is an abstract class,
  711. with the usual default implementation being <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>.</p>
  712. <dl class="method">
  713. <dt id="mako.lookup.TemplateCollection.adjust_uri">
  714. <tt class="descname">adjust_uri</tt><big>(</big><em>uri</em>, <em>filename</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateCollection.adjust_uri" title="Permalink to this definition">¶</a></dt>
  715. <dd><p>Adjust the given <tt class="docutils literal"><span class="pre">uri</span></tt> based on the calling <tt class="docutils literal"><span class="pre">filename</span></tt>.</p>
  716. <p>When this method is called from the runtime, the
  717. <tt class="docutils literal"><span class="pre">filename</span></tt> parameter is taken directly to the <tt class="docutils literal"><span class="pre">filename</span></tt>
  718. attribute of the calling template. Therefore a custom
  719. <a class="reference internal" href="#mako.lookup.TemplateCollection" title="mako.lookup.TemplateCollection"><tt class="xref py py-class docutils literal"><span class="pre">TemplateCollection</span></tt></a> subclass can place any string
  720. identifier desired in the <tt class="docutils literal"><span class="pre">filename</span></tt> parameter of the
  721. <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> objects it constructs and have them come back
  722. here.</p>
  723. </dd></dl>
  724. <dl class="method">
  725. <dt id="mako.lookup.TemplateCollection.filename_to_uri">
  726. <tt class="descname">filename_to_uri</tt><big>(</big><em>uri</em>, <em>filename</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateCollection.filename_to_uri" title="Permalink to this definition">¶</a></dt>
  727. <dd><p>Convert the given <tt class="docutils literal"><span class="pre">filename</span></tt> to a URI relative to
  728. this <a class="reference internal" href="#mako.lookup.TemplateCollection" title="mako.lookup.TemplateCollection"><tt class="xref py py-class docutils literal"><span class="pre">TemplateCollection</span></tt></a>.</p>
  729. </dd></dl>
  730. <dl class="method">
  731. <dt id="mako.lookup.TemplateCollection.get_template">
  732. <tt class="descname">get_template</tt><big>(</big><em>uri</em>, <em>relativeto=None</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateCollection.get_template" title="Permalink to this definition">¶</a></dt>
  733. <dd><p>Return a <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object corresponding to the given
  734. <tt class="docutils literal"><span class="pre">uri</span></tt>.</p>
  735. <p>The default implementation raises
  736. <tt class="xref py py-class docutils literal"><span class="pre">NotImplementedError</span></tt>. Implementations should
  737. raise <tt class="xref py py-class docutils literal"><span class="pre">TemplateLookupException</span></tt> if the given <tt class="docutils literal"><span class="pre">uri</span></tt>
  738. cannot be resolved.</p>
  739. <table class="docutils field-list" frame="void" rules="none">
  740. <col class="field-name" />
  741. <col class="field-body" />
  742. <tbody valign="top">
  743. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  744. <li><strong>uri</strong> &#8211; String URI of the template to be resolved.</li>
  745. <li><strong>relativeto</strong> &#8211; if present, the given <tt class="docutils literal"><span class="pre">uri</span></tt> is assumed to
  746. be relative to this URI.</li>
  747. </ul>
  748. </td>
  749. </tr>
  750. </tbody>
  751. </table>
  752. </dd></dl>
  753. <dl class="method">
  754. <dt id="mako.lookup.TemplateCollection.has_template">
  755. <tt class="descname">has_template</tt><big>(</big><em>uri</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateCollection.has_template" title="Permalink to this definition">¶</a></dt>
  756. <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if this <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> is
  757. capable of returning a <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object for the
  758. given <tt class="docutils literal"><span class="pre">uri</span></tt>.</p>
  759. <table class="docutils field-list" frame="void" rules="none">
  760. <col class="field-name" />
  761. <col class="field-body" />
  762. <tbody valign="top">
  763. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>uri</strong> &#8211; String URI of the template to be resolved.</td>
  764. </tr>
  765. </tbody>
  766. </table>
  767. </dd></dl>
  768. </dd></dl>
  769. <dl class="class">
  770. <dt id="mako.lookup.TemplateLookup">
  771. <em class="property">class </em><tt class="descclassname">mako.lookup.</tt><tt class="descname">TemplateLookup</tt><big>(</big><em>directories=None</em>, <em>module_directory=None</em>, <em>filesystem_checks=True</em>, <em>collection_size=-1</em>, <em>format_exceptions=False</em>, <em>error_handler=None</em>, <em>disable_unicode=False</em>, <em>bytestring_passthrough=False</em>, <em>output_encoding=None</em>, <em>encoding_errors='strict'</em>, <em>cache_args=None</em>, <em>cache_impl='beaker'</em>, <em>cache_enabled=True</em>, <em>cache_type=None</em>, <em>cache_dir=None</em>, <em>cache_url=None</em>, <em>modulename_callable=None</em>, <em>module_writer=None</em>, <em>default_filters=None</em>, <em>buffer_filters=()</em>, <em>strict_undefined=False</em>, <em>imports=None</em>, <em>future_imports=None</em>, <em>enable_loop=True</em>, <em>input_encoding=None</em>, <em>preprocessor=None</em>, <em>lexer_cls=None</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateLookup" title="Permalink to this definition">¶</a></dt>
  772. <dd><p>Bases: <a class="reference internal" href="#mako.lookup.TemplateCollection" title="mako.lookup.TemplateCollection"><tt class="xref py py-class docutils literal"><span class="pre">mako.lookup.TemplateCollection</span></tt></a></p>
  773. <p>Represent a collection of templates that locates template source files
  774. from the local filesystem.</p>
  775. <p>The primary argument is the <tt class="docutils literal"><span class="pre">directories</span></tt> argument, the list of
  776. directories to search:</p>
  777. <div class="highlight-python"><div class="highlight"><pre><span class="n">lookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">([</span><span class="s">&quot;/path/to/templates&quot;</span><span class="p">])</span>
  778. <span class="n">some_template</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">&quot;/index.html&quot;</span><span class="p">)</span>
  779. </pre></div>
  780. </div>
  781. <p>The <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> can also be given <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> objects
  782. programatically using <a class="reference internal" href="#mako.lookup.TemplateLookup.put_string" title="mako.lookup.TemplateLookup.put_string"><tt class="xref py py-meth docutils literal"><span class="pre">put_string()</span></tt></a> or <a class="reference internal" href="#mako.lookup.TemplateLookup.put_template" title="mako.lookup.TemplateLookup.put_template"><tt class="xref py py-meth docutils literal"><span class="pre">put_template()</span></tt></a>:</p>
  783. <div class="highlight-python"><div class="highlight"><pre><span class="n">lookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">()</span>
  784. <span class="n">lookup</span><span class="o">.</span><span class="n">put_string</span><span class="p">(</span><span class="s">&quot;base.html&quot;</span><span class="p">,</span> <span class="s">&#39;&#39;&#39;</span>
  785. <span class="s"> &lt;html&gt;&lt;body&gt;${self.next()}&lt;/body&gt;&lt;/html&gt;</span>
  786. <span class="s">&#39;&#39;&#39;</span><span class="p">)</span>
  787. <span class="n">lookup</span><span class="o">.</span><span class="n">put_string</span><span class="p">(</span><span class="s">&quot;hello.html&quot;</span><span class="p">,</span> <span class="s">&#39;&#39;&#39;</span>
  788. <span class="s"> &lt;</span><span class="si">%i</span><span class="s">nclude file=&#39;base.html&#39;/&gt;</span>
  789. <span class="s"> Hello, world !</span>
  790. <span class="s">&#39;&#39;&#39;</span><span class="p">)</span>
  791. </pre></div>
  792. </div>
  793. <table class="docutils field-list" frame="void" rules="none">
  794. <col class="field-name" />
  795. <col class="field-body" />
  796. <tbody valign="top">
  797. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  798. <li><strong>directories</strong> &#8211; A list of directory names which will be
  799. searched for a particular template URI. The URI is appended
  800. to each directory and the filesystem checked.</li>
  801. <li><strong>collection_size</strong> &#8211; Approximate size of the collection used
  802. to store templates. If left at its default of <tt class="docutils literal"><span class="pre">-1</span></tt>, the size
  803. is unbounded, and a plain Python dictionary is used to
  804. relate URI strings to <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> instances.
  805. Otherwise, a least-recently-used cache object is used which
  806. will maintain the size of the collection approximately to
  807. the number given.</li>
  808. <li><strong>filesystem_checks</strong> &#8211; When at its default value of <tt class="docutils literal"><span class="pre">True</span></tt>,
  809. each call to <a class="reference internal" href="#mako.lookup.TemplateLookup.get_template" title="mako.lookup.TemplateLookup.get_template"><tt class="xref py py-meth docutils literal"><span class="pre">TemplateLookup.get_template()</span></tt></a> will
  810. compare the filesystem last modified time to the time in
  811. which an existing <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object was created.
  812. This allows the <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> to regenerate a
  813. new <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> whenever the original source has
  814. been updated. Set this to <tt class="docutils literal"><span class="pre">False</span></tt> for a very minor
  815. performance increase.</li>
  816. <li><strong>modulename_callable</strong> &#8211; A callable which, when present,
  817. is passed the path of the source file as well as the
  818. requested URI, and then returns the full path of the
  819. generated Python module file. This is used to inject
  820. alternate schemes for Python module location. If left at
  821. its default of <tt class="docutils literal"><span class="pre">None</span></tt>, the built in system of generation
  822. based on <tt class="docutils literal"><span class="pre">module_directory</span></tt> plus <tt class="docutils literal"><span class="pre">uri</span></tt> is used.</li>
  823. </ul>
  824. </td>
  825. </tr>
  826. </tbody>
  827. </table>
  828. <p>All other keyword parameters available for
  829. <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> are mirrored here. When new
  830. <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> objects are created, the keywords
  831. established with this <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> are passed on
  832. to each new <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>.</p>
  833. <dl class="method">
  834. <dt id="mako.lookup.TemplateLookup.adjust_uri">
  835. <tt class="descname">adjust_uri</tt><big>(</big><em>uri</em>, <em>relativeto</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateLookup.adjust_uri" title="Permalink to this definition">¶</a></dt>
  836. <dd><p>Adjust the given <tt class="docutils literal"><span class="pre">uri</span></tt> based on the given relative URI.</p>
  837. </dd></dl>
  838. <dl class="method">
  839. <dt id="mako.lookup.TemplateLookup.filename_to_uri">
  840. <tt class="descname">filename_to_uri</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateLookup.filename_to_uri" title="Permalink to this definition">¶</a></dt>
  841. <dd><p>Convert the given <tt class="docutils literal"><span class="pre">filename</span></tt> to a URI relative to
  842. this <a class="reference internal" href="#mako.lookup.TemplateCollection" title="mako.lookup.TemplateCollection"><tt class="xref py py-class docutils literal"><span class="pre">TemplateCollection</span></tt></a>.</p>
  843. </dd></dl>
  844. <dl class="method">
  845. <dt id="mako.lookup.TemplateLookup.get_template">
  846. <tt class="descname">get_template</tt><big>(</big><em>uri</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateLookup.get_template" title="Permalink to this definition">¶</a></dt>
  847. <dd><p>Return a <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object corresponding to the given
  848. <tt class="docutils literal"><span class="pre">uri</span></tt>.</p>
  849. <div class="admonition note">
  850. <p class="first admonition-title">Note</p>
  851. <p class="last">The <tt class="docutils literal"><span class="pre">relativeto</span></tt> argument is not supported here at the moment.</p>
  852. </div>
  853. </dd></dl>
  854. <dl class="method">
  855. <dt id="mako.lookup.TemplateLookup.put_string">
  856. <tt class="descname">put_string</tt><big>(</big><em>uri</em>, <em>text</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateLookup.put_string" title="Permalink to this definition">¶</a></dt>
  857. <dd><p>Place a new <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object into this
  858. <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>, based on the given string of
  859. <tt class="docutils literal"><span class="pre">text</span></tt>.</p>
  860. </dd></dl>
  861. <dl class="method">
  862. <dt id="mako.lookup.TemplateLookup.put_template">
  863. <tt class="descname">put_template</tt><big>(</big><em>uri</em>, <em>template</em><big>)</big><a class="headerlink" href="#mako.lookup.TemplateLookup.put_template" title="Permalink to this definition">¶</a></dt>
  864. <dd><p>Place a new <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object into this
  865. <a class="reference internal" href="#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>, based on the given
  866. <a class="reference internal" href="#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object.</p>
  867. </dd></dl>
  868. </dd></dl>
  869. <dl class="class">
  870. <dt id="mako.exceptions.RichTraceback">
  871. <em class="property">class </em><tt class="descclassname">mako.exceptions.</tt><tt class="descname">RichTraceback</tt><big>(</big><em>error=None</em>, <em>traceback=None</em><big>)</big><a class="headerlink" href="#mako.exceptions.RichTraceback" title="Permalink to this definition">¶</a></dt>
  872. <dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
  873. <p>Pull the current exception from the <tt class="docutils literal"><span class="pre">sys</span></tt> traceback and extracts
  874. Mako-specific template information.</p>
  875. <p>See the usage examples in <a class="reference internal" href="#handling-exceptions"><em>Handling Exceptions</em></a>.</p>
  876. <dl class="attribute">
  877. <dt id="RichTraceback.error">
  878. <tt class="descname">error</tt><a class="headerlink" href="#RichTraceback.error" title="Permalink to this definition">¶</a></dt>
  879. <dd><p>the exception instance.</p>
  880. </dd></dl>
  881. <dl class="attribute">
  882. <dt id="RichTraceback.message">
  883. <tt class="descname">message</tt><a class="headerlink" href="#RichTraceback.message" title="Permalink to this definition">¶</a></dt>
  884. <dd><p>the exception error message as unicode.</p>
  885. </dd></dl>
  886. <dl class="attribute">
  887. <dt id="RichTraceback.source">
  888. <tt class="descname">source</tt><a class="headerlink" href="#RichTraceback.source" title="Permalink to this definition">¶</a></dt>
  889. <dd><p>source code of the file where the error occurred.
  890. If the error occurred within a compiled template,
  891. this is the template source.</p>
  892. </dd></dl>
  893. <dl class="attribute">
  894. <dt id="RichTraceback.lineno">
  895. <tt class="descname">lineno</tt><a class="headerlink" href="#RichTraceback.lineno" title="Permalink to this definition">¶</a></dt>
  896. <dd><p>line number where the error occurred. If the error
  897. occurred within a compiled template, the line number
  898. is adjusted to that of the template source.</p>
  899. </dd></dl>
  900. <dl class="attribute">
  901. <dt id="RichTraceback.records">
  902. <tt class="descname">records</tt><a class="headerlink" href="#RichTraceback.records" title="Permalink to this definition">¶</a></dt>
  903. <dd><p>a list of 8-tuples containing the original
  904. python traceback elements, plus the
  905. filename, line number, source line, and full template source
  906. for the traceline mapped back to its originating source
  907. template, if any for that traceline (else the fields are <tt class="docutils literal"><span class="pre">None</span></tt>).</p>
  908. </dd></dl>
  909. <dl class="attribute">
  910. <dt id="RichTraceback.reverse_records">
  911. <tt class="descname">reverse_records</tt><a class="headerlink" href="#RichTraceback.reverse_records" title="Permalink to this definition">¶</a></dt>
  912. <dd><p>the list of records in reverse
  913. traceback &#8211; a list of 4-tuples, in the same format as a regular
  914. python traceback, with template-corresponding
  915. traceback records replacing the originals.</p>
  916. </dd></dl>
  917. <dl class="attribute">
  918. <dt id="RichTraceback.reverse_traceback">
  919. <tt class="descname">reverse_traceback</tt><a class="headerlink" href="#RichTraceback.reverse_traceback" title="Permalink to this definition">¶</a></dt>
  920. <dd><p>the traceback list in reverse.</p>
  921. </dd></dl>
  922. </dd></dl>
  923. <dl class="function">
  924. <dt id="mako.exceptions.html_error_template">
  925. <tt class="descclassname">mako.exceptions.</tt><tt class="descname">html_error_template</tt><big>(</big><big>)</big><a class="headerlink" href="#mako.exceptions.html_error_template" title="Permalink to this definition">¶</a></dt>
  926. <dd><p>Provides a template that renders a stack trace in an HTML format,
  927. providing an excerpt of code as well as substituting source template
  928. filenames, line numbers and code for that of the originating source
  929. template, as applicable.</p>
  930. <p>The template&#8217;s default <tt class="docutils literal"><span class="pre">encoding_errors</span></tt> value is <tt class="docutils literal"><span class="pre">'htmlentityreplace'</span></tt>. The
  931. template has two options. With the <tt class="docutils literal"><span class="pre">full</span></tt> option disabled, only a section of
  932. an HTML document is returned. With the <tt class="docutils literal"><span class="pre">css</span></tt> option disabled, the default
  933. stylesheet won&#8217;t be included.</p>
  934. </dd></dl>
  935. <dl class="function">
  936. <dt id="mako.exceptions.text_error_template">
  937. <tt class="descclassname">mako.exceptions.</tt><tt class="descname">text_error_template</tt><big>(</big><em>lookup=None</em><big>)</big><a class="headerlink" href="#mako.exceptions.text_error_template" title="Permalink to this definition">¶</a></dt>
  938. <dd><p>Provides a template that renders a stack trace in a similar format to
  939. the Python interpreter, substituting source template filenames, line
  940. numbers and code for that of the originating source template, as
  941. applicable.</p>
  942. </dd></dl>
  943. </div>
  944. </div>
  945. </div>
  946. </div>
  947. <div id="docs-bottom-navigation" class="docs-navigation-links">
  948. Previous:
  949. <a href="index.html" title="previous chapter">Table of Contents</a>
  950. Next:
  951. <a href="syntax.html" title="next chapter">Syntax</a>
  952. <div id="docs-copyright">
  953. &copy; Copyright the Mako authors and contributors.
  954. Documentation generated using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3
  955. with Mako templates.
  956. </div>
  957. </div>
  958. </div>
  959. <div class="clearfix">
  960. <hr/>
  961. <div class="copyright">Website content copyright &copy; by Michael Bayer.
  962. All rights reserved. Mako and its documentation are licensed
  963. under the MIT license. mike(&)zzzcomputing.com</div>
  964. </div>
  965. </div>
  966. </body>
  967. </html>