Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...