how to put tag in string in smarty template

2019-09-20 12:41发布

问题:

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



标签: smarty put