Self-Dictation smarty template engine, do not parse PHP (for the master to help)
$realcontent = $this->filecontent;
$mode = '/\{\$ ([\w]+) \}/';
if (Preg_match ($mode, $realcontent)) {
Var_dump ($assign _vars);
$realcontent =preg_replace ($mode, " assign_vars[' $ ']?> ", $realcontent); /Replace with variables injected into the index.php file
}
return $realcontent;
}
After the code is run
assign_vars[' content ']?>
Author: assign_vars[' name ']?> Assign_vars[' var ']?>
------to solve the idea----------------------
$realcontent = file_get_contents ("template_test.html");
$tpl _string = <<< templeate
<title>{\ $title}</title>
Author: {\ $name}
Country: {\ $china}
Province: {\ $provice}
City: {\ $city}
Templeate;
$mode = '/\{\$ (\w+) \}/';
$assign _arr = Array ();
function assign ($name, $value) {
Global $assign _arr;
$assign _arr[$name] = empty ($value)? ": $value;
return $assign _arr;
}
function Replace_content ($match) {
Global $assign _arr;
if (!empty ($match)) {
return Array_key_exists ($match [1], $assign _arr)? $assign _arr[$match [1]]: ';
}
}
Assign ("title", "I Love You");
Assign ("name", "Iseagold");
Assign ("China", "Chinese");
Assign ("Provice", "Guangdong province");
Assign ("City", "Shenzhen");
Assign ("No", "page not present, non-fetched variable");
Echo Preg_replace_callback ($mode, "replace_content", $tpl _string);
=================================== Simple class version ==========================================================
Class Template {
Public $assign _items = Array ();
Public $content;
function __construct ($file) {
$this->content = file_get_contents ($file);
}
function assign ($name, $value) {
$this->assign_items[$name] = empty ($value)? "": $value;
}
function replace () {
$arr _assign_items = Array ();
$content = ";
$content = $this->content;
$arr _assign_items = $this->assign_items;
$pattern = '/\{\$ (\w+) \}/';
$this->content = preg_replace_callback ($pattern, function ($match) use ($arr _assign_items) {
if (!empty ($match)) {
if (Array_key_exists ($match [1], $arr _assign_items)) {
echo $arr _assign_items[$match [1]];
return $arr _assign_items[$match [1]];
}
}
}, $content);
}
function display () {
Echo $this->content;
}
}
$template = new Template ("template_test.html");
$template->assign ("title", "I Love You");
$template->assign ("name", "Iseagold");
$template->assign ("China", "Chinese");
$template->assign ("Provice", "Guangdong province");
$template->assign ("City", "Shenzhen");
$template->assign ("No", "page not present, non-fetched variable");
$template->replace ();
$template->display ();
?>
------to solve the idea----------------------
You only generated PHP code, and did not perform his