Write your own smarty template engine without parsing php (please help)

Source: Internet
Author: User
Tags smarty template
Write your own smarty template engine without parsing php (please help) $ realcontent = $ this-> filecontent;


$ Mode = '/\{\$ ([\ w] + )\}/';
If (preg_match ($ mode, $ realcontent )){

Var_dump ($ assign_vars );

$ Realcontent = preg_replace ($ mode ," Assign_vars ['$ 1']?> ", $ Realcontent); // replace it with the variable injected in the index. php file


}
Return $ realcontent;


}

The code after running is
Assign_vars ['content']?>

Author: Assign_vars ['name']?> Assign_vars ['var']?>


Reply to discussion (solution)

             {\ $ 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", "china"); assign ("provice", "Guangdong province"); assign ("city", "Shenzhen city"); assign ("no ", "The page does not exist. unretrieved variables"); 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", "china"); $ template-> assign ("provice", "Guangdong province "); $ Template-> assign ("city", "Shenzhen city"); $ template-> assign ("no", "the page does not exist, unretrieved variable "); $ template-> replace (); $ template-> display ();?>

After smarty 3, PHP mixed write is not supported by default. you can replace it with smarty 2.

It's a false positive. it's a write imitation, not a silent write...

It's a false positive. it's a write imitation, not a silent write...


Understand the compilation class, all of which are regular expressions.

You only generated the php code and did not execute it.

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.