如何使用过滤器作为模板工具包中的方法?
我尝试对 2 个变量(rsstitle 和 rssdescription)使用截断过滤器,并将截断版本分配给新变量(rsstitletrunc 和 rssdescriptiontrunc)。我对模板工具包相对较新,并且不明白为什么此代码不起作用(SET 和 IF/ELSE/END):
[% FOREACH feed IN rss_feeds %]
<div class="rssfeed">
<a class="rsstitle" href="[% feed.link | html %]">[% feed.title %]</a>
<div class="rssdescription">[% feed.description %]</div>
[% SET rsstitle = feed.title %]
[% SET rsstitleclean = rsstitle | truncate(10) %]
[% SET rssdescription = feed.description %]
[% SET rssdescriptionclean = rssdescription | truncate(10) %]
[% IF rssdescriptionclean == rsstitleclean %]
<div class="rssdescription">Same: [% rsstitleclean %] | [% rssdescriptionclean %]</div>
[% ELSE %]
<div class="rssdescription">Differs: [% rsstitleclean %] | [% rssdescriptionclean %]</div>
[% END %]
</div>
[% END %]
rsstitleclean 返回 rsstitle 的值(未截断) )。 rssdescriptionclean 返回rssdescription 的值(未截断)。看来我无法对变量使用过滤器并将过滤后的值声明给另一个变量。或者我可以吗?
I am trying to use the truncate filter on 2 variables (rsstitle and rssdescription), and assign the truncated version to a new variable (rsstitletrunc and rssdescriptiontrunc). I am relatively new to Template Toolkit, and dont understand why this code wont work (the SETs and IF/ELSE/END):
[% FOREACH feed IN rss_feeds %]
<div class="rssfeed">
<a class="rsstitle" href="[% feed.link | html %]">[% feed.title %]</a>
<div class="rssdescription">[% feed.description %]</div>
[% SET rsstitle = feed.title %]
[% SET rsstitleclean = rsstitle | truncate(10) %]
[% SET rssdescription = feed.description %]
[% SET rssdescriptionclean = rssdescription | truncate(10) %]
[% IF rssdescriptionclean == rsstitleclean %]
<div class="rssdescription">Same: [% rsstitleclean %] | [% rssdescriptionclean %]</div>
[% ELSE %]
<div class="rssdescription">Differs: [% rsstitleclean %] | [% rssdescriptionclean %]</div>
[% END %]
</div>
[% END %]
rsstitleclean returns the value of rsstitle (not truncated). rssdescriptionclean returns the value of rssdescription (not truncated). It seems I cant use filters on variables and declare the filtered value to another variable. Or can I?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现我应该做什么。我最终得到的代码是:
我必须将哈希键声明为新字符串,然后我才能截断并比较变量。据我所知,不可能将过滤器作为一种方法来运行。希望这对某人有帮助!
I found out what I should've been doing. The code I ended up with is:
I had to declare the hash key as a new string, and then I was able to truncate and compare the variables. From what I can tell, it is NOT possible to run a filter as a method. Hope this helps someone!