开发者

How can I use a filter as a method in template toolkit?

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 %]

 &开发者_开发知识库lt;/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?


I found out what I should've been doing. The code I ended up with is:

[% FOREACH feed IN rss_feeds %]
 <div class="rssfeed">
   <a class="rsstitle" href="[% feed.link | html %]">[% feed.title %]</a>

   [% USE String %]
   [% SET rsstitle = String.new(feed.title) %]
   [% SET rssdescription = String.new(feed.description) %]
   [% IF rsstitle.truncate(10) != rssdescription.truncate(10) %]
     <div class="rssdescription">[% feed.description %]</div>
   [% END %]

 </div>
[% END %]

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜