File: subs.html

package info (click to toggle)
cubictemp 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 264 kB
  • ctags: 139
  • sloc: python: 682; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 4,941 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta  content="text/html; charset=utf-8" http-equiv="Content-Type"/><link  href="static/yui/fonts/fonts.css" type="text/css" rel="StyleSheet"/>
<link  href="static/yui/reset/reset.css" type="text/css" rel="StyleSheet"/>
<link  href="static/yui/grids/grids.css" type="text/css" rel="StyleSheet"/>
<link  href="static/style.css" type="text/css" rel="StyleSheet"/>
<link  href="static/yui/base/base.css" type="text/css" rel="StyleSheet"/>
<link  href="docstyle.css" type="text/css" rel="StyleSheet"/>
<link  href="syntax.css" type="text/css" rel="StyleSheet"/>


<title>Cubictemp Manual - Tags</title></head><body><div class="yui-t2" id="doc3"><div style="" id="hd"><h1> Cubictemp - Tags </h1></div><div id="bd"><div id="yui-main"><div style="" class="yui-b"><h1>Tags and Expressions</h1><p>Cubictemp evaluates expressions between tag delimiters as expressions in the
specified namespace. The result is converted to a string, and placed in the
rendered template. There are two flavours of delimiters - <strong>escaped</strong>:</p><pre class="output">
@!...!@
</pre><p>and <strong>unescaped</strong>:</p><pre class="output">
$!...!$
</pre><p>In Python, an <b>expression</b> can be thought of as anything that can be
assigned to a variable. Arithmetic operators, boolean operators, parentheses
for grouping, method/function calls, object instantiation and conditional
expressions are all valid components of expressions. Python <b>statements</b>
include things like <b>while</b>, <b>print</b>, variable assignment and full
<b>if</b> blocks.  Cubictemp allows <b>only expressions</b> in subtitution
tags. </p><h3> Example</h3><p>Template:</p><pre class="output">
@!foo!@ times two is @!foo*2!@
@!foo!@ squared is @!foo*foo!@
@!"yes" if (1==2) else "no"!@
key is @!mydict["key"]!@
</pre><p>Code:</p><div class="highlight"><pre><span class="k">import</span> <span class="nn">cubictemp</span>
<span class="k">print</span> <span class="n">cubictemp</span><span class="o">.</span><span class="n">File</span><span class="p">(</span>
            <span class="s">&quot;template&quot;</span><span class="p">,</span>
            <span class="n">foo</span><span class="o">=</span><span class="mf">3</span><span class="p">,</span>
            <span class="n">mydict</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="s">&quot;value&quot;</span><span class="p">)</span>
        <span class="p">)</span>
</pre></div><p>Output:</p><pre class="output">
3 times two is 6
3 squared is 9
no
key is value
</pre><h1>Escaping</h1><p>In an escaped substitution tag, the &amp;, &lt;, &gt;, &quot;, &#146;
characters are converted to their corresponding HTML escape sequences.  Always
use the <b>escaped</b> substitution syntax if you can. When you really need to
place HTML in a substitution tag, make sure you carefully evaluate the
application context to make sure that users cannot inject malicious data.</p><h3> Example</h3><div class="highlight"><pre><span class="k">import</span> <span class="nn">cubictemp</span>
<span class="k">print</span> <span class="n">cubictemp</span><span class="o">.</span><span class="n">Template</span><span class="p">(</span>
        <span class="s">&quot;@!x!@ $!x!$&quot;</span><span class="p">,</span>
        <span class="n">x</span> <span class="o">=</span> <span class="s">&quot;&lt;H1&gt;foo&lt;/H1&gt;&quot;</span>
    <span class="p">)</span>
</pre></div><p>... will print:</p><div class="highlight"><pre><span class="nt">&lt;H1&gt;</span>foo<span class="nt">&lt;/H1&gt;</span> <span class="ni">&amp;lt;</span>H1<span class="ni">&amp;gt;</span>foo<span class="ni">&amp;lt;</span>/H1<span class="ni">&amp;gt;</span>
</pre></div><h1>Controlling Escaping</h1><p>Sometimes, it is handy to be able to construct objects that bypass Cubictemp's
escaping mechanism, regardless of the type of tag in which they occur. You can
signal this to cubictemp by giving the object a special attribute
<b>_cubictemp_unescaped</b> which evaluates to true.</p><p><strong>Template</strong>, <strong>File</strong>, and named block objects all have a
<b>_cubictemp_unescaped</b> attribute, so none of these objects will be escaped
when referenced inside an escaped tag.</p></div></div><div style="" class="yui-b"><div class="pageindex"><ul><li class="inactive"><a href="index.html">Introduction</a></li><li class="active"><a href="subs.html">Tags</a></li><li class="inactive"><a href="blocks.html">Blocks</a></li><li class="inactive"><a href="processors.html">Processors</a></li><li class="inactive"><a href="api.html">API</a></li><li class="inactive"><a href="admin.html">Administrivia</a></li></ul></div></div></div><div style="" id="ft"><p>Copyright Nullcube 2008</p></div></div></body></html>