namespaces.html 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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.7.2 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.7.2',
  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.7.2 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.7.2 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.7.2</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.7.2 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="#api-reference">API Reference</a></li>
  95. </ul>
  96. </li>
  97. </ul>
  98. <h4>Previous Topic</h4>
  99. <p>
  100. <a href="runtime.html" title="previous chapter">The Mako Runtime Environment</a>
  101. </p>
  102. <h4>Next Topic</h4>
  103. <p>
  104. <a href="inheritance.html" title="next chapter">Inheritance</a>
  105. </p>
  106. <h4>Quick Search</h4>
  107. <p>
  108. <form class="search" action="search.html" method="get">
  109. <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
  110. <input type="hidden" name="check_keywords" value="yes" />
  111. <input type="hidden" name="area" value="default" />
  112. </form>
  113. </p>
  114. </div>
  115. <div id="docs-body" class="withsidebar" >
  116. <div class="section" id="namespaces">
  117. <span id="namespaces-toplevel"></span><h1>Namespaces<a class="headerlink" href="#namespaces" title="Permalink to this headline">¶</a></h1>
  118. <p>Namespaces are used to organize groups of defs into
  119. categories, and also to &#8220;import&#8221; defs from other files.</p>
  120. <p>If the file <tt class="docutils literal"><span class="pre">components.html</span></tt> defines these two defs:</p>
  121. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## components.html</span><span class="x"></span>
  122. <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>
  123. <span class="x"> this is comp1</span>
  124. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  125. <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>
  126. <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>
  127. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  128. </pre></div>
  129. </div>
  130. <p>you can make another file, for example <tt class="docutils literal"><span class="pre">index.html</span></tt>, that
  131. pulls those two defs into a namespace called <tt class="docutils literal"><span class="pre">comp</span></tt>:</p>
  132. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## index.html</span><span class="x"></span>
  133. <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>
  134. <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>
  135. <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>
  136. </pre></div>
  137. </div>
  138. <p>The <tt class="docutils literal"><span class="pre">comp</span></tt> variable above is an instance of
  139. <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
  140. method calls to the underlying template callable using the
  141. current context.</p>
  142. <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
  143. be used to pull the names into the local namespace, removing the
  144. 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
  145. <tt class="docutils literal"><span class="pre">name</span></tt> attribute is optional.</p>
  146. <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>
  147. <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>
  148. <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>
  149. </pre></div>
  150. </div>
  151. <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>
  152. <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>
  153. <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>
  154. <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>
  155. </pre></div>
  156. </div>
  157. <p>The names imported by the <tt class="docutils literal"><span class="pre">import</span></tt> attribute take precedence
  158. over any names that exist within the current context.</p>
  159. <div class="admonition note">
  160. <p class="first admonition-title">Note</p>
  161. <p class="last">In current versions of Mako, usage of <tt class="docutils literal"><span class="pre">import='*'</span></tt> is
  162. known to decrease performance of the template. This will be
  163. fixed in a future release.</p>
  164. </div>
  165. <p>The <tt class="docutils literal"><span class="pre">file</span></tt> argument allows expressions &#8211; if looking for
  166. context variables, the <tt class="docutils literal"><span class="pre">context</span></tt> must be named explicitly:</p>
  167. <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>
  168. </pre></div>
  169. </div>
  170. <div class="section" id="ways-to-call-namespaces">
  171. <h2>Ways to Call Namespaces<a class="headerlink" href="#ways-to-call-namespaces" title="Permalink to this headline">¶</a></h2>
  172. <p>There are essentially four ways to call a function from a
  173. namespace.</p>
  174. <p>The &#8220;expression&#8221; format, as described previously. Namespaces are
  175. just Python objects with functions on them, and can be used in
  176. expressions like any other function:</p>
  177. <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>
  178. </pre></div>
  179. </div>
  180. <p>Synonymous with the &#8220;expression&#8221; format is the &#8220;custom tag&#8221;
  181. format, when a &#8220;closed&#8221; tag is used. This format, introduced in
  182. Mako 0.2.3, allows the usage of a &#8220;custom&#8221; Mako tag, with the
  183. function arguments passed in using named attributes:</p>
  184. <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>
  185. </pre></div>
  186. </div>
  187. <p>When using tags, the values of the arguments are taken as
  188. literal strings by default. To embed Python expressions as
  189. arguments, use the embedded expression format:</p>
  190. <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>
  191. </pre></div>
  192. </div>
  193. <p>The &#8220;custom tag&#8221; format is intended mainly for namespace
  194. functions which recognize body content, which in Mako is known
  195. as a &#8220;def with embedded content&#8221;:</p>
  196. <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>
  197. <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>
  198. <span class="cp">&lt;/%</span><span class="nb">mynamespace:somefunction</span><span class="cp">&gt;</span><span class="x"></span>
  199. </pre></div>
  200. </div>
  201. <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>
  202. <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>
  203. <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>
  204. <span class="cp">&lt;/%</span><span class="nb">call</span><span class="cp">&gt;</span><span class="x"></span>
  205. </pre></div>
  206. </div>
  207. <p>For information on how to construct defs that embed content from
  208. 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>
  209. </div>
  210. <div class="section" id="namespaces-from-regular-python-modules">
  211. <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>
  212. <p>Namespaces can also import regular Python functions from
  213. modules. These callables need to take at least one argument,
  214. <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
  215. <tt class="docutils literal"><span class="pre">some/module.py</span></tt> might contain the callable:</p>
  216. <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>
  217. <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>
  218. <span class="k">return</span> <span class="s">&#39;&#39;</span>
  219. </pre></div>
  220. </div>
  221. <p>A template can use this module via:</p>
  222. <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>
  223. <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>
  224. </pre></div>
  225. </div>
  226. <p>Note that the <tt class="docutils literal"><span class="pre">context</span></tt> argument is not needed in the call;
  227. 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
  228. 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
  229. dump a <tt class="docutils literal"><span class="pre">None</span></tt> into the output stream &#8211; the return value of any
  230. def is rendered after the def completes, in addition to whatever
  231. 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>
  232. <p>If your def is to be called in an &#8220;embedded content&#8221; context,
  233. 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
  234. 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
  235. will ensure the correct &#8220;caller&#8221; variable is available when your
  236. def is called, supporting embedded content:</p>
  237. <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>
  238. <span class="nd">@supports_caller</span>
  239. <span class="k">def</span> <span class="nf">my_tag</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
  240. <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>
  241. <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>
  242. <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>
  243. <span class="k">return</span> <span class="s">&#39;&#39;</span>
  244. </pre></div>
  245. </div>
  246. <p>Capturing of output is available as well, using the
  247. 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,
  248. which accepts the &#8220;context&#8221; as its first argument:</p>
  249. <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>
  250. <span class="nd">@supports_caller</span>
  251. <span class="k">def</span> <span class="nf">my_tag</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
  252. <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> \
  253. <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>
  254. </pre></div>
  255. </div>
  256. </div>
  257. <div class="section" id="declaring-defs-in-namespaces">
  258. <h2>Declaring Defs in Namespaces<a class="headerlink" href="#declaring-defs-in-namespaces" title="Permalink to this headline">¶</a></h2>
  259. <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
  260. directly inside the tag. These defs become part of the namespace
  261. like any other function, and will override the definitions
  262. pulled in from a remote template or module:</p>
  263. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## define a namespace</span><span class="x"></span>
  264. <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>
  265. <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>
  266. <span class="x"> comp1</span>
  267. <span class="x"> </span><span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  268. <span class="cp">&lt;/%</span><span class="nb">namespace</span><span class="cp">&gt;</span>
  269. <span class="cp">## then call it</span><span class="x"></span>
  270. <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>
  271. </pre></div>
  272. </div>
  273. </div>
  274. <div class="section" id="the-body-method">
  275. <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>
  276. <p>Every namespace that is generated from a template contains a
  277. method called <tt class="docutils literal"><span class="pre">body()</span></tt>. This method corresponds to the main
  278. body of the template, and plays its most important roles when
  279. using inheritance relationships as well as
  280. def-calls-with-content.</p>
  281. <p>Since the <tt class="docutils literal"><span class="pre">body()</span></tt> method is available from a namespace just
  282. like all the other defs defined in a template, what happens if
  283. you send arguments to it? By default, the <tt class="docutils literal"><span class="pre">body()</span></tt> method
  284. accepts no positional arguments, and for usefulness in
  285. inheritance scenarios will by default dump all keyword arguments
  286. into a dictionary called <tt class="docutils literal"><span class="pre">pageargs</span></tt>. But if you actually want
  287. to get at the keyword arguments, Mako recommends you define your
  288. own argument signature explicitly. You do this via using the
  289. <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> tag:</p>
  290. <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>
  291. </pre></div>
  292. </div>
  293. <p>A template which defines the above signature requires that the
  294. 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
  295. 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
  296. receive all other keyword arguments. If <tt class="docutils literal"><span class="pre">**kwargs</span></tt> or similar
  297. is not present, the argument <tt class="docutils literal"><span class="pre">**pageargs</span></tt> gets tacked on by
  298. Mako. When the template is called as a top-level template (i.e.
  299. 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
  300. values for these arguments will be pulled from the <tt class="docutils literal"><span class="pre">Context</span></tt>.
  301. In all other cases, i.e. via calling the <tt class="docutils literal"><span class="pre">body()</span></tt> method, the
  302. arguments are taken as ordinary arguments from the method call.
  303. So above, the body might be called as:</p>
  304. <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>
  305. </pre></div>
  306. </div>
  307. <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
  308. cases when you&#8217;d like to pass along whatever is in the context to
  309. a <tt class="docutils literal"><span class="pre">body()</span></tt> callable:</p>
  310. <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>
  311. </pre></div>
  312. </div>
  313. <p>The usefulness of calls like the above become more apparent when
  314. one works with inheriting templates. For more information on
  315. this, as well as the meanings of the names <tt class="docutils literal"><span class="pre">self</span></tt> and
  316. <tt class="docutils literal"><span class="pre">next</span></tt>, see <a class="reference internal" href="inheritance.html"><em>Inheritance</em></a>.</p>
  317. </div>
  318. <div class="section" id="built-in-namespaces">
  319. <span id="namespaces-builtin"></span><h2>Built-in Namespaces<a class="headerlink" href="#built-in-namespaces" title="Permalink to this headline">¶</a></h2>
  320. <p>The namespace is so great that Mako gives your template one (or
  321. two) for free. The names of these namespaces are <tt class="docutils literal"><span class="pre">local</span></tt> and
  322. <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
  323. <tt class="docutils literal"><span class="pre">next</span></tt>, which are optional and are described in
  324. <a class="reference internal" href="inheritance.html"><em>Inheritance</em></a>.</p>
  325. <div class="section" id="local">
  326. <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>
  327. <p>The <tt class="docutils literal"><span class="pre">local</span></tt> namespace is basically the namespace for the
  328. currently executing template. This means that all of the top
  329. level defs defined in your template, as well as your template&#8217;s
  330. <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>
  331. namespace.</p>
  332. <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>,
  333. <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
  334. can be particularly useful.</p>
  335. </div>
  336. <div class="section" id="self">
  337. <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>
  338. <p>The <tt class="docutils literal"><span class="pre">self</span></tt> namespace, in the case of a template that does not
  339. use inheritance, is synonymous with <tt class="docutils literal"><span class="pre">local</span></tt>. If inheritance is
  340. used, then <tt class="docutils literal"><span class="pre">self</span></tt> references the topmost template in the
  341. inheritance chain, where it is most useful for providing the
  342. ultimate form of various &#8220;method&#8221; calls which may have been
  343. overridden at various points in an inheritance chain. See
  344. <a class="reference internal" href="inheritance.html"><em>Inheritance</em></a>.</p>
  345. </div>
  346. </div>
  347. <div class="section" id="inheritable-namespaces">
  348. <h2>Inheritable Namespaces<a class="headerlink" href="#inheritable-namespaces" title="Permalink to this headline">¶</a></h2>
  349. <p>The <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag includes an optional attribute
  350. <tt class="docutils literal"><span class="pre">inheritable=&quot;True&quot;</span></tt>, which will cause the namespace to be
  351. 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
  352. available throughout an inheritance chain (described in the next
  353. section), all the templates in an inheritance chain can get at
  354. the namespace imported in a super-template via <tt class="docutils literal"><span class="pre">self</span></tt>.</p>
  355. <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## base.html</span><span class="x"></span>
  356. <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>
  357. <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>
  358. <span class="cp">## somefile.html</span><span class="x"></span>
  359. <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>
  360. <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>
  361. </pre></div>
  362. </div>
  363. <p>This allows a super-template to load a whole bunch of namespaces
  364. that its inheriting templates can get to, without them having to
  365. explicitly load those namespaces themselves.</p>
  366. <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
  367. interact with the <tt class="docutils literal"><span class="pre">inheritable</span></tt> flag, so currently you have to
  368. use the explicit namespace name off of <tt class="docutils literal"><span class="pre">self</span></tt>, followed by the
  369. desired function name. But more on this in a future release.</p>
  370. </div>
  371. <div class="section" id="api-reference">
  372. <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h2>
  373. <dl class="class">
  374. <dt id="mako.runtime.Namespace">
  375. <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>
  376. <dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
  377. <p>Provides access to collections of rendering methods, which
  378. can be local, from other templates, or from imported modules.</p>
  379. <p>To access a particular rendering method referenced by a
  380. <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>
  381. <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>
  382. </pre></div>
  383. </div>
  384. <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
  385. described here.</p>
  386. <dl class="attribute">
  387. <dt id="mako.runtime.Namespace.attr">
  388. <tt class="descname">attr</tt><a class="headerlink" href="#mako.runtime.Namespace.attr" title="Permalink to this definition">¶</a></dt>
  389. <dd><p>Access module level attributes by name.</p>
  390. <p>This accessor allows templates to supply &#8220;scalar&#8221;
  391. attributes which are particularly handy in inheritance
  392. relationships. See the example in
  393. <a class="reference internal" href="inheritance.html"><em>Inheritance</em></a>.</p>
  394. </dd></dl>
  395. <dl class="attribute">
  396. <dt id="mako.runtime.Namespace.cache">
  397. <tt class="descname">cache</tt><a class="headerlink" href="#mako.runtime.Namespace.cache" title="Permalink to this definition">¶</a></dt>
  398. <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
  399. 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
  400. <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>
  401. </dd></dl>
  402. <dl class="attribute">
  403. <dt id="mako.runtime.Namespace.context">
  404. <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>
  405. <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>
  406. <p>Namespaces are often created with copies of contexts that
  407. contain slightly different data, particularly in inheritance
  408. 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
  409. can traverse an entire chain of templates that inherit from
  410. one-another.</p>
  411. </dd></dl>
  412. <dl class="attribute">
  413. <dt id="mako.runtime.Namespace.filename">
  414. <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>
  415. <dd><p>The path of the filesystem file used for this
  416. <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>
  417. <p>If this is a pure module-based
  418. <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
  419. template-based namespace, it evaluates to the original
  420. template file location.</p>
  421. </dd></dl>
  422. <dl class="method">
  423. <dt id="mako.runtime.Namespace.get_cached">
  424. <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>
  425. <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
  426. <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>
  427. <p>The advantage to this method versus direct access to the
  428. <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
  429. declared in <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> take effect here, thereby calling
  430. up the same configured backend as that configured
  431. by <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt>.</p>
  432. </dd></dl>
  433. <dl class="method">
  434. <dt id="mako.runtime.Namespace.get_namespace">
  435. <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>
  436. <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>
  437. <p>If the given <tt class="docutils literal"><span class="pre">uri</span></tt> is a relative URI (i.e. it does not
  438. 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
  439. be relative to the <tt class="docutils literal"><span class="pre">uri</span></tt> of the namespace itself. This
  440. method is therefore mostly useful off of the built-in
  441. <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>
  442. <p>In
  443. most cases, a template wouldn&#8217;t need this function, and
  444. should instead use the <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag to load
  445. namespaces. However, since all <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tags are
  446. evaluated before the body of a template ever runs,
  447. this method can be used to locate namespaces using
  448. expressions that were generated within the body code of
  449. the template, or to conditionally use a particular
  450. namespace.</p>
  451. </dd></dl>
  452. <dl class="method">
  453. <dt id="mako.runtime.Namespace.get_template">
  454. <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>
  455. <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>
  456. <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>
  457. 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>
  458. </dd></dl>
  459. <dl class="method">
  460. <dt id="mako.runtime.Namespace.include_file">
  461. <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>
  462. <dd><p>Include a file at the given <tt class="docutils literal"><span class="pre">uri</span></tt>.</p>
  463. </dd></dl>
  464. <dl class="attribute">
  465. <dt id="mako.runtime.Namespace.module">
  466. <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>
  467. <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>
  468. <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
  469. this module is the equivalent of <tt class="docutils literal"><span class="pre">template.module</span></tt>,
  470. i.e. the generated module for the template.</p>
  471. </dd></dl>
  472. <dl class="attribute">
  473. <dt id="mako.runtime.Namespace.template">
  474. <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>
  475. <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
  476. <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>
  477. </dd></dl>
  478. <dl class="attribute">
  479. <dt id="mako.runtime.Namespace.uri">
  480. <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>
  481. <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>
  482. <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>
  483. <p>This is the equivalent of <tt class="xref py py-attr docutils literal"><span class="pre">Template.uri</span></tt>.</p>
  484. </dd></dl>
  485. </dd></dl>
  486. <dl class="class">
  487. <dt id="mako.runtime.TemplateNamespace">
  488. <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>
  489. <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>
  490. <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>
  491. <dl class="attribute">
  492. <dt id="mako.runtime.TemplateNamespace.filename">
  493. <tt class="descname">filename</tt><a class="headerlink" href="#mako.runtime.TemplateNamespace.filename" title="Permalink to this definition">¶</a></dt>
  494. <dd><p>The path of the filesystem file used for this
  495. <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>
  496. </dd></dl>
  497. <dl class="attribute">
  498. <dt id="mako.runtime.TemplateNamespace.module">
  499. <tt class="descname">module</tt><a class="headerlink" href="#mako.runtime.TemplateNamespace.module" title="Permalink to this definition">¶</a></dt>
  500. <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>
  501. <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
  502. this module is the equivalent of <tt class="docutils literal"><span class="pre">template.module</span></tt>,
  503. i.e. the generated module for the template.</p>
  504. </dd></dl>
  505. <dl class="attribute">
  506. <dt id="mako.runtime.TemplateNamespace.uri">
  507. <tt class="descname">uri</tt><a class="headerlink" href="#mako.runtime.TemplateNamespace.uri" title="Permalink to this definition">¶</a></dt>
  508. <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>
  509. <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>
  510. <p>This is the equivalent of <tt class="xref py py-attr docutils literal"><span class="pre">Template.uri</span></tt>.</p>
  511. </dd></dl>
  512. </dd></dl>
  513. <dl class="class">
  514. <dt id="mako.runtime.ModuleNamespace">
  515. <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>
  516. <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>
  517. <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>
  518. <dl class="attribute">
  519. <dt id="mako.runtime.ModuleNamespace.filename">
  520. <tt class="descname">filename</tt><a class="headerlink" href="#mako.runtime.ModuleNamespace.filename" title="Permalink to this definition">¶</a></dt>
  521. <dd><p>The path of the filesystem file used for this
  522. <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>
  523. </dd></dl>
  524. </dd></dl>
  525. <dl class="function">
  526. <dt id="mako.runtime.supports_caller">
  527. <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>
  528. <dd><p>Apply a caller_stack compatibility decorator to a plain
  529. Python function.</p>
  530. <p>See the example in <a class="reference internal" href="#namespaces-python-modules"><em>Namespaces from Regular Python Modules</em></a>.</p>
  531. </dd></dl>
  532. <dl class="function">
  533. <dt id="mako.runtime.capture">
  534. <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>
  535. <dd><p>Execute the given template def, capturing the output into
  536. a buffer.</p>
  537. <p>See the example in <a class="reference internal" href="#namespaces-python-modules"><em>Namespaces from Regular Python Modules</em></a>.</p>
  538. </dd></dl>
  539. </div>
  540. </div>
  541. </div>
  542. </div>
  543. <div id="docs-bottom-navigation" class="docs-navigation-links">
  544. Previous:
  545. <a href="runtime.html" title="previous chapter">The Mako Runtime Environment</a>
  546. Next:
  547. <a href="inheritance.html" title="next chapter">Inheritance</a>
  548. <div id="docs-copyright">
  549. &copy; Copyright the Mako authors and contributors.
  550. Documentation generated using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3
  551. with Mako templates.
  552. </div>
  553. </div>
  554. </div>
  555. <div class="clearfix">
  556. <hr/>
  557. <div class="copyright">Website content copyright &copy; by Michael Bayer.
  558. All rights reserved. Mako and its documentation are licensed
  559. under the MIT license. mike(&)zzzcomputing.com</div>
  560. </div>
  561. </div>
  562. </body>
  563. </html>