This site has been archived. To learn more about our current products Ibexa Content, Ibexa Experience, Ibexa Commerce head over to the Ibexa Developer Portal

eZ Community » Forums » Localized forums » 中文讨论区 - Chinese » 如何创建新的操作符,例如...
expandshrink

如何创建新的操作符,例如:wash()函数

如何创建新的操作符,例如:wash()函数

Friday 07 December 2012 2:23:33 am - 2 replies

我想新建一个类似于wash()操作符的函数,能够在模版中如{$node.name|html()}一样使用,但我不知道如何创建,也没有找到创建的文档。请教。

谢谢

Friday 07 December 2012 8:57:00 am

http://www.netgenlabs.com/Blog/Co...-up-template-operators-in-eZ-Publish

Wednesday 12 December 2012 7:45:56 am

谢谢雄杰提供的文档url,我经过实际操作是完全可以成功的。但我还是想把我实际操作过程复述一下。虽然这有些啰嗦,但我想也许有这个必要。1.首先创建一个extension,暂时命名为mypage;同时在settings/override/site.ini.append.php中配置开启新建的extension。如添加如下code[ExtensionSettings]
ActiveExtensions[]=mypage2.在extension/mypage/settings/下创建site.ini.append.php,并输入以下code[TemplateSettings]AutoloadPathList[]=extension/mypage/autoloads/3.创建文件 extension/mypage/autoloads/eztemplateautoload.php,并输入以下代码

<span style="font-weight: bold;"><?php</span>
<span style="color: #000088;">$eZTemplateOperatorArray</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #000088;">
$eZTemplateOperatorArray</span><span style="color: #009900;">[</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span>                <span style="color: #0000ff;">'script'</span> <span style="color: #339933;">=></span> <span style="color: #0000ff;">'extension/mypage/classes/my_operator.php'</span><span style="color: #339933;">,</span>                <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=></span> <span style="color: #0000ff;">'MyOperator'</span><span style="color: #339933;">,</span>                <span style="color: #0000ff;">'operator_names'</span> <span style="color: #339933;">=></span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span><span style="color: #0000ff;">'my_operator'</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="font-weight: bold;">?></span>

 4.创建文件my_operator.php,在 extension/mypage/classes/下

<span style="font-weight: bold;"><?php</span><span style="font-weight: bold;">class</span> MyOperator<span style="color: #009900;">{</span>  <span style="font-weight: bold;">function</span> __construct<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>    <span style="color: #000088;">$this</span><span style="color: #339933;">-></span><span style="color: #004000;">Operators</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span><span style="color: #0000ff;">'my_operator'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span> <span style="font-weight: bold;">function</span> operatorList<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-></span><span style="color: #004000;">Operators</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span> <span style="font-weight: bold;">function</span> namedParameterPerOperator<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span> <span style="font-weight: bold;">function</span> namedParameterList<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span>            <span style="color: #0000ff;">'my_operator'</span> <span style="color: #339933;">=></span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span>                    <span style="color: #0000ff;">'display_string'</span> <span style="color: #339933;">=></span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span>                            <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=></span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span>                            <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=></span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>                            <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=></span> <span style="color: #0000ff;">''</span>                    <span style="color: #009900;">)</span>            <span style="color: #009900;">)</span>    <span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span> <span style="font-weight: bold;">function</span> modify<span style="color: #009900;">(</span> <span style="color: #000088;">$tpl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operatorName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operatorParameters</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rootNamespace</span><span style="color: #339933;">,</span><span style="color: #000088;">$currentNamespace</span><span style="color: #339933;">,</span> <span style="color: #339933;">&</span><span style="color: #000088;">$operatorValue</span><span style="color: #339933;">,</span> <span style="color: #000088;">$namedParameters</span> <span style="color: #009900;">)</span><span style="color: #009900;">{</span>    <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #000088;">$operatorName</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'my_operator'</span><span style="color: #009900;">)</span>    <span style="color: #009900;">{</span>        <span style="color: #000088;">$operatorValue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-></span><span style="color: #004000;">my_operator</span><span style="color: #009900;">(</span><span style="color: #000088;">$namedParameters</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'display_string'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>    <span style="color: #009900;">}</span><span style="color: #009900;">}</span> <span style="font-weight: bold;">function</span> my_operator<span style="color: #009900;">(</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">)</span><span style="color: #009900;">{</span>    <span style="color: #b1b100;">return</span> <span style="color: #009900;">(</span><span style="color: #0000ff;">'My operator displays any string I want, for example: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$args</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #009900;">
}</span> <span style="font-weight: bold;">private</span> <span style="color: #000088;">$Operators</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span><span style="font-weight: bold;">
?></span>
<strong>以上代码为</strong><span style="color: #66cc66;">{</span>my_operator<span style="color: #66cc66;">(</span><span style="color: #dd0000;">'some string or param I want to display'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">}的使用方式</span>
<span style="color: #66cc66;">如果以</span><span style="color: #66cc66;">{</span><span style="color: #dd0000;">'some string or param I want to display'</span><span style="color: #66cc66;">|</span>my_operator<span style="color: #66cc66;">}</span>
这种方式访问
则需要修改为以下代码
<span style="font-weight: bold;">function</span> namedParameterList<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span> <span style="color: #0000ff;">'my_operator'</span> <span style="color: #339933;">=></span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span><span style="color: #0000ff;">'display_string'</span> <span style="color: #339933;">=></span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">(</span><span style="color: #0000ff;">'type'</span><span style="color: #339933;">=></span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=></span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=></span> <span style="color: #0000ff;">''</span> <span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span> <span style="font-weight: bold;">function</span> modify<span style="color: #009900;">(</span><span style="color: #000088;">$tpl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operatorName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$operatorParameters</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rootNamespace</span><span style="color: #339933;">,</span> <span style="color: #000088;">$currentNamespace</span><span style="color: #339933;">,</span> <span style="color: #339933;">&</span><span style="color: #000088;">$operatorValue</span><span style="color: #339933;">,</span> <span style="color: #000088;">$namedParameters</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>    <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #000088;">$operatorName</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>        <span style="color: #000088;">$operatorValue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-></span><span style="color: #004000;">my_operator</span><span style="color: #009900;">(</span><span style="color: #000088;">$operatorValue</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>    <span style="color: #009900;">}</span><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">(</span> <span style="color: #000088;">$namedParameters</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'display_string'</span><span style="color: #009900;">]</span> <span style="color: #009900;">)</span> <span style="color: #009900;">)</span><span style="color: #009900;">{</span>    <span style="color: #000088;">$operatorValue</span> <span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-></span><span style="color: #004000;">my_operator</span><span style="color: #009900;">(</span><span style="color: #000088;">$namedParameters</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'display_string'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span><span style="color: #b1b100;">else</span><span style="color: #009900;">{</span>    <span style="color: #000088;">$operatorValue</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">""</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span><span style="color: #009900;">
}

至此,模版操作符创建成功,可以在模版中使用。



</span>
expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from