如何使用过滤器作为模板工具包中的方法?

发布于 2024-08-23 18:22:43 字数 1107 浏览 4 评论 0原文

我尝试对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2024-08-30 18:22:43

我发现我应该做什么。我最终得到的代码是:

[% 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 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文