如何放置<b>在 smarty 模板中的字符串中标记 [英] how to put <b> tag in string in smarty template

查看:69
本文介绍了如何放置<b>在 smarty 模板中的字符串中标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SMARTY,我需要将 <b> 标记放在字符串中,在以下 php 代码中我可以将标记放在字符串中

I am using SMARTY and I need to put <b> tag in string in the following php code i can put tag in string

$search = 'this is my sample strangا';
$dbContent = 'this strang is for sample hello world';

$search = explode( ' ' , $search );
function wrapTag($inVal){
  return '<b>'.$inVal.'</b>';
}
$replace = array_map( 'wrapTag' , $search );

$dbContent = str_replace( $search , $replace , $dbContent );

echo $dbContent;

如何在 smarty 模板中使用此代码或如何将此代码转换为 smarty

how to use this code in smarty template or how to convert this code for smarty

推荐答案

在我看来没有必要把这样的代码放到 Smarty 模板中,所以你唯一应该做的就是

In my opinion there's no need put such code into Smarty template, so the only thing you should do is

$smarty->assign('dbContent', $dbContent);

在 Smarty 模板文件中:

and in Smarty template file:

{$dbContent}

您应该将逻辑和显示分开.在这种情况下,您不应该将此代码移至 Smarty.如果您的 wrapTag 函数包含大量 HTML,您可以这样做(我知道 global 不是很好的解决方案,但可能也可以通过其他方式完成):

You should separate logic and display. In this case you shouldn't rather move this code to Smarty. If Your wrapTag function contained a lot of HTML you could do it this way ( I know global is not nice solution but probably it could be done also in the other way):

function wrapTag($inVal){
  global $smarty;
  $smarty->assign('inVal', $inVal);
  return $smarty->fetch('bold_template.tpl');
}

在bold_template.tpl 中,您可以拥有:

and inside bold_template.tpl you could have:

<b>{$inVal}</b>

但如果你只添加 标签,那么把它放在 Smarty 模板中是没有意义的

But if you only add <b> tags there's no point to put it in Smarty template

这篇关于如何放置&lt;b&gt;在 smarty 模板中的字符串中标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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