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