namespaces.html 66 KB

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