我目前正在搜索和替换这样的网页模板:
$template = <<<TEMP
<html>
<head>
<title>[{pageTitle}]</title>
</head>
[{menuA}]
[{menuB}]
[{bodyContent}]
</html>
<<<TEMP;
Run Code Online (Sandbox Code Playgroud)
以上内容放在一个单独的文件中.
然后,我做:
$template = str_replace("[{pageTitle}]",$pageTitle,$template);
$template = str_replace("[{menuA}]",$menuA,$template);
$template = str_replace("[{menuB}]",$menuB,$template);
$template = str_replace("[{bodyContent}]",$bodyContent,$template);
//Some 10 more similar to the above go here.
echo $template;
Run Code Online (Sandbox Code Playgroud)
问题是,总共有15个,就像上面那样.
有没有更好/更清洁/专业的方式来做到这一点(搜索和替换,或以不同的方式完成整个事情).我发现这非常混乱和不专业.
小智 3
是的,您可以定义要替换的事物的数组,以及要替换的事物的另一个数组。
$array1 = array("[{pageTitle}]", "[{menuA}]");
$array2 = array($pageTitle, $menuA);
$template = str_replace($array1 , $array2 , $template);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
113 次 |
| 最近记录: |