namespaces.html 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. Namespaces
  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="Inheritance" href="inheritance.html" />
  27. <link rel="prev" title="The Mako Runtime Environment" href="runtime.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="runtime.html" title="previous chapter">The Mako Runtime Environment</a>
  59. </li>
  60. <li>Next:
  61. <a href="inheritance.html" title="next chapter">Inheritance</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/namespaces.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. Namespaces
  74. <h2>
  75. Namespaces
  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="#">Namespaces</a><ul>
  84. <li><a class="reference internal" href="#ways-to-call-namespaces">Ways to Call Namespaces</a></li>
  85. <li><a class="reference internal" href="#namespaces-from-regular-python-modules">Namespaces from Regular Python Modules</a></li>
  86. <li><a class="reference internal" href="#declaring-defs-in-namespaces">Declaring Defs in Namespaces</a></li>
  87. <li><a class="reference internal" href="#the-body-method">The <tt class="docutils literal"><span class="pre">body()</span></tt> Method</a></li>
  88. <li><a class="reference internal" href="#built-in-namespaces">Built-in Namespaces</a><ul>
  89. <li><a class="reference internal" href="#local"><tt class="docutils literal"><span class="pre">local</span></tt></a></li>
  90. <li><a class="reference internal" href="#self"><tt class="docutils literal"><span class="pre">self</span></tt></a></li>
  91. </ul>
  92. </li>
  93. <li><a class="reference internal" href="#inheritable-namespaces">Inheritable Namespaces</a></li>
  94. <li><a class="reference internal" href="#namespace-api-usage-example-static-dependencies">Namespace API Usage Example - Static Dependencies</a><ul>
  95. <li><a class="reference internal" href="#version-one-use-namespace-attr">Version One - Use <tt class="docutils literal"><span class="pre">Namespace.attr</span></tt></a></li>
  96. <li><a class="reference internal" href="#version-two-use-a-specific-named-def">Version Two - Use a specific named def</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="runtime.html" title="previous chapter">The Mako Runtime Environment</a>
  106. </p>
  107. <h4>Next Topic</h4>
  108. <p>
  109. <a href="inheritance.html" title="next chapter">Inheritance</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="namespaces">
  122. <span id="namespaces-toplevel"></span><h1>Namespaces<a class="headerlink" href="#namespaces" title="Permalink to this headline">¶</a></h1>
  123. <p>Namespaces are used to organize groups of defs into
  124. categories, and also to &#8220;import&#8221; defs from other files.</p>
  125. <p>If the file <tt class="docutils literal"><span class="pre">components.html</span></tt> defines these two defs:</p>
  126. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## components.html</span><span class="x"></span>
  127. <span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;comp1()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  128. <span class="x"> this is comp1</span>
  129. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  130. <span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;comp2(x)&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  131. <span class="x"> this is comp2, x is </span><span class="cp">${</span><span class="n">x</span><span class="cp">}</span><span class="x"></span>
  132. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  133. </pre></div>
  134. </div>
  135. <p>you can make another file, for example <tt class="docutils literal"><span class="pre">index.html</span></tt>, that
  136. pulls those two defs into a namespace called <tt class="docutils literal"><span class="pre">comp</span></tt>:</p>
  137. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## index.html</span><span class="x"></span>
  138. <span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">name=</span><span class="s">&quot;comp&quot;</span> <span class="na">file=</span><span class="s">&quot;components.html&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  139. <span class="x">Here&#39;s comp1: </span><span class="cp">${</span><span class="n">comp</span><span class="o">.</span><span class="n">comp1</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  140. <span class="x">Here&#39;s comp2: </span><span class="cp">${</span><span class="n">comp</span><span class="o">.</span><span class="n">comp2</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  141. </pre></div>
  142. </div>
  143. <p>The <tt class="docutils literal"><span class="pre">comp</span></tt> variable above is an instance of
  144. <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>, a <strong>proxy object</strong> which delivers
  145. method calls to the underlying template callable using the
  146. current context.</p>
  147. <p><tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> also provides an <tt class="docutils literal"><span class="pre">import</span></tt> attribute which can
  148. be used to pull the names into the local namespace, removing the
  149. need to call it via the &#8220;<tt class="docutils literal"><span class="pre">.</span></tt>&#8221; operator. When <tt class="docutils literal"><span class="pre">import</span></tt> is used, the
  150. <tt class="docutils literal"><span class="pre">name</span></tt> attribute is optional.</p>
  151. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">file=</span><span class="s">&quot;components.html&quot;</span> <span class="na">import=</span><span class="s">&quot;comp1, comp2&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  152. <span class="x">Heres comp1: </span><span class="cp">${</span><span class="n">comp1</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  153. <span class="x">Heres comp2: </span><span class="cp">${</span><span class="n">comp2</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  154. </pre></div>
  155. </div>
  156. <p><tt class="docutils literal"><span class="pre">import</span></tt> also supports the &#8220;<tt class="docutils literal"><span class="pre">*</span></tt>&#8221; operator:</p>
  157. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">file=</span><span class="s">&quot;components.html&quot;</span> <span class="na">import=</span><span class="s">&quot;*&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  158. <span class="x">Heres comp1: </span><span class="cp">${</span><span class="n">comp1</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  159. <span class="x">Heres comp2: </span><span class="cp">${</span><span class="n">comp2</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  160. </pre></div>
  161. </div>
  162. <p>The names imported by the <tt class="docutils literal"><span class="pre">import</span></tt> attribute take precedence
  163. over any names that exist within the current context.</p>
  164. <div class="admonition note">
  165. <p class="first admonition-title">Note</p>
  166. <p class="last">In current versions of Mako, usage of <tt class="docutils literal"><span class="pre">import='*'</span></tt> is
  167. known to decrease performance of the template. This will be
  168. fixed in a future release.</p>
  169. </div>
  170. <p>The <tt class="docutils literal"><span class="pre">file</span></tt> argument allows expressions &#8211; if looking for
  171. context variables, the <tt class="docutils literal"><span class="pre">context</span></tt> must be named explicitly:</p>
  172. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">name=</span><span class="s">&quot;dyn&quot;</span> <span class="na">file=</span><span class="s">&quot;${context[&#39;namespace_name&#39;]}&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  173. </pre></div>
  174. </div>
  175. <div class="section" id="ways-to-call-namespaces">
  176. <h2>Ways to Call Namespaces<a class="headerlink" href="#ways-to-call-namespaces" title="Permalink to this headline">¶</a></h2>
  177. <p>There are essentially four ways to call a function from a
  178. namespace.</p>
  179. <p>The &#8220;expression&#8221; format, as described previously. Namespaces are
  180. just Python objects with functions on them, and can be used in
  181. expressions like any other function:</p>
  182. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">${</span><span class="n">mynamespace</span><span class="o">.</span><span class="n">somefunction</span><span class="p">(</span><span class="s">&#39;some arg1&#39;</span><span class="p">,</span> <span class="s">&#39;some arg2&#39;</span><span class="p">,</span> <span class="n">arg3</span><span class="o">=</span><span class="s">&#39;some arg3&#39;</span><span class="p">,</span> <span class="n">arg4</span><span class="o">=</span><span class="s">&#39;some arg4&#39;</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  183. </pre></div>
  184. </div>
  185. <p>Synonymous with the &#8220;expression&#8221; format is the &#8220;custom tag&#8221;
  186. format, when a &#8220;closed&#8221; tag is used. This format, introduced in
  187. Mako 0.2.3, allows the usage of a &#8220;custom&#8221; Mako tag, with the
  188. function arguments passed in using named attributes:</p>
  189. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">mynamespace:somefunction</span> <span class="na">arg1=</span><span class="s">&quot;some arg1&quot;</span> <span class="na">arg2=</span><span class="s">&quot;some arg2&quot;</span> <span class="na">arg3=</span><span class="s">&quot;some arg3&quot;</span> <span class="na">arg4=</span><span class="s">&quot;some arg4&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  190. </pre></div>
  191. </div>
  192. <p>When using tags, the values of the arguments are taken as
  193. literal strings by default. To embed Python expressions as
  194. arguments, use the embedded expression format:</p>
  195. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">mynamespace:somefunction</span> <span class="na">arg1=</span><span class="s">&quot;${someobject.format()}&quot;</span> <span class="na">arg2=</span><span class="s">&quot;${somedef(5, 12)}&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  196. </pre></div>
  197. </div>
  198. <p>The &#8220;custom tag&#8221; format is intended mainly for namespace
  199. functions which recognize body content, which in Mako is known
  200. as a &#8220;def with embedded content&#8221;:</p>
  201. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">mynamespace:somefunction</span> <span class="na">arg1=</span><span class="s">&quot;some argument&quot;</span> <span class="na">args=</span><span class="s">&quot;x, y&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  202. <span class="x"> Some record: </span><span class="cp">${</span><span class="n">x</span><span class="cp">}</span><span class="x">, </span><span class="cp">${</span><span class="n">y</span><span class="cp">}</span><span class="x"></span>
  203. <span class="cp">&lt;/%</span><span class="nb">mynamespace:somefunction</span><span class="cp">&gt;</span><span class="x"></span>
  204. </pre></div>
  205. </div>
  206. <p>The &#8220;classic&#8221; way to call defs with embedded content is the <tt class="docutils literal"><span class="pre">&lt;%call&gt;</span></tt> tag:</p>
  207. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">call</span> <span class="na">expr=</span><span class="s">&quot;mynamespace.somefunction(arg1=&#39;some argument&#39;)&quot;</span> <span class="na">args=</span><span class="s">&quot;x, y&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  208. <span class="x"> Some record: </span><span class="cp">${</span><span class="n">x</span><span class="cp">}</span><span class="x">, </span><span class="cp">${</span><span class="n">y</span><span class="cp">}</span><span class="x"></span>
  209. <span class="cp">&lt;/%</span><span class="nb">call</span><span class="cp">&gt;</span><span class="x"></span>
  210. </pre></div>
  211. </div>
  212. <p>For information on how to construct defs that embed content from
  213. the caller, see <a class="reference internal" href="defs.html#defs-with-content"><em>Calling a Def with Embedded Content and/or Other Defs</em></a>.</p>
  214. </div>
  215. <div class="section" id="namespaces-from-regular-python-modules">
  216. <span id="namespaces-python-modules"></span><h2>Namespaces from Regular Python Modules<a class="headerlink" href="#namespaces-from-regular-python-modules" title="Permalink to this headline">¶</a></h2>
  217. <p>Namespaces can also import regular Python functions from
  218. modules. These callables need to take at least one argument,
  219. <tt class="docutils literal"><span class="pre">context</span></tt>, an instance of <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>. A module file
  220. <tt class="docutils literal"><span class="pre">some/module.py</span></tt> might contain the callable:</p>
  221. <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">my_tag</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
  222. <span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;hello world&quot;</span><span class="p">)</span>
  223. <span class="k">return</span> <span class="s">&#39;&#39;</span>
  224. </pre></div>
  225. </div>
  226. <p>A template can use this module via:</p>
  227. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">name=</span><span class="s">&quot;hw&quot;</span> <span class="na">module=</span><span class="s">&quot;some.module&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  228. <span class="cp">${</span><span class="n">hw</span><span class="o">.</span><span class="n">my_tag</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  229. </pre></div>
  230. </div>
  231. <p>Note that the <tt class="docutils literal"><span class="pre">context</span></tt> argument is not needed in the call;
  232. the <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> tag creates a locally-scoped callable which
  233. takes care of it. The <tt class="docutils literal"><span class="pre">return</span> <span class="pre">''</span></tt> is so that the def does not
  234. dump a <tt class="docutils literal"><span class="pre">None</span></tt> into the output stream &#8211; the return value of any
  235. def is rendered after the def completes, in addition to whatever
  236. was passed to <a class="reference internal" href="runtime.html#mako.runtime.Context.write" title="mako.runtime.Context.write"><tt class="xref py py-meth docutils literal"><span class="pre">Context.write()</span></tt></a> within its body.</p>
  237. <p>If your def is to be called in an &#8220;embedded content&#8221; context,
  238. that is as described in <a class="reference internal" href="defs.html#defs-with-content"><em>Calling a Def with Embedded Content and/or Other Defs</em></a>, you should use
  239. the <a class="reference internal" href="#mako.runtime.supports_caller" title="mako.runtime.supports_caller"><tt class="xref py py-func docutils literal"><span class="pre">supports_caller()</span></tt></a> decorator, which will ensure that Mako
  240. will ensure the correct &#8220;caller&#8221; variable is available when your
  241. def is called, supporting embedded content:</p>
  242. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.runtime</span> <span class="kn">import</span> <span class="n">supports_caller</span>
  243. <span class="nd">@supports_caller</span>
  244. <span class="k">def</span> <span class="nf">my_tag</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
  245. <span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&lt;div&gt;&quot;</span><span class="p">)</span>
  246. <span class="n">context</span><span class="p">[</span><span class="s">&#39;caller&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">body</span><span class="p">()</span>
  247. <span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&lt;/div&gt;&quot;</span><span class="p">)</span>
  248. <span class="k">return</span> <span class="s">&#39;&#39;</span>
  249. </pre></div>
  250. </div>
  251. <p>Capturing of output is available as well, using the
  252. outside-of-templates version of the <a class="reference internal" href="#mako.runtime.capture" title="mako.runtime.capture"><tt class="xref py py-func docutils literal"><span class="pre">capture()</span></tt></a> function,
  253. which accepts the &#8220;context&#8221; as its first argument:</p>
  254. <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.runtime</span> <span class="kn">import</span> <span class="n">supports_caller</span><span class="p">,</span> <span class="n">capture</span>
  255. <span class="nd">@supports_caller</span>
  256. <span class="k">def</span> <span class="nf">my_tag</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
  257. <span class="k">return</span> <span class="s">&quot;&lt;div&gt;</span><span class="si">%s</span><span class="s">&lt;/div&gt;&quot;</span> <span class="o">%</span> \
  258. <span class="n">capture</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">context</span><span class="p">[</span><span class="s">&#39;caller&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">body</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="s">&quot;bar&quot;</span><span class="p">)</span>
  259. </pre></div>
  260. </div>
  261. </div>
  262. <div class="section" id="declaring-defs-in-namespaces">
  263. <h2>Declaring Defs in Namespaces<a class="headerlink" href="#declaring-defs-in-namespaces" title="Permalink to this headline">¶</a></h2>
  264. <p>The <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag supports the definition of <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>s
  265. directly inside the tag. These defs become part of the namespace
  266. like any other function, and will override the definitions
  267. pulled in from a remote template or module:</p>
  268. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## define a namespace</span><span class="x"></span>
  269. <span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">name=</span><span class="s">&quot;stuff&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  270. <span class="x"> </span><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;comp1()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  271. <span class="x"> comp1</span>
  272. <span class="x"> </span><span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  273. <span class="cp">&lt;/%</span><span class="nb">namespace</span><span class="cp">&gt;</span>
  274. <span class="cp">## then call it</span><span class="x"></span>
  275. <span class="cp">${</span><span class="n">stuff</span><span class="o">.</span><span class="n">comp1</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  276. </pre></div>
  277. </div>
  278. </div>
  279. <div class="section" id="the-body-method">
  280. <span id="namespaces-body"></span><h2>The <tt class="docutils literal"><span class="pre">body()</span></tt> Method<a class="headerlink" href="#the-body-method" title="Permalink to this headline">¶</a></h2>
  281. <p>Every namespace that is generated from a template contains a
  282. method called <tt class="docutils literal"><span class="pre">body()</span></tt>. This method corresponds to the main
  283. body of the template, and plays its most important roles when
  284. using inheritance relationships as well as
  285. def-calls-with-content.</p>
  286. <p>Since the <tt class="docutils literal"><span class="pre">body()</span></tt> method is available from a namespace just
  287. like all the other defs defined in a template, what happens if
  288. you send arguments to it? By default, the <tt class="docutils literal"><span class="pre">body()</span></tt> method
  289. accepts no positional arguments, and for usefulness in
  290. inheritance scenarios will by default dump all keyword arguments
  291. into a dictionary called <tt class="docutils literal"><span class="pre">pageargs</span></tt>. But if you actually want
  292. to get at the keyword arguments, Mako recommends you define your
  293. own argument signature explicitly. You do this via using the
  294. <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> tag:</p>
  295. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">args=</span><span class="s">&quot;x, y, someval=8, scope=&#39;foo&#39;, **kwargs&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  296. </pre></div>
  297. </div>
  298. <p>A template which defines the above signature requires that the
  299. variables <tt class="docutils literal"><span class="pre">x</span></tt> and <tt class="docutils literal"><span class="pre">y</span></tt> are defined, defines default values
  300. for <tt class="docutils literal"><span class="pre">someval</span></tt> and <tt class="docutils literal"><span class="pre">scope</span></tt>, and sets up <tt class="docutils literal"><span class="pre">**kwargs</span></tt> to
  301. receive all other keyword arguments. If <tt class="docutils literal"><span class="pre">**kwargs</span></tt> or similar
  302. is not present, the argument <tt class="docutils literal"><span class="pre">**pageargs</span></tt> gets tacked on by
  303. Mako. When the template is called as a top-level template (i.e.
  304. via <a class="reference internal" href="usage.html#mako.template.Template.render" title="mako.template.Template.render"><tt class="xref py py-meth docutils literal"><span class="pre">render()</span></tt></a>) or via the <tt class="docutils literal"><span class="pre">&lt;%include&gt;</span></tt> tag, the
  305. values for these arguments will be pulled from the <tt class="docutils literal"><span class="pre">Context</span></tt>.
  306. In all other cases, i.e. via calling the <tt class="docutils literal"><span class="pre">body()</span></tt> method, the
  307. arguments are taken as ordinary arguments from the method call.
  308. So above, the body might be called as:</p>
  309. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">body</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">someval</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span> <span class="n">delta</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  310. </pre></div>
  311. </div>
  312. <p>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> object also supplies a <a class="reference internal" href="runtime.html#mako.runtime.Context.kwargs" title="mako.runtime.Context.kwargs"><tt class="xref py py-attr docutils literal"><span class="pre">kwargs</span></tt></a> accessor, for
  313. cases when you&#8217;d like to pass along whatever is in the context to
  314. a <tt class="docutils literal"><span class="pre">body()</span></tt> callable:</p>
  315. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">${</span><span class="nb">next</span><span class="o">.</span><span class="n">body</span><span class="p">(</span><span class="o">**</span><span class="n">context</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  316. </pre></div>
  317. </div>
  318. <p>The usefulness of calls like the above become more apparent when
  319. one works with inheriting templates. For more information on
  320. this, as well as the meanings of the names <tt class="docutils literal"><span class="pre">self</span></tt> and
  321. <tt class="docutils literal"><span class="pre">next</span></tt>, see <a class="reference internal" href="inheritance.html"><em>Inheritance</em></a>.</p>
  322. </div>
  323. <div class="section" id="built-in-namespaces">
  324. <span id="namespaces-builtin"></span><h2>Built-in Namespaces<a class="headerlink" href="#built-in-namespaces" title="Permalink to this headline">¶</a></h2>
  325. <p>The namespace is so great that Mako gives your template one (or
  326. two) for free. The names of these namespaces are <tt class="docutils literal"><span class="pre">local</span></tt> and
  327. <tt class="docutils literal"><span class="pre">self</span></tt>. Other built-in namespaces include <tt class="docutils literal"><span class="pre">parent</span></tt> and
  328. <tt class="docutils literal"><span class="pre">next</span></tt>, which are optional and are described in
  329. <a class="reference internal" href="inheritance.html"><em>Inheritance</em></a>.</p>
  330. <div class="section" id="local">
  331. <span id="namespace-local"></span><h3><tt class="docutils literal"><span class="pre">local</span></tt><a class="headerlink" href="#local" title="Permalink to this headline">¶</a></h3>
  332. <p>The <tt class="docutils literal"><span class="pre">local</span></tt> namespace is basically the namespace for the
  333. currently executing template. This means that all of the top
  334. level defs defined in your template, as well as your template&#8217;s
  335. <tt class="docutils literal"><span class="pre">body()</span></tt> function, are also available off of the <tt class="docutils literal"><span class="pre">local</span></tt>
  336. namespace.</p>
  337. <p>The <tt class="docutils literal"><span class="pre">local</span></tt> namespace is also where properties like <tt class="docutils literal"><span class="pre">uri</span></tt>,
  338. <tt class="docutils literal"><span class="pre">filename</span></tt>, and <tt class="docutils literal"><span class="pre">module</span></tt> and the <tt class="docutils literal"><span class="pre">get_namespace</span></tt> method
  339. can be particularly useful.</p>
  340. </div>
  341. <div class="section" id="self">
  342. <span id="namespace-self"></span><h3><tt class="docutils literal"><span class="pre">self</span></tt><a class="headerlink" href="#self" title="Permalink to this headline">¶</a></h3>
  343. <p>The <tt class="docutils literal"><span class="pre">self</span></tt> namespace, in the case of a template that does not
  344. use inheritance, is synonymous with <tt class="docutils literal"><span class="pre">local</span></tt>. If inheritance is
  345. used, then <tt class="docutils literal"><span class="pre">self</span></tt> references the topmost template in the
  346. inheritance chain, where it is most useful for providing the
  347. ultimate form of various &#8220;method&#8221; calls which may have been
  348. overridden at various points in an inheritance chain. See
  349. <a class="reference internal" href="inheritance.html"><em>Inheritance</em></a>.</p>
  350. </div>
  351. </div>
  352. <div class="section" id="inheritable-namespaces">
  353. <h2>Inheritable Namespaces<a class="headerlink" href="#inheritable-namespaces" title="Permalink to this headline">¶</a></h2>
  354. <p>The <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag includes an optional attribute
  355. <tt class="docutils literal"><span class="pre">inheritable=&quot;True&quot;</span></tt>, which will cause the namespace to be
  356. attached to the <tt class="docutils literal"><span class="pre">self</span></tt> namespace. Since <tt class="docutils literal"><span class="pre">self</span></tt> is globally
  357. available throughout an inheritance chain (described in the next
  358. section), all the templates in an inheritance chain can get at
  359. the namespace imported in a super-template via <tt class="docutils literal"><span class="pre">self</span></tt>.</p>
  360. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## base.html</span><span class="x"></span>
  361. <span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">name=</span><span class="s">&quot;foo&quot;</span> <span class="na">file=</span><span class="s">&quot;foo.html&quot;</span> <span class="na">inheritable=</span><span class="s">&quot;True&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  362. <span class="cp">${</span><span class="nb">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span>
  363. <span class="cp">## somefile.html</span><span class="x"></span>
  364. <span class="cp">&lt;%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">&quot;base.html&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  365. <span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">bar</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  366. </pre></div>
  367. </div>
  368. <p>This allows a super-template to load a whole bunch of namespaces
  369. that its inheriting templates can get to, without them having to
  370. explicitly load those namespaces themselves.</p>
  371. <p>The <tt class="docutils literal"><span class="pre">import=&quot;*&quot;</span></tt> part of the <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag doesn&#8217;t yet
  372. interact with the <tt class="docutils literal"><span class="pre">inheritable</span></tt> flag, so currently you have to
  373. use the explicit namespace name off of <tt class="docutils literal"><span class="pre">self</span></tt>, followed by the
  374. desired function name. But more on this in a future release.</p>
  375. </div>
  376. <div class="section" id="namespace-api-usage-example-static-dependencies">
  377. <h2>Namespace API Usage Example - Static Dependencies<a class="headerlink" href="#namespace-api-usage-example-static-dependencies" title="Permalink to this headline">¶</a></h2>
  378. <p>The <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag at runtime produces an instance of
  379. <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>. Programmatic access of <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> can be used
  380. to build various kinds of scaffolding in templates and between templates.</p>
  381. <p>A common request is the ability for a particular template to declare
  382. &#8220;static includes&#8221; - meaning, the usage of a particular set of defs requires
  383. that certain Javascript/CSS files are present. Using <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> as the
  384. object that holds together the various templates present, we can build a variety
  385. of such schemes. In particular, 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> has a <tt class="docutils literal"><span class="pre">namespaces</span></tt>
  386. attribute, which is a dictionary of all <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> objects declared.
  387. Iterating the values of this dictionary will provide a <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>
  388. object for each time the <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag was used, anywhere within the
  389. inheritance chain.</p>
  390. <div class="section" id="version-one-use-namespace-attr">
  391. <span id="namespace-attr-for-includes"></span><h3>Version One - Use <a class="reference internal" href="#mako.runtime.Namespace.attr" title="mako.runtime.Namespace.attr"><tt class="xref py py-attr docutils literal"><span class="pre">Namespace.attr</span></tt></a><a class="headerlink" href="#version-one-use-namespace-attr" title="Permalink to this headline">¶</a></h3>
  392. <p>The <a class="reference internal" href="#mako.runtime.Namespace.attr" title="mako.runtime.Namespace.attr"><tt class="xref py py-attr docutils literal"><span class="pre">Namespace.attr</span></tt></a> attribute allows us to locate any variables declared
  393. in the <tt class="docutils literal"><span class="pre">&lt;%!</span> <span class="pre">%&gt;</span></tt> of a template.</p>
  394. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## base.mako</span><span class="x"></span>
  395. <span class="cp">## base-most template, renders layout etc.</span><span class="x"></span>
  396. <span class="x">&lt;html&gt;</span>
  397. <span class="x">&lt;head&gt;</span>
  398. <span class="cp">## traverse through all namespaces present,</span><span class="x"></span>
  399. <span class="cp">## look for an attribute named &#39;includes&#39;</span><span class="x"></span>
  400. <span class="cp">%</span> <span class="k">for</span> <span class="n">ns</span> <span class="ow">in</span> <span class="n">context</span><span class="o">.</span><span class="n">namespaces</span><span class="o">.</span><span class="n">values</span><span class="p">():</span><span class="x"></span>
  401. <span class="cp">%</span> <span class="k">for</span> <span class="n">incl</span> <span class="ow">in</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">ns</span><span class="o">.</span><span class="n">attr</span><span class="p">,</span> <span class="s">&#39;includes&#39;</span><span class="p">,</span> <span class="p">[]):</span><span class="x"></span>
  402. <span class="x"> </span><span class="cp">${</span><span class="n">incl</span><span class="cp">}</span>
  403. <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
  404. <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
  405. <span class="x">&lt;/head&gt;</span>
  406. <span class="x">&lt;body&gt;</span>
  407. <span class="cp">${</span><span class="nb">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  408. <span class="x">&lt;/body</span>
  409. <span class="x">&lt;/html&gt;</span>
  410. <span class="cp">## library.mako</span><span class="x"></span>
  411. <span class="cp">## library functions.</span><span class="x"></span>
  412. <span class="cp">&lt;%!</span>
  413. <span class="n">includes</span> <span class="o">=</span> <span class="p">[</span>
  414. <span class="s">&#39;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;mystyle.css&quot;/&gt;&#39;</span><span class="p">,</span>
  415. <span class="s">&#39;&lt;script type=&quot;text/javascript&quot; src=&quot;functions.js&quot;&gt;&lt;/script&gt;&#39;</span>
  416. <span class="p">]</span>
  417. <span class="cp">%&gt;</span><span class="x"></span>
  418. <span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;mytag()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  419. <span class="x"> &lt;form&gt;</span>
  420. <span class="x"> </span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  421. <span class="x"> &lt;/form&gt;</span>
  422. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
  423. <span class="cp">## index.mako</span><span class="x"></span>
  424. <span class="cp">## calling template.</span><span class="x"></span>
  425. <span class="cp">&lt;%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">&quot;base.mako&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  426. <span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">name=</span><span class="s">&quot;foo&quot;</span> <span class="na">file=</span><span class="s">&quot;library.mako&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  427. <span class="cp">&lt;%</span><span class="nb">foo:mytag</span><span class="cp">&gt;</span><span class="x"></span>
  428. <span class="x"> a form</span>
  429. <span class="cp">&lt;/%</span><span class="nb">foo:mytag</span><span class="cp">&gt;</span><span class="x"></span>
  430. </pre></div>
  431. </div>
  432. <p>Above, the file <tt class="docutils literal"><span class="pre">library.mako</span></tt> declares an attribute <tt class="docutils literal"><span class="pre">includes</span></tt> inside its global <tt class="docutils literal"><span class="pre">&lt;%!</span> <span class="pre">%&gt;</span></tt> section.
  433. <tt class="docutils literal"><span class="pre">index.mako</span></tt> includes this template using the <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag. The base template <tt class="docutils literal"><span class="pre">base.mako</span></tt>, which is the inherited parent of <tt class="docutils literal"><span class="pre">index.mako</span></tt> and is reponsible for layout, then locates this attribute and iterates through its contents to produce the includes that are specific to <tt class="docutils literal"><span class="pre">library.mako</span></tt>.</p>
  434. </div>
  435. <div class="section" id="version-two-use-a-specific-named-def">
  436. <h3>Version Two - Use a specific named def<a class="headerlink" href="#version-two-use-a-specific-named-def" title="Permalink to this headline">¶</a></h3>
  437. <p>In this version, we put the includes into a <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> that
  438. follows a naming convention.</p>
  439. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## base.mako</span><span class="x"></span>
  440. <span class="cp">## base-most template, renders layout etc.</span><span class="x"></span>
  441. <span class="x">&lt;html&gt;</span>
  442. <span class="x">&lt;head&gt;</span>
  443. <span class="cp">## traverse through all namespaces present,</span><span class="x"></span>
  444. <span class="cp">## look for a %def named &#39;includes&#39;</span><span class="x"></span>
  445. <span class="cp">%</span> <span class="k">for</span> <span class="n">ns</span> <span class="ow">in</span> <span class="n">context</span><span class="o">.</span><span class="n">namespaces</span><span class="o">.</span><span class="n">values</span><span class="p">():</span><span class="x"></span>
  446. <span class="cp">%</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">ns</span><span class="p">,</span> <span class="s">&#39;includes&#39;</span><span class="p">):</span><span class="x"></span>
  447. <span class="x"> </span><span class="cp">${</span><span class="n">ns</span><span class="o">.</span><span class="n">includes</span><span class="p">()</span><span class="cp">}</span>
  448. <span class="cp">%</span><span class="k"> endif</span><span class="x"></span>
  449. <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
  450. <span class="x">&lt;/head&gt;</span>
  451. <span class="x">&lt;body&gt;</span>
  452. <span class="cp">${</span><span class="nb">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  453. <span class="x">&lt;/body</span>
  454. <span class="x">&lt;/html&gt;</span>
  455. <span class="cp">## library.mako</span><span class="x"></span>
  456. <span class="cp">## library functions.</span><span class="x"></span>
  457. <span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;includes()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  458. <span class="x"> &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;mystyle.css&quot;/&gt;</span>
  459. <span class="x"> &lt;script type=&quot;text/javascript&quot; src=&quot;functions.js&quot;&gt;&lt;/script&gt;</span>
  460. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  461. <span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;mytag()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  462. <span class="x"> &lt;form&gt;</span>
  463. <span class="x"> </span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
  464. <span class="x"> &lt;/form&gt;</span>
  465. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
  466. <span class="cp">## index.mako</span><span class="x"></span>
  467. <span class="cp">## calling template.</span><span class="x"></span>
  468. <span class="cp">&lt;%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">&quot;base.mako&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  469. <span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">name=</span><span class="s">&quot;foo&quot;</span> <span class="na">file=</span><span class="s">&quot;library.mako&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  470. <span class="cp">&lt;%</span><span class="nb">foo:mytag</span><span class="cp">&gt;</span><span class="x"></span>
  471. <span class="x"> a form</span>
  472. <span class="cp">&lt;/%</span><span class="nb">foo:mytag</span><span class="cp">&gt;</span><span class="x"></span>
  473. </pre></div>
  474. </div>
  475. <p>In this version, <tt class="docutils literal"><span class="pre">library.mako</span></tt> declares a <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> named <tt class="docutils literal"><span class="pre">includes</span></tt>. The example works
  476. identically to the previous one, except that <tt class="docutils literal"><span class="pre">base.mako</span></tt> looks for defs named <tt class="docutils literal"><span class="pre">include</span></tt>
  477. on each namespace it examines.</p>
  478. </div>
  479. </div>
  480. <div class="section" id="api-reference">
  481. <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h2>
  482. <dl class="class">
  483. <dt id="mako.runtime.Namespace">
  484. <em class="property">class </em><tt class="descclassname">mako.runtime.</tt><tt class="descname">Namespace</tt><big>(</big><em>name</em>, <em>context</em>, <em>callables=None</em>, <em>inherits=None</em>, <em>populate_self=True</em>, <em>calling_uri=None</em><big>)</big><a class="headerlink" href="#mako.runtime.Namespace" title="Permalink to this definition">¶</a></dt>
  485. <dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
  486. <p>Provides access to collections of rendering methods, which
  487. can be local, from other templates, or from imported modules.</p>
  488. <p>To access a particular rendering method referenced by a
  489. <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>, use plain attribute access:</p>
  490. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">${</span><span class="n">some_namespace</span><span class="o">.</span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  491. </pre></div>
  492. </div>
  493. <p><a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> also contains several built-in attributes
  494. described here.</p>
  495. <dl class="attribute">
  496. <dt id="mako.runtime.Namespace.attr">
  497. <tt class="descname">attr</tt><a class="headerlink" href="#mako.runtime.Namespace.attr" title="Permalink to this definition">¶</a></dt>
  498. <dd><p>Access module level attributes by name.</p>
  499. <p>This accessor allows templates to supply &#8220;scalar&#8221;
  500. attributes which are particularly handy in inheritance
  501. relationships.</p>
  502. <div class="admonition-see-also admonition seealso">
  503. <p class="first admonition-title">See also</p>
  504. <p><a class="reference internal" href="inheritance.html#inheritance-attr"><em>Inheritable Attributes</em></a></p>
  505. <p class="last"><a class="reference internal" href="#namespace-attr-for-includes"><em>Version One - Use Namespace.attr</em></a></p>
  506. </div>
  507. </dd></dl>
  508. <dl class="attribute">
  509. <dt id="mako.runtime.Namespace.cache">
  510. <tt class="descname">cache</tt><a class="headerlink" href="#mako.runtime.Namespace.cache" title="Permalink to this definition">¶</a></dt>
  511. <dd><p>Return the <a class="reference internal" href="caching.html#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a> object referenced
  512. by this <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> object&#8217;s
  513. <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>.</p>
  514. </dd></dl>
  515. <dl class="attribute">
  516. <dt id="mako.runtime.Namespace.context">
  517. <tt class="descname">context</tt><em class="property"> = None</em><a class="headerlink" href="#mako.runtime.Namespace.context" title="Permalink to this definition">¶</a></dt>
  518. <dd><p>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> object for this <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>.</p>
  519. <p>Namespaces are often created with copies of contexts that
  520. contain slightly different data, particularly in inheritance
  521. scenarios. Using 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> off of a <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> one
  522. can traverse an entire chain of templates that inherit from
  523. one-another.</p>
  524. </dd></dl>
  525. <dl class="attribute">
  526. <dt id="mako.runtime.Namespace.filename">
  527. <tt class="descname">filename</tt><em class="property"> = None</em><a class="headerlink" href="#mako.runtime.Namespace.filename" title="Permalink to this definition">¶</a></dt>
  528. <dd><p>The path of the filesystem file used for this
  529. <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>&#8216;s module or template.</p>
  530. <p>If this is a pure module-based
  531. <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>, this evaluates to <tt class="docutils literal"><span class="pre">module.__file__</span></tt>. If a
  532. template-based namespace, it evaluates to the original
  533. template file location.</p>
  534. </dd></dl>
  535. <dl class="method">
  536. <dt id="mako.runtime.Namespace.get_cached">
  537. <tt class="descname">get_cached</tt><big>(</big><em>key</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mako.runtime.Namespace.get_cached" title="Permalink to this definition">¶</a></dt>
  538. <dd><p>Return a value from the <a class="reference internal" href="caching.html#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a> referenced by this
  539. <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> object&#8217;s <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>.</p>
  540. <p>The advantage to this method versus direct access to the
  541. <a class="reference internal" href="caching.html#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a> is that the configuration parameters
  542. declared in <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> take effect here, thereby calling
  543. up the same configured backend as that configured
  544. by <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt>.</p>
  545. </dd></dl>
  546. <dl class="method">
  547. <dt id="mako.runtime.Namespace.get_namespace">
  548. <tt class="descname">get_namespace</tt><big>(</big><em>uri</em><big>)</big><a class="headerlink" href="#mako.runtime.Namespace.get_namespace" title="Permalink to this definition">¶</a></dt>
  549. <dd><p>Return a <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> corresponding to the given <tt class="docutils literal"><span class="pre">uri</span></tt>.</p>
  550. <p>If the given <tt class="docutils literal"><span class="pre">uri</span></tt> is a relative URI (i.e. it does not
  551. contain a leading slash <tt class="docutils literal"><span class="pre">/</span></tt>), the <tt class="docutils literal"><span class="pre">uri</span></tt> is adjusted to
  552. be relative to the <tt class="docutils literal"><span class="pre">uri</span></tt> of the namespace itself. This
  553. method is therefore mostly useful off of the built-in
  554. <tt class="docutils literal"><span class="pre">local</span></tt> namespace, described in <a class="reference internal" href="#namespace-local"><em>local</em></a>.</p>
  555. <p>In
  556. most cases, a template wouldn&#8217;t need this function, and
  557. should instead use the <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag to load
  558. namespaces. However, since all <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tags are
  559. evaluated before the body of a template ever runs,
  560. this method can be used to locate namespaces using
  561. expressions that were generated within the body code of
  562. the template, or to conditionally use a particular
  563. namespace.</p>
  564. </dd></dl>
  565. <dl class="method">
  566. <dt id="mako.runtime.Namespace.get_template">
  567. <tt class="descname">get_template</tt><big>(</big><em>uri</em><big>)</big><a class="headerlink" href="#mako.runtime.Namespace.get_template" title="Permalink to this definition">¶</a></dt>
  568. <dd><p>Return a <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> from the given <tt class="docutils literal"><span class="pre">uri</span></tt>.</p>
  569. <p>The <tt class="docutils literal"><span class="pre">uri</span></tt> resolution is relative to the <tt class="docutils literal"><span class="pre">uri</span></tt> of this <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>
  570. object&#8217;s <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>.</p>
  571. </dd></dl>
  572. <dl class="method">
  573. <dt id="mako.runtime.Namespace.include_file">
  574. <tt class="descname">include_file</tt><big>(</big><em>uri</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mako.runtime.Namespace.include_file" title="Permalink to this definition">¶</a></dt>
  575. <dd><p>Include a file at the given <tt class="docutils literal"><span class="pre">uri</span></tt>.</p>
  576. </dd></dl>
  577. <dl class="attribute">
  578. <dt id="mako.runtime.Namespace.module">
  579. <tt class="descname">module</tt><em class="property"> = None</em><a class="headerlink" href="#mako.runtime.Namespace.module" title="Permalink to this definition">¶</a></dt>
  580. <dd><p>The Python module referenced by this <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>.</p>
  581. <p>If the namespace references a <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>, then
  582. this module is the equivalent of <tt class="docutils literal"><span class="pre">template.module</span></tt>,
  583. i.e. the generated module for the template.</p>
  584. </dd></dl>
  585. <dl class="attribute">
  586. <dt id="mako.runtime.Namespace.template">
  587. <tt class="descname">template</tt><em class="property"> = None</em><a class="headerlink" href="#mako.runtime.Namespace.template" title="Permalink to this definition">¶</a></dt>
  588. <dd><p>The <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object referenced by this
  589. <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>, if any.</p>
  590. </dd></dl>
  591. <dl class="attribute">
  592. <dt id="mako.runtime.Namespace.uri">
  593. <tt class="descname">uri</tt><em class="property"> = None</em><a class="headerlink" href="#mako.runtime.Namespace.uri" title="Permalink to this definition">¶</a></dt>
  594. <dd><p>The URI for this <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>&#8216;s template.</p>
  595. <p>I.e. whatever was sent to <a class="reference internal" href="usage.html#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>.</p>
  596. <p>This is the equivalent of <tt class="xref py py-attr docutils literal"><span class="pre">Template.uri</span></tt>.</p>
  597. </dd></dl>
  598. </dd></dl>
  599. <dl class="class">
  600. <dt id="mako.runtime.TemplateNamespace">
  601. <em class="property">class </em><tt class="descclassname">mako.runtime.</tt><tt class="descname">TemplateNamespace</tt><big>(</big><em>name</em>, <em>context</em>, <em>template=None</em>, <em>templateuri=None</em>, <em>callables=None</em>, <em>inherits=None</em>, <em>populate_self=True</em>, <em>calling_uri=None</em><big>)</big><a class="headerlink" href="#mako.runtime.TemplateNamespace" title="Permalink to this definition">¶</a></dt>
  602. <dd><p>Bases: <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">mako.runtime.Namespace</span></tt></a></p>
  603. <p>A <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> specific to a <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> instance.</p>
  604. <dl class="attribute">
  605. <dt id="mako.runtime.TemplateNamespace.filename">
  606. <tt class="descname">filename</tt><a class="headerlink" href="#mako.runtime.TemplateNamespace.filename" title="Permalink to this definition">¶</a></dt>
  607. <dd><p>The path of the filesystem file used for this
  608. <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>&#8216;s module or template.</p>
  609. </dd></dl>
  610. <dl class="attribute">
  611. <dt id="mako.runtime.TemplateNamespace.module">
  612. <tt class="descname">module</tt><a class="headerlink" href="#mako.runtime.TemplateNamespace.module" title="Permalink to this definition">¶</a></dt>
  613. <dd><p>The Python module referenced by this <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>.</p>
  614. <p>If the namespace references a <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>, then
  615. this module is the equivalent of <tt class="docutils literal"><span class="pre">template.module</span></tt>,
  616. i.e. the generated module for the template.</p>
  617. </dd></dl>
  618. <dl class="attribute">
  619. <dt id="mako.runtime.TemplateNamespace.uri">
  620. <tt class="descname">uri</tt><a class="headerlink" href="#mako.runtime.TemplateNamespace.uri" title="Permalink to this definition">¶</a></dt>
  621. <dd><p>The URI for this <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>&#8216;s template.</p>
  622. <p>I.e. whatever was sent to <a class="reference internal" href="usage.html#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>.</p>
  623. <p>This is the equivalent of <tt class="xref py py-attr docutils literal"><span class="pre">Template.uri</span></tt>.</p>
  624. </dd></dl>
  625. </dd></dl>
  626. <dl class="class">
  627. <dt id="mako.runtime.ModuleNamespace">
  628. <em class="property">class </em><tt class="descclassname">mako.runtime.</tt><tt class="descname">ModuleNamespace</tt><big>(</big><em>name</em>, <em>context</em>, <em>module</em>, <em>callables=None</em>, <em>inherits=None</em>, <em>populate_self=True</em>, <em>calling_uri=None</em><big>)</big><a class="headerlink" href="#mako.runtime.ModuleNamespace" title="Permalink to this definition">¶</a></dt>
  629. <dd><p>Bases: <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">mako.runtime.Namespace</span></tt></a></p>
  630. <p>A <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> specific to a Python module instance.</p>
  631. <dl class="attribute">
  632. <dt id="mako.runtime.ModuleNamespace.filename">
  633. <tt class="descname">filename</tt><a class="headerlink" href="#mako.runtime.ModuleNamespace.filename" title="Permalink to this definition">¶</a></dt>
  634. <dd><p>The path of the filesystem file used for this
  635. <a class="reference internal" href="#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>&#8216;s module or template.</p>
  636. </dd></dl>
  637. </dd></dl>
  638. <dl class="function">
  639. <dt id="mako.runtime.supports_caller">
  640. <tt class="descclassname">mako.runtime.</tt><tt class="descname">supports_caller</tt><big>(</big><em>func</em><big>)</big><a class="headerlink" href="#mako.runtime.supports_caller" title="Permalink to this definition">¶</a></dt>
  641. <dd><p>Apply a caller_stack compatibility decorator to a plain
  642. Python function.</p>
  643. <p>See the example in <a class="reference internal" href="#namespaces-python-modules"><em>Namespaces from Regular Python Modules</em></a>.</p>
  644. </dd></dl>
  645. <dl class="function">
  646. <dt id="mako.runtime.capture">
  647. <tt class="descclassname">mako.runtime.</tt><tt class="descname">capture</tt><big>(</big><em>context</em>, <em>callable_</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mako.runtime.capture" title="Permalink to this definition">¶</a></dt>
  648. <dd><p>Execute the given template def, capturing the output into
  649. a buffer.</p>
  650. <p>See the example in <a class="reference internal" href="#namespaces-python-modules"><em>Namespaces from Regular Python Modules</em></a>.</p>
  651. </dd></dl>
  652. </div>
  653. </div>
  654. </div>
  655. </div>
  656. <div id="docs-bottom-navigation" class="docs-navigation-links">
  657. Previous:
  658. <a href="runtime.html" title="previous chapter">The Mako Runtime Environment</a>
  659. Next:
  660. <a href="inheritance.html" title="next chapter">Inheritance</a>
  661. <div id="docs-copyright">
  662. &copy; Copyright the Mako authors and contributors.
  663. Documentation generated using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3
  664. with Mako templates.
  665. </div>
  666. </div>
  667. </div>
  668. <div class="clearfix">
  669. <hr/>
  670. <div class="copyright">Website content copyright &copy; by Michael Bayer.
  671. All rights reserved. Mako and its documentation are licensed
  672. under the MIT license. mike(&)zzzcomputing.com</div>
  673. </div>
  674. </div>
  675. </body>
  676. </html>