how to put <b> tag in string in smarty template

老子叫甜甜 提交于 2019-12-25 19:01:09

问题


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;

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


回答1:


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);

and in Smarty template file:

{$dbContent}

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');
}

and inside bold_template.tpl you could have:

<b>{$inVal}</b>

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



来源:https://stackoverflow.com/questions/25979515/how-to-put-b-tag-in-string-in-smarty-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!