Smarty The template engine, do not parse PHP (for the master to help)

Source: Internet
Author: User
Tags smarty template
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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.