【发布时间】:2018-09-14 20:52:00
【问题描述】:
您好,我正在尝试在 PHP 中执行一个非常简单的模板系统。
我愿意这样做:
- 读取一些 .md 文件
-
用数组中的对应变量更改所有 {{ variables }}
$data( array('variable1' => 'value', 'variable2' => 'value2')); - 管理变量不存在的情况,以防止出现“key not found in array”之类的错误。
目前我已经这样做了(不是在正则表达式中,它也不起作用)
class Markdown{
private static $folder = 'markdowns/';
public static function fill_template($template, $array){
$text = file_get_contents(self::$folder . $template . '.md');
foreach($array as $key => $value){
$text = str_replace('{{'.$key.'}}', $value, $text);
}
return $text;
}
}
有什么想法吗?
谢谢
【问题讨论】:
标签: php regex templating