syntax.html 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. Syntax
  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="Defs and Blocks" href="defs.html" />
  32. <link rel="prev" title="Usage" href="usage.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="usage.html" title="previous chapter">Usage</a>
  63. </li>
  64. <li>Next:
  65. <a href="defs.html" title="next chapter">Defs and Blocks</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/syntax.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. Syntax
  78. <h2>
  79. Syntax
  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="#">Syntax</a><ul>
  88. <li><a class="reference internal" href="#expression-substitution">Expression Substitution</a></li>
  89. <li><a class="reference internal" href="#expression-escaping">Expression Escaping</a></li>
  90. <li><a class="reference internal" href="#control-structures">Control Structures</a><ul>
  91. <li><a class="reference internal" href="#the-loop-context">The Loop Context</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#comments">Comments</a></li>
  95. <li><a class="reference internal" href="#newline-filters">Newline Filters</a></li>
  96. <li><a class="reference internal" href="#python-blocks">Python Blocks</a></li>
  97. <li><a class="reference internal" href="#module-level-blocks">Module-level Blocks</a></li>
  98. <li><a class="reference internal" href="#tags">Tags</a><ul>
  99. <li><a class="reference internal" href="#page"><code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code></a></li>
  100. <li><a class="reference internal" href="#include"><code class="docutils literal"><span class="pre">&lt;%include&gt;</span></code></a></li>
  101. <li><a class="reference internal" href="#def"><code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code></a></li>
  102. <li><a class="reference internal" href="#block"><code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code></a></li>
  103. <li><a class="reference internal" href="#namespace"><code class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></code></a></li>
  104. <li><a class="reference internal" href="#inherit"><code class="docutils literal"><span class="pre">&lt;%inherit&gt;</span></code></a></li>
  105. <li><a class="reference internal" href="#nsname-defname"><code class="docutils literal"><span class="pre">&lt;%</span></code>nsname<code class="docutils literal"><span class="pre">:</span></code>defname<code class="docutils literal"><span class="pre">&gt;</span></code></a></li>
  106. <li><a class="reference internal" href="#call"><code class="docutils literal"><span class="pre">&lt;%call&gt;</span></code></a></li>
  107. <li><a class="reference internal" href="#doc"><code class="docutils literal"><span class="pre">&lt;%doc&gt;</span></code></a></li>
  108. <li><a class="reference internal" href="#text"><code class="docutils literal"><span class="pre">&lt;%text&gt;</span></code></a></li>
  109. </ul>
  110. </li>
  111. <li><a class="reference internal" href="#exiting-early-from-a-template">Exiting Early from a Template</a></li>
  112. </ul>
  113. </li>
  114. </ul>
  115. <h4>Previous Topic</h4>
  116. <p>
  117. <a href="usage.html" title="previous chapter">Usage</a>
  118. </p>
  119. <h4>Next Topic</h4>
  120. <p>
  121. <a href="defs.html" title="next chapter">Defs and Blocks</a>
  122. </p>
  123. <h4>Quick Search</h4>
  124. <p>
  125. <form class="search" action="search.html" method="get">
  126. <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
  127. <input type="hidden" name="check_keywords" value="yes" />
  128. <input type="hidden" name="area" value="default" />
  129. </form>
  130. </p>
  131. </div>
  132. <div id="docs-body" class="withsidebar" >
  133. <div class="section" id="syntax">
  134. <span id="syntax-toplevel"></span><h1>Syntax<a class="headerlink" href="#syntax" title="Permalink to this headline">¶</a></h1>
  135. <p>A Mako template is parsed from a text stream containing any kind
  136. of content, XML, HTML, email text, etc. The template can further
  137. contain Mako-specific directives which represent variable and/or
  138. expression substitutions, control structures (i.e. conditionals
  139. and loops), server-side comments, full blocks of Python code, as
  140. well as various tags that offer additional functionality. All of
  141. these constructs compile into real Python code. This means that
  142. you can leverage the full power of Python in almost every aspect
  143. of a Mako template.</p>
  144. <div class="section" id="expression-substitution">
  145. <h2>Expression Substitution<a class="headerlink" href="#expression-substitution" title="Permalink to this headline">¶</a></h2>
  146. <p>The simplest expression is just a variable substitution. The
  147. syntax for this is the <code class="docutils literal"><span class="pre">${}</span></code> construct, which is inspired by
  148. Perl, Genshi, JSP EL, and others:</p>
  149. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="x">this is x: </span><span class="cp">${</span><span class="n">x</span><span class="cp">}</span><span class="x"></span>
  150. </pre></div>
  151. </div>
  152. <p>Above, the string representation of <code class="docutils literal"><span class="pre">x</span></code> is applied to the
  153. template&#8217;s output stream. If you&#8217;re wondering where <code class="docutils literal"><span class="pre">x</span></code> comes
  154. from, it&#8217;s usually from 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> supplied to the
  155. template&#8217;s rendering function. If <code class="docutils literal"><span class="pre">x</span></code> was not supplied to the
  156. template and was not otherwise assigned locally, it evaluates to
  157. a special value <code class="docutils literal"><span class="pre">UNDEFINED</span></code>. More on that later.</p>
  158. <p>The contents within the <code class="docutils literal"><span class="pre">${}</span></code> tag are evaluated by Python
  159. directly, so full expressions are OK:</p>
  160. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="x">pythagorean theorem: </span><span class="cp">${</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="nb">pow</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  161. </pre></div>
  162. </div>
  163. <p>The results of the expression are evaluated into a string result
  164. in all cases before being rendered to the output stream, such as
  165. the above example where the expression produces a numeric
  166. result.</p>
  167. </div>
  168. <div class="section" id="expression-escaping">
  169. <h2>Expression Escaping<a class="headerlink" href="#expression-escaping" title="Permalink to this headline">¶</a></h2>
  170. <p>Mako includes a number of built-in escaping mechanisms,
  171. including HTML, URI and XML escaping, as well as a &#8220;trim&#8221;
  172. function. These escapes can be added to an expression
  173. substitution using the <code class="docutils literal"><span class="pre">|</span></code> operator:</p>
  174. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">${</span><span class="s2">&quot;this is some text&quot;</span> <span class="o">|</span> <span class="n">u</span><span class="cp">}</span><span class="x"></span>
  175. </pre></div>
  176. </div>
  177. <p>The above expression applies URL escaping to the expression, and
  178. produces <code class="docutils literal"><span class="pre">this+is+some+text</span></code>. The <code class="docutils literal"><span class="pre">u</span></code> name indicates URL
  179. escaping, whereas <code class="docutils literal"><span class="pre">h</span></code> represents HTML escaping, <code class="docutils literal"><span class="pre">x</span></code>
  180. represents XML escaping, and <code class="docutils literal"><span class="pre">trim</span></code> applies a trim function.</p>
  181. <p>Read more about built-in filtering functions, including how to
  182. make your own filter functions, in <a class="reference internal" href="filtering.html"><span class="std std-ref">Filtering and Buffering</span></a>.</p>
  183. </div>
  184. <div class="section" id="control-structures">
  185. <h2>Control Structures<a class="headerlink" href="#control-structures" title="Permalink to this headline">¶</a></h2>
  186. <p>A control structure refers to all those things that control the
  187. flow of a program &#8211; conditionals (i.e. <code class="docutils literal"><span class="pre">if</span></code>/<code class="docutils literal"><span class="pre">else</span></code>), loops (like
  188. <code class="docutils literal"><span class="pre">while</span></code> and <code class="docutils literal"><span class="pre">for</span></code>), as well as things like <code class="docutils literal"><span class="pre">try</span></code>/<code class="docutils literal"><span class="pre">except</span></code>. In Mako,
  189. control structures are written using the <code class="docutils literal"><span class="pre">%</span></code> marker followed
  190. by a regular Python control expression, and are &#8220;closed&#8221; by
  191. using another <code class="docutils literal"><span class="pre">%</span></code> marker with the tag &#8220;<code class="docutils literal"><span class="pre">end&lt;name&gt;</span></code>&#8221;, where
  192. &#8220;<code class="docutils literal"><span class="pre">&lt;name&gt;</span></code>&#8221; is the keyword of the expression:</p>
  193. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">%</span> <span class="k">if</span> <span class="n">x</span><span class="o">==</span><span class="mi">5</span><span class="p">:</span><span class="x"></span>
  194. <span class="x"> this is some output</span>
  195. <span class="cp">%</span><span class="k"> endif</span><span class="x"></span>
  196. </pre></div>
  197. </div>
  198. <p>The <code class="docutils literal"><span class="pre">%</span></code> can appear anywhere on the line as long as no text
  199. precedes it; indentation is not significant. The full range of
  200. Python &#8220;colon&#8221; expressions are allowed here, including
  201. <code class="docutils literal"><span class="pre">if</span></code>/<code class="docutils literal"><span class="pre">elif</span></code>/<code class="docutils literal"><span class="pre">else</span></code>, <code class="docutils literal"><span class="pre">while</span></code>, <code class="docutils literal"><span class="pre">for</span></code>, and even <code class="docutils literal"><span class="pre">def</span></code>, although
  202. Mako has a built-in tag for defs which is more full-featured.</p>
  203. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">%</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;one&#39;</span><span class="p">,</span> <span class="s1">&#39;two&#39;</span><span class="p">,</span> <span class="s1">&#39;three&#39;</span><span class="p">,</span> <span class="s1">&#39;four&#39;</span><span class="p">,</span> <span class="s1">&#39;five&#39;</span><span class="p">]:</span><span class="x"></span>
  204. <span class="cp">%</span> <span class="k">if</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span><span class="p">:</span><span class="x"></span>
  205. <span class="x"> its two or three</span>
  206. <span class="cp">%</span> <span class="k">elif</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;f&#39;</span><span class="p">:</span><span class="x"></span>
  207. <span class="x"> four/five</span>
  208. <span class="cp">%</span> <span class="k">else</span><span class="p">:</span><span class="x"></span>
  209. <span class="x"> one</span>
  210. <span class="cp">%</span><span class="k"> endif</span><span class="x"></span>
  211. <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
  212. </pre></div>
  213. </div>
  214. <p>The <code class="docutils literal"><span class="pre">%</span></code> sign can also be &#8220;escaped&#8221;, if you actually want to
  215. emit a percent sign as the first non whitespace character on a
  216. line, by escaping it as in <code class="docutils literal"><span class="pre">%%</span></code>:</p>
  217. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="x">%% some text</span>
  218. <span class="x"> %% some more text</span>
  219. </pre></div>
  220. </div>
  221. <div class="section" id="the-loop-context">
  222. <h3>The Loop Context<a class="headerlink" href="#the-loop-context" title="Permalink to this headline">¶</a></h3>
  223. <p>The <strong>loop context</strong> provides additional information about a loop
  224. while inside of a <code class="docutils literal"><span class="pre">%</span> <span class="pre">for</span></code> structure:</p>
  225. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="x">&lt;ul&gt;</span>
  226. <span class="cp">%</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="p">(</span><span class="s2">&quot;one&quot;</span><span class="p">,</span> <span class="s2">&quot;two&quot;</span><span class="p">,</span> <span class="s2">&quot;three&quot;</span><span class="p">):</span><span class="x"></span>
  227. <span class="x"> &lt;li&gt;Item </span><span class="cp">${</span><span class="n">loop</span><span class="o">.</span><span class="n">index</span><span class="cp">}</span><span class="x">: </span><span class="cp">${</span><span class="n">a</span><span class="cp">}</span><span class="x">&lt;/li&gt;</span>
  228. <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
  229. <span class="x">&lt;/ul&gt;</span>
  230. </pre></div>
  231. </div>
  232. <p>See <a class="reference internal" href="runtime.html#loop-context"><span class="std std-ref">The Loop Context</span></a> for more information on this feature.</p>
  233. <div class="versionadded">
  234. <p><span class="versionmodified">New in version 0.7.</span></p>
  235. </div>
  236. </div>
  237. </div>
  238. <div class="section" id="comments">
  239. <h2>Comments<a class="headerlink" href="#comments" title="Permalink to this headline">¶</a></h2>
  240. <p>Comments come in two varieties. The single line comment uses
  241. <code class="docutils literal"><span class="pre">##</span></code> as the first non-space characters on a line:</p>
  242. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">## this is a comment.</span><span class="x"></span>
  243. <span class="x">...text ...</span>
  244. </pre></div>
  245. </div>
  246. <p>A multiline version exists using <code class="docutils literal"><span class="pre">&lt;%doc&gt;</span> <span class="pre">...text...</span> <span class="pre">&lt;/%doc&gt;</span></code>:</p>
  247. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%doc&gt;</span>
  248. <span class="cp"> these are comments</span>
  249. <span class="cp"> more comments</span>
  250. <span class="cp">&lt;/%doc&gt;</span><span class="x"></span>
  251. </pre></div>
  252. </div>
  253. </div>
  254. <div class="section" id="newline-filters">
  255. <h2>Newline Filters<a class="headerlink" href="#newline-filters" title="Permalink to this headline">¶</a></h2>
  256. <p>The backslash (&#8220;<code class="docutils literal"><span class="pre">\</span></code>&#8221;) character, placed at the end of any
  257. line, will consume the newline character before continuing to
  258. the next line:</p>
  259. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="x">here is a line that goes onto </span><span class="o">\</span>
  260. <span class="x">another line.</span>
  261. </pre></div>
  262. </div>
  263. <p>The above text evaluates to:</p>
  264. <div class="highlight-text"><div class="highlight"><pre><span></span>here is a line that goes onto another line.
  265. </pre></div>
  266. </div>
  267. </div>
  268. <div class="section" id="python-blocks">
  269. <h2>Python Blocks<a class="headerlink" href="#python-blocks" title="Permalink to this headline">¶</a></h2>
  270. <p>Any arbitrary block of python can be dropped in using the <code class="docutils literal"><span class="pre">&lt;%</span>
  271. <span class="pre">%&gt;</span></code> tags:</p>
  272. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="x">this is a template</span>
  273. <span class="cp">&lt;%</span>
  274. <span class="n">x</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">get_resource</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span>
  275. <span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">z</span><span class="o">.</span><span class="n">element</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="n">x</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">frobnizzle</span><span class="o">==</span><span class="mi">5</span><span class="p">]</span>
  276. <span class="cp">%&gt;</span>
  277. <span class="cp">%</span> <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">y</span><span class="p">:</span><span class="x"></span>
  278. <span class="x"> element: </span><span class="cp">${</span><span class="n">elem</span><span class="cp">}</span>
  279. <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
  280. </pre></div>
  281. </div>
  282. <p>Within <code class="docutils literal"><span class="pre">&lt;%</span> <span class="pre">%&gt;</span></code>, you&#8217;re writing a regular block of Python code.
  283. While the code can appear with an arbitrary level of preceding
  284. whitespace, it has to be consistently formatted with itself.
  285. Mako&#8217;s compiler will adjust the block of Python to be consistent
  286. with the surrounding generated Python code.</p>
  287. </div>
  288. <div class="section" id="module-level-blocks">
  289. <h2>Module-level Blocks<a class="headerlink" href="#module-level-blocks" title="Permalink to this headline">¶</a></h2>
  290. <p>A variant on <code class="docutils literal"><span class="pre">&lt;%</span> <span class="pre">%&gt;</span></code> is the module-level code block, denoted
  291. by <code class="docutils literal"><span class="pre">&lt;%!</span> <span class="pre">%&gt;</span></code>. Code within these tags is executed at the module
  292. level of the template, and not within the rendering function of
  293. the template. Therefore, this code does not have access to the
  294. template&#8217;s context and is only executed when the template is
  295. loaded into memory (which can be only once per application, or
  296. more, depending on the runtime environment). Use the <code class="docutils literal"><span class="pre">&lt;%!</span> <span class="pre">%&gt;</span></code>
  297. tags to declare your template&#8217;s imports, as well as any
  298. pure-Python functions you might want to declare:</p>
  299. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%!</span>
  300. <span class="kn">import</span> <span class="nn">mylib</span>
  301. <span class="kn">import</span> <span class="nn">re</span>
  302. <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
  303. <span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^@&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
  304. <span class="cp">%&gt;</span><span class="x"></span>
  305. </pre></div>
  306. </div>
  307. <p>Any number of <code class="docutils literal"><span class="pre">&lt;%!</span> <span class="pre">%&gt;</span></code> blocks can be declared anywhere in a
  308. template; they will be rendered in the resulting module
  309. in a single contiguous block above all render callables,
  310. in the order in which they appear in the source template.</p>
  311. </div>
  312. <div class="section" id="tags">
  313. <h2>Tags<a class="headerlink" href="#tags" title="Permalink to this headline">¶</a></h2>
  314. <p>The rest of what Mako offers takes place in the form of tags.
  315. All tags use the same syntax, which is similar to an XML tag
  316. except that the first character of the tag name is a <code class="docutils literal"><span class="pre">%</span></code>
  317. character. The tag is closed either by a contained slash
  318. character, or an explicit closing tag:</p>
  319. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">include</span> <span class="na">file=</span><span class="s">&quot;foo.txt&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  320. <span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;foo&quot;</span> <span class="na">buffered=</span><span class="s">&quot;True&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  321. <span class="x"> this is a def</span>
  322. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  323. </pre></div>
  324. </div>
  325. <p>All tags have a set of attributes which are defined for each
  326. tag. Some of these attributes are required. Also, many
  327. attributes support <strong>evaluation</strong>, meaning you can embed an
  328. expression (using <code class="docutils literal"><span class="pre">${}</span></code>) inside the attribute text:</p>
  329. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">include</span> <span class="na">file=</span><span class="s">&quot;/foo/bar/${myfile}.txt&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  330. </pre></div>
  331. </div>
  332. <p>Whether or not an attribute accepts runtime evaluation depends
  333. on the type of tag and how that tag is compiled into the
  334. template. The best way to find out if you can stick an
  335. expression in is to try it! The lexer will tell you if it&#8217;s not
  336. valid.</p>
  337. <p>Heres a quick summary of all the tags:</p>
  338. <div class="section" id="page">
  339. <h3><code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code><a class="headerlink" href="#page" title="Permalink to this headline">¶</a></h3>
  340. <p>This tag defines general characteristics of the template,
  341. including caching arguments, and optional lists of arguments
  342. which the template expects when invoked.</p>
  343. <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, z=&#39;default&#39;&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  344. </pre></div>
  345. </div>
  346. <p>Or a page tag that defines caching characteristics:</p>
  347. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_type=</span><span class="s">&quot;memory&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  348. </pre></div>
  349. </div>
  350. <p>Currently, only one <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code> tag gets used per template, the
  351. rest get ignored. While this will be improved in a future
  352. release, for now make sure you have only one <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code> tag
  353. defined in your template, else you may not get the results you
  354. want. The details of what <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code> is used for are described
  355. further in <a class="reference internal" href="namespaces.html#namespaces-body"><span class="std std-ref">The body() Method</span></a> as well as <a class="reference internal" href="caching.html"><span class="std std-ref">Caching</span></a>.</p>
  356. </div>
  357. <div class="section" id="include">
  358. <h3><code class="docutils literal"><span class="pre">&lt;%include&gt;</span></code><a class="headerlink" href="#include" title="Permalink to this headline">¶</a></h3>
  359. <p>A tag that is familiar from other template languages, <code class="docutils literal"><span class="pre">%include</span></code>
  360. is a regular joe that just accepts a file argument and calls in
  361. the rendered result of that file:</p>
  362. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">include</span> <span class="na">file=</span><span class="s">&quot;header.html&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  363. <span class="x"> hello world</span>
  364. <span class="cp">&lt;%</span><span class="nb">include</span> <span class="na">file=</span><span class="s">&quot;footer.html&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  365. </pre></div>
  366. </div>
  367. <p>Include also accepts arguments which are available as <code class="docutils literal"><span class="pre">&lt;%page&gt;</span></code> arguments in the receiving template:</p>
  368. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">include</span> <span class="na">file=</span><span class="s">&quot;toolbar.html&quot;</span> <span class="na">args=</span><span class="s">&quot;current_section=&#39;members&#39;, username=&#39;ed&#39;&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  369. </pre></div>
  370. </div>
  371. </div>
  372. <div class="section" id="def">
  373. <h3><code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code><a class="headerlink" href="#def" title="Permalink to this headline">¶</a></h3>
  374. <p>The <code class="docutils literal"><span class="pre">%def</span></code> tag defines a Python function which contains a set
  375. of content, that can be called at some other point in the
  376. template. The basic idea is simple:</p>
  377. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;myfunc(x)&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  378. <span class="x"> this is myfunc, x is </span><span class="cp">${</span><span class="n">x</span><span class="cp">}</span><span class="x"></span>
  379. <span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  380. <span class="cp">${</span><span class="n">myfunc</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  381. </pre></div>
  382. </div>
  383. <p>The <code class="docutils literal"><span class="pre">%def</span></code> tag is a lot more powerful than a plain Python <code class="docutils literal"><span class="pre">def</span></code>, as
  384. the Mako compiler provides many extra services with <code class="docutils literal"><span class="pre">%def</span></code> that
  385. you wouldn&#8217;t normally have, such as the ability to export defs
  386. as template &#8220;methods&#8221;, automatic propagation of the current
  387. <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>, buffering/filtering/caching flags, and def calls
  388. with content, which enable packages of defs to be sent as
  389. arguments to other def calls (not as hard as it sounds). Get the
  390. full deal on what <code class="docutils literal"><span class="pre">%def</span></code> can do in <a class="reference internal" href="defs.html"><span class="std std-ref">Defs and Blocks</span></a>.</p>
  391. </div>
  392. <div class="section" id="block">
  393. <h3><code class="docutils literal"><span class="pre">&lt;%block&gt;</span></code><a class="headerlink" href="#block" title="Permalink to this headline">¶</a></h3>
  394. <p><code class="docutils literal"><span class="pre">%block</span></code> is a tag that is close to a <code class="docutils literal"><span class="pre">%def</span></code>,
  395. except executes itself immediately in its base-most scope,
  396. and can also be anonymous (i.e. with no name):</p>
  397. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">filter=</span><span class="s">&quot;h&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  398. <span class="x"> some &lt;html&gt; stuff.</span>
  399. <span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
  400. </pre></div>
  401. </div>
  402. <p>Inspired by Jinja2 blocks, named blocks offer a syntactically pleasing way
  403. to do inheritance:</p>
  404. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="x">&lt;html&gt;</span>
  405. <span class="x"> &lt;body&gt;</span>
  406. <span class="x"> </span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">name=</span><span class="s">&quot;header&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  407. <span class="x"> &lt;h2&gt;</span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">name=</span><span class="s">&quot;title&quot;</span><span class="cp">/&gt;</span><span class="x">&lt;/h2&gt;</span>
  408. <span class="x"> </span><span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
  409. <span class="x"> </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="cp">}</span><span class="x"></span>
  410. <span class="x"> &lt;/body&gt;</span>
  411. <span class="x">&lt;/html&gt;</span>
  412. </pre></div>
  413. </div>
  414. <p>Blocks are introduced in <a class="reference internal" href="defs.html#blocks"><span class="std std-ref">Using Blocks</span></a> and further described in <a class="reference internal" href="inheritance.html"><span class="std std-ref">Inheritance</span></a>.</p>
  415. <div class="versionadded">
  416. <p><span class="versionmodified">New in version 0.4.1.</span></p>
  417. </div>
  418. </div>
  419. <div class="section" id="namespace">
  420. <h3><code class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></code><a class="headerlink" href="#namespace" title="Permalink to this headline">¶</a></h3>
  421. <p><code class="docutils literal"><span class="pre">%namespace</span></code> is Mako&#8217;s equivalent of Python&#8217;s <code class="docutils literal"><span class="pre">import</span></code>
  422. statement. It allows access to all the rendering functions and
  423. metadata of other template files, plain Python modules, as well
  424. as locally defined &#8220;packages&#8221; of functions.</p>
  425. <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;functions.html&quot;</span> <span class="na">import=</span><span class="s">&quot;*&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
  426. </pre></div>
  427. </div>
  428. <p>The underlying object generated by <code class="docutils literal"><span class="pre">%namespace</span></code>, an instance of
  429. <a class="reference internal" href="namespaces.html#mako.runtime.Namespace" title="mako.runtime.Namespace"><code class="xref py py-class docutils literal"><span class="pre">mako.runtime.Namespace</span></code></a>, is a central construct used in
  430. templates to reference template-specific information such as the
  431. current URI, inheritance structures, and other things that are
  432. not as hard as they sound right here. Namespaces are described
  433. in <a class="reference internal" href="namespaces.html"><span class="std std-ref">Namespaces</span></a>.</p>
  434. </div>
  435. <div class="section" id="inherit">
  436. <h3><code class="docutils literal"><span class="pre">&lt;%inherit&gt;</span></code><a class="headerlink" href="#inherit" title="Permalink to this headline">¶</a></h3>
  437. <p>Inherit allows templates to arrange themselves in <strong>inheritance
  438. chains</strong>. This is a concept familiar in many other template
  439. languages.</p>
  440. <div class="highlight-mako"><div class="highlight"><pre><span></span><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>
  441. </pre></div>
  442. </div>
  443. <p>When using the <code class="docutils literal"><span class="pre">%inherit</span></code> tag, control is passed to the topmost
  444. inherited template first, which then decides how to handle
  445. calling areas of content from its inheriting templates. Mako
  446. offers a lot of flexibility in this area, including dynamic
  447. inheritance, content wrapping, and polymorphic method calls.
  448. Check it out in <a class="reference internal" href="inheritance.html"><span class="std std-ref">Inheritance</span></a>.</p>
  449. </div>
  450. <div class="section" id="nsname-defname">
  451. <h3><code class="docutils literal"><span class="pre">&lt;%</span></code>nsname<code class="docutils literal"><span class="pre">:</span></code>defname<code class="docutils literal"><span class="pre">&gt;</span></code><a class="headerlink" href="#nsname-defname" title="Permalink to this headline">¶</a></h3>
  452. <p>Any user-defined &#8220;tag&#8221; can be created against
  453. a namespace by using a tag with a name of the form
  454. <code class="docutils literal"><span class="pre">&lt;%&lt;namespacename&gt;:&lt;defname&gt;&gt;</span></code>. The closed and open formats of such a
  455. tag are equivalent to an inline expression and the <code class="docutils literal"><span class="pre">&lt;%call&gt;</span></code>
  456. tag, respectively.</p>
  457. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">mynamespace:somedef</span> <span class="na">param=</span><span class="s">&quot;some value&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  458. <span class="x"> this is the body</span>
  459. <span class="cp">&lt;/%</span><span class="nb">mynamespace:somedef</span><span class="cp">&gt;</span><span class="x"></span>
  460. </pre></div>
  461. </div>
  462. <p>To create custom tags which accept a body, see
  463. <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>
  464. <div class="versionadded">
  465. <p><span class="versionmodified">New in version 0.2.3.</span></p>
  466. </div>
  467. </div>
  468. <div class="section" id="call">
  469. <h3><code class="docutils literal"><span class="pre">&lt;%call&gt;</span></code><a class="headerlink" href="#call" title="Permalink to this headline">¶</a></h3>
  470. <p>The call tag is the &#8220;classic&#8221; form of a user-defined tag, and is
  471. roughly equivalent to the <code class="docutils literal"><span class="pre">&lt;%namespacename:defname&gt;</span></code> syntax
  472. described above. This tag is also 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>.</p>
  473. </div>
  474. <div class="section" id="doc">
  475. <h3><code class="docutils literal"><span class="pre">&lt;%doc&gt;</span></code><a class="headerlink" href="#doc" title="Permalink to this headline">¶</a></h3>
  476. <p>The <code class="docutils literal"><span class="pre">%doc</span></code> tag handles multiline comments:</p>
  477. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%doc&gt;</span>
  478. <span class="cp"> these are comments</span>
  479. <span class="cp"> more comments</span>
  480. <span class="cp">&lt;/%doc&gt;</span><span class="x"></span>
  481. </pre></div>
  482. </div>
  483. <p>Also the <code class="docutils literal"><span class="pre">##</span></code> symbol as the first non-space characters on a line can be used for single line comments.</p>
  484. </div>
  485. <div class="section" id="text">
  486. <h3><code class="docutils literal"><span class="pre">&lt;%text&gt;</span></code><a class="headerlink" href="#text" title="Permalink to this headline">¶</a></h3>
  487. <p>This tag suspends the Mako lexer&#8217;s normal parsing of Mako
  488. template directives, and returns its entire body contents as
  489. plain text. It is used pretty much to write documentation about
  490. Mako:</p>
  491. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span><span class="nb">text</span> <span class="na">filter=</span><span class="s">&quot;h&quot;</span><span class="cp">&gt;</span><span class="x"></span>
  492. <span class="x"> heres some fake mako </span><span class="cp">${</span><span class="n">syntax</span><span class="cp">}</span><span class="x"></span>
  493. <span class="x"> </span><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;x()&quot;</span><span class="cp">&gt;${</span><span class="n">x</span><span class="cp">}&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
  494. <span class="cp">&lt;/%</span><span class="nb">text</span><span class="cp">&gt;</span><span class="x"></span>
  495. </pre></div>
  496. </div>
  497. </div>
  498. </div>
  499. <div class="section" id="exiting-early-from-a-template">
  500. <span id="syntax-exiting-early"></span><h2>Exiting Early from a Template<a class="headerlink" href="#exiting-early-from-a-template" title="Permalink to this headline">¶</a></h2>
  501. <p>Sometimes you want to stop processing a template or <code class="docutils literal"><span class="pre">&lt;%def&gt;</span></code>
  502. method in the middle and just use the text you&#8217;ve accumulated so
  503. far. This is accomplished by using <code class="docutils literal"><span class="pre">return</span></code> statement inside
  504. a Python block. It&#8217;s a good idea for the <code class="docutils literal"><span class="pre">return</span></code> statement
  505. to return an empty string, which prevents the Python default return
  506. value of <code class="docutils literal"><span class="pre">None</span></code> from being rendered by the template. This
  507. return value is for semantic purposes provided in templates via
  508. the <code class="docutils literal"><span class="pre">STOP_RENDERING</span></code> symbol:</p>
  509. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">%</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">records</span><span class="p">):</span><span class="x"></span>
  510. <span class="x"> No records found.</span>
  511. <span class="x"> </span><span class="cp">&lt;%</span> <span class="k">return</span> <span class="n">STOP_RENDERING</span> <span class="cp">%&gt;</span>
  512. <span class="cp">%</span><span class="k"> endif</span><span class="x"></span>
  513. </pre></div>
  514. </div>
  515. <p>Or perhaps:</p>
  516. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span>
  517. <span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">records</span><span class="p">):</span>
  518. <span class="k">return</span> <span class="n">STOP_RENDERING</span>
  519. <span class="cp">%&gt;</span><span class="x"></span>
  520. </pre></div>
  521. </div>
  522. <p>In older versions of Mako, an empty string can be substituted for
  523. the <code class="docutils literal"><span class="pre">STOP_RENDERING</span></code> symbol:</p>
  524. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">&lt;%</span> <span class="k">return</span> <span class="s1">&#39;&#39;</span> <span class="cp">%&gt;</span><span class="x"></span>
  525. </pre></div>
  526. </div>
  527. <div class="versionadded">
  528. <p><span class="versionmodified">New in version 1.0.2: </span>- added the <code class="docutils literal"><span class="pre">STOP_RENDERING</span></code> symbol which serves
  529. as a semantic identifier for the empty string <code class="docutils literal"><span class="pre">&quot;&quot;</span></code> used by a
  530. Python <code class="docutils literal"><span class="pre">return</span></code> statement.</p>
  531. </div>
  532. </div>
  533. </div>
  534. </div>
  535. </div>
  536. <div id="docs-bottom-navigation" class="docs-navigation-links">
  537. Previous:
  538. <a href="usage.html" title="previous chapter">Usage</a>
  539. Next:
  540. <a href="defs.html" title="next chapter">Defs and Blocks</a>
  541. <div id="docs-copyright">
  542. &copy; Copyright the Mako authors and contributors.
  543. Documentation generated using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.5.3
  544. with Mako templates.
  545. </div>
  546. </div>
  547. </div>
  548. <div class="clearfix">
  549. <hr/>
  550. <div class="copyright">Website content copyright &copy; by Michael Bayer.
  551. All rights reserved. Mako and its documentation are licensed
  552. under the MIT license. mike(&)zzzcomputing.com</div>
  553. </div>
  554. </div>
  555. </body>
  556. </html>