如何在 smarty 模板中将 <b> 标签放入字符串中

问题描述 投票:0回答:1

我正在使用 SMARTY,我需要在以下 php 代码中将

<b>
标签放入字符串中,我可以将标签放入字符串中

$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

php smarty
1个回答
0
投票

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

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

在 Smarty 模板文件中:

{$dbContent}

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

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

在bold_template.tpl中你可以有:

<b>{$inVal}</b>

但是如果你只添加

<b>
标签,那么将其放入 Smarty 模板中是没有意义的

© www.soinside.com 2019 - 2024. All rights reserved.