1 |
|
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
4 |
|
5 <html xmlns="http://www.w3.org/1999/xhtml"> |
|
6 <head> |
|
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|
8 <title>Custom TALES extensions — PyAMS User Guide 0.1.0 documentation</title> |
|
9 <link rel="stylesheet" href="_static/pyramid.css" type="text/css" /> |
|
10 <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> |
|
11 <script type="text/javascript"> |
|
12 var DOCUMENTATION_OPTIONS = { |
|
13 URL_ROOT: './', |
|
14 VERSION: '0.1.0', |
|
15 COLLAPSE_INDEX: false, |
|
16 FILE_SUFFIX: '.html', |
|
17 HAS_SOURCE: true, |
|
18 SOURCELINK_SUFFIX: '.txt' |
|
19 }; |
|
20 </script> |
|
21 <script type="text/javascript" src="_static/jquery.js"></script> |
|
22 <script type="text/javascript" src="_static/underscore.js"></script> |
|
23 <script type="text/javascript" src="_static/doctools.js"></script> |
|
24 <link rel="index" title="Index" href="genindex.html" /> |
|
25 <link rel="search" title="Search" href="search.html" /> |
|
26 <link rel="next" title="Custom PyAMS utilities" href="utilities.html" /> |
|
27 <link rel="prev" title="PyAMS namespace traverser" href="traverser.html" /> |
|
28 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Neuton&subset=latin" type="text/css" media="screen" charset="utf-8" /> |
|
29 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nobile:regular,italic,bold,bolditalic&subset=latin" type="text/css" media="screen" charset="utf-8" /> |
|
30 <!--[if lte IE 6]> |
|
31 <link rel="stylesheet" href="_static/ie6.css" type="text/css" media="screen" charset="utf-8" /> |
|
32 <![endif]--> |
|
33 |
|
34 </head> |
|
35 <body> |
|
36 |
|
37 <div class="related" role="navigation" aria-label="related navigation"> |
|
38 <h3>Navigation</h3> |
|
39 <ul> |
|
40 <li class="right" style="margin-right: 10px"> |
|
41 <a href="genindex.html" title="General Index" |
|
42 accesskey="I">index</a></li> |
|
43 <li class="right" > |
|
44 <a href="utilities.html" title="Custom PyAMS utilities" |
|
45 accesskey="N">next</a> |</li> |
|
46 <li class="right" > |
|
47 <a href="traverser.html" title="PyAMS namespace traverser" |
|
48 accesskey="P">previous</a> |</li> |
|
49 <li class="nav-item nav-item-0"><a href="index.html">PyAMS User Guide 0.1.0 documentation</a> »</li> |
|
50 </ul> |
|
51 </div> |
|
52 |
|
53 <div class="document"> |
|
54 <div class="documentwrapper"> |
|
55 <div class="bodywrapper"> |
|
56 <div class="body" role="main"> |
|
57 |
|
58 <div class="section" id="custom-tales-extensions"> |
|
59 <span id="tales"></span><h1>Custom TALES extensions<a class="headerlink" href="#custom-tales-extensions" title="Permalink to this headline">¶</a></h1> |
|
60 <p>PyAMS defines a custom expression for TALES called <em>extension</em>.</p> |
|
61 <p>When this expression is encountered, the renderer is looking for an |
|
62 <code class="xref py py-class docutils literal"><span class="pre">ITALESExtension</span></code> |
|
63 multi-adapter for the current <em>context</em>, <em>request</em> and <em>view</em>, for the current |
|
64 <em>context</em> and <em>request</em>, or only for the current <em>context</em>, in this order. |
|
65 If an adapter is found, the renderer call it’s <code class="xref py py-func docutils literal"><span class="pre">render()</span></code> method with |
|
66 the expression parameters as input parameters.</p> |
|
67 <p>For example, the <em>metas</em> extension is an <em>ITALESExtension</em> adapter defined into |
|
68 <code class="xref py py-mod docutils literal"><span class="pre">pyams_skin.metas</span></code> module which can be used to include all required headers in |
|
69 a page template. Extension is used like this in the page layout template:</p> |
|
70 <div class="highlight-html"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">tal:var</span> <span class="na">replace</span><span class="o">=</span><span class="s">"structure extension:metas"</span> <span class="p">/></span> |
|
71 </pre></div> |
|
72 </div> |
|
73 <p>This extension is defined like this:</p> |
|
74 <div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyams_skin.interfaces.metas</span> <span class="kn">import</span> <span class="n">IHTMLContentMetas</span> |
|
75 <span class="kn">from</span> <span class="nn">pyams_utils.interfaces.tales</span> <span class="kn">import</span> <span class="n">ITALESExtension</span> |
|
76 <span class="kn">from</span> <span class="nn">pyramid.interfaces</span> <span class="kn">import</span> <span class="n">IRequest</span> |
|
77 |
|
78 <span class="kn">from</span> <span class="nn">pyams_utils.adapter</span> <span class="kn">import</span> <span class="n">adapter_config</span><span class="p">,</span> <span class="n">ContextRequestViewAdapter</span> |
|
79 |
|
80 <span class="nd">@adapter_config</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'metas'</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="p">(</span><span class="n">Interface</span><span class="p">,</span> <span class="n">IRequest</span><span class="p">,</span> <span class="n">Interface</span><span class="p">),</span> <span class="n">provides</span><span class="o">=</span><span class="n">ITALESExtension</span><span class="p">)</span> |
|
81 <span class="k">class</span> <span class="nc">MetasTalesExtension</span><span class="p">(</span><span class="n">ContextRequestViewAdapter</span><span class="p">):</span> |
|
82 <span class="sd">'''extension:metas TALES extension'''</span> |
|
83 |
|
84 <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> |
|
85 <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> |
|
86 <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span> |
|
87 <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span> |
|
88 <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">adapter</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">registry</span><span class="o">.</span><span class="n">getAdapters</span><span class="p">((</span><span class="n">context</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">view</span><span class="p">),</span> |
|
89 <span class="n">IHTMLContentMetas</span><span class="p">),</span> |
|
90 <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s1">'order'</span><span class="p">,</span> <span class="mi">9999</span><span class="p">)):</span> |
|
91 <span class="n">result</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">meta</span><span class="o">.</span><span class="n">render</span><span class="p">()</span> <span class="k">for</span> <span class="n">meta</span> <span class="ow">in</span> <span class="n">adapter</span><span class="o">.</span><span class="n">get_metas</span><span class="p">()])</span> |
|
92 <span class="k">return</span> <span class="s1">'</span><span class="se">\n\t</span><span class="s1">'</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> |
|
93 </pre></div> |
|
94 </div> |
|
95 <p>Some TALES extensions can require or accept arguments. For example, the <em>absolute_url</em> extension can accept |
|
96 a context and a view name:</p> |
|
97 <div class="highlight-html"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">tal:var</span> <span class="na">define</span><span class="o">=</span><span class="s">"logo config.logo"</span><span class="p">></span> |
|
98 <span class="p"><</span><span class="nt">img</span> <span class="na">tal:attributes</span><span class="o">=</span><span class="s">"src extension:absolute_url(logo, '++thumb++200x36.png');"</span> <span class="p">/></span> |
|
99 <span class="p"></</span><span class="nt">tal:var</span><span class="p">></span> |
|
100 </pre></div> |
|
101 </div> |
|
102 <p>The extension is defined like this:</p> |
|
103 <div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">persistent.interfaces</span> <span class="kn">import</span> <span class="n">IPersistent</span> |
|
104 <span class="kn">from</span> <span class="nn">pyams_utils.interfaces.tales</span> <span class="kn">import</span> <span class="n">ITALESExtension</span> |
|
105 |
|
106 <span class="kn">from</span> <span class="nn">pyams_utils.adapter</span> <span class="kn">import</span> <span class="n">adapter_config</span><span class="p">,</span> <span class="n">ContextRequestViewAdapter</span> |
|
107 <span class="kn">from</span> <span class="nn">pyramid.url</span> <span class="kn">import</span> <span class="n">resource_url</span> |
|
108 <span class="kn">from</span> <span class="nn">zope.interface</span> <span class="kn">import</span> <span class="n">Interface</span> |
|
109 |
|
110 <span class="nd">@adapter_config</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'absolute_url'</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="p">(</span><span class="n">IPersistent</span><span class="p">,</span> <span class="n">Interface</span><span class="p">,</span> <span class="n">Interface</span><span class="p">),</span> <span class="n">provides</span><span class="o">=</span><span class="n">ITALESExtension</span><span class="p">)</span> |
|
111 <span class="k">class</span> <span class="nc">AbsoluteUrlTalesExtension</span><span class="p">(</span><span class="n">ContextRequestViewAdapter</span><span class="p">):</span> |
|
112 <span class="sd">'''extension:absolute_url(context, view_name) TALES extension'''</span> |
|
113 |
|
114 <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">view_name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> |
|
115 <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> |
|
116 <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span> |
|
117 <span class="k">return</span> <span class="n">resource_url</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="p">,</span> <span class="n">view_name</span><span class="p">)</span> |
|
118 </pre></div> |
|
119 </div> |
|
120 </div> |
|
121 |
|
122 |
|
123 </div> |
|
124 </div> |
|
125 </div> |
|
126 <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> |
|
127 <div class="sphinxsidebarwrapper"> |
|
128 <h4>Previous topic</h4> |
|
129 <p class="topless"><a href="traverser.html" |
|
130 title="previous chapter">PyAMS namespace traverser</a></p> |
|
131 <h4>Next topic</h4> |
|
132 <p class="topless"><a href="utilities.html" |
|
133 title="next chapter">Custom PyAMS utilities</a></p> |
|
134 <div id="searchbox" style="display: none" role="search"> |
|
135 <h3>Quick search</h3> |
|
136 <form class="search" action="search.html" method="get"> |
|
137 <div><input type="text" name="q" /></div> |
|
138 <div><input type="submit" value="Go" /></div> |
|
139 <input type="hidden" name="check_keywords" value="yes" /> |
|
140 <input type="hidden" name="area" value="default" /> |
|
141 </form> |
|
142 </div> |
|
143 <script type="text/javascript">$('#searchbox').show(0);</script> |
|
144 </div> |
|
145 </div> |
|
146 <div class="clearer"></div> |
|
147 </div> |
|
148 <div class="related" role="navigation" aria-label="related navigation"> |
|
149 <h3>Navigation</h3> |
|
150 <ul> |
|
151 <li class="right" style="margin-right: 10px"> |
|
152 <a href="genindex.html" title="General Index" |
|
153 >index</a></li> |
|
154 <li class="right" > |
|
155 <a href="utilities.html" title="Custom PyAMS utilities" |
|
156 >next</a> |</li> |
|
157 <li class="right" > |
|
158 <a href="traverser.html" title="PyAMS namespace traverser" |
|
159 >previous</a> |</li> |
|
160 <li class="nav-item nav-item-0"><a href="index.html">PyAMS User Guide 0.1.0 documentation</a> »</li> |
|
161 </ul> |
|
162 </div> |
|
163 <div class="footer" role="contentinfo"> |
|
164 © Copyright 2017, Thierry Florac. |
|
165 Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.5. |
|
166 </div> |
|
167 </body> |
|
168 </html> |
|