使用正则表达式替换为smarty模板系统? [英] Using regex replace with smarty template system?

查看:52
本文介绍了使用正则表达式替换为smarty模板系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前在Smarty模板中拥有的内容:

Here's what I currently have in my Smarty template:

<div class="tbl_pagination">
    {if $pager}{$pager->links}{/if}
<div>

我的 {$ pager-> links} 将输出以下HTML:

My {$pager->links} will output this HTML:

<div class="tbl_pagination">
    <ul><li><a href="javascript:toPage(3)" title="previous page">Back</a></li>
    <li><a href="javascript:toPage(1)" title="page 1">1</a></li>
    <li><a href="javascript:toPage(2)" title="page 2">2</a></li>
    <li><a href="javascript:toPage(3)" title="page 3">3</a></li>
    <li class='active'>4</li>
    <li><a href="javascript:toPage(5)" title="page 5">5</a></li>
    <li><a href="javascript:toPage(6)" title="page 6">6</a></li>
    <li><a href="javascript:toPage(7)" title="page 7">7</a></li>
    <li><a href="javascript:toPage(5)" title="next page">Next</a></li>
    </ul></div>
</div>

我正在尝试替换两个不同的东西:

I'm trying to replace two different things:

  1. 我想完成删除 title 属性.

具有 active 类的所有 li 也需要具有 a href .

Whichever li has the class active needs to also have an a href.

这就是我想要的样子:

<div class="tbl_pagination">
    <ul><li><a href="javascript:toPage(3)">Back</a></li>
    <li><a href="javascript:toPage(1)">1</a></li>
    <li><a href="javascript:toPage(2)">2</a></li>
    <li><a href="javascript:toPage(3)">3</a></li>
    <li class='active'><a href="#">4</a></li>
    <li><a href="javascript:toPage(5)">5</a></li>
    <li><a href="javascript:toPage(6)">6</a></li>
    <li><a href="javascript:toPage(7)">7</a></li>
    <li><a href="javascript:toPage(5)">Next</a></li>
    </ul></div>
</div>

使用替换功能在Smarty模板系统中可以做到这一点吗?

Is this possible to do in the Smarty template system using their replace function?

推荐答案

由于您在注释中提到自己已经实现了(2),因此这是使用2种方法进行第一次替换的解决方案:

Since you mentioned in your comments that you achieved (2) on your own, here's a solution for the first replace, with 2 methods:

  1. 您可以使用 regex_replace :

{$ pager-> links | regex_replace:'/title ="[\ w \ s] +"/':"}

或者,您可以直接使用php的preg_replace,如下所示:

Alternatively, you can use directly php's preg_replace, like this:

{'/title ="[\ w \ s] +"/'| preg_replace:'':$ pager->链接}

乍一看似乎很奇怪,但是让我解释一下语法:

This might seem weird at first, but let me explain the syntax:

Smarty支持使用 | (管道)符号将变量/字符串传递给某些php函数.但是,其他参数然后与:(冒号)参数一起传递.这与变量修饰符

Smarty supports passing a variable/string to some php function using the | (pipe) symbol. However, additional parameters are then passed with the : (colon) parameter. This is consistent for the syntax of Variable Modifiers

例如,如果您想计算字符串中的字母,您可以这样做:

For example, if you wanted to count the letters on your string, you would do:

{$pager->links|strlen}

如果要查看值 foo 是否在数组 $ bar 内,则可以执行以下操作:

And if you wanted to see if a value foo is within an array $bar you would do:

{'foo'|in_array:$bar}

因此,对于看起来像 func($ arg1,$ arg2,$ arg3)的php函数,这将转换为 {$ arg1 | func:$ arg2:$ arg3}

so, for a php function that looks like func($arg1, $arg2, $arg3), this translates to {$arg1|func:$arg2:$arg3}

不用说首选方法是(1),我只建议第二种方法,因为我认为这很有趣.

Needless to say the preferred method is (1), I only suggested the 2nd because, to my opinion, it is interesting to know.

这篇关于使用正则表达式替换为smarty模板系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆