Simple PHP template engine implementation example

Source: Internet
Author: User
Tags foreach flock php template php tutorial

Simple php Tutorial template engine implementation example

<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312"/>
</Head>

<Body>
{$ Var}
<Br/>

{Loop $ arr $ v}
{$ V} |
{/Loop}
</Body>
</Html>

Index. php call File

<? Php
Header ("content-type: text/html; charset = utf-8 ");
Require_once "./include/common. inc. php ";

$ Var = 'abc ';
$ Arr = array (1, 2, 3 );

Include template ('index ');
?>

/Common. inc. Php file

<? Php
Define ('in _ site', true );
Define ('site _ root', substr (dirname (_ file _), 0,-7 ));

Require_once site_root. './include/template. func. Php ';

$ Tplrefresh = 1; // Set whether to check for updates
$ Tpldir = site_root. './templates/default/'; // template storage directory
$ Objdir = site_root. './sitedata/templates/'; // Directory for storing template compilation files
?>

Template. func. Php file

<?

If (! Defined ('in _ site ')){
Exit ('Access denied! ');
}

Function checktplrefresh ($ tplfile, $ timecompare ){
Global $ tplrefresh;

If ($ tplrefresh = 1 & @ filemtime ($ tplfile )! = $ Timecompare ){
Parse_template ($ tplfile );
    }
Return true;
}

Function template ($ file ){
Global $ tpldir, $ objdir;

$ Objfile = $ objdir. $ file. '. tpl. Php ';

If (! File_exists ($ objfile )){
$ Tplfile = effectpldir.20.file.'.htm ';
Parse_template ($ tplfile );
    }
Return $ objfile;
}

Function parse_template ($ tplfile ){
Global $ tpldir, $ objdir;

$ File = basename ($ tplfile, '.htm ');
$ Objfile = $ objdir. $ file. '. tpl. Php ';

If (! $ Fp = @ fopen ($ tplfile, 'r ')){
Exit ("current template file '$ tplfile' not found or have no access! ");
 }
$ Template = fread ($ fp, max (filesize ($ tplfile), 1 ));
Fclose ($ fp );

$ Template = preg_replace ("/[nrt] * {subtemplates + ([a-z0-9 _:] +)} [nrt] */is", "<? Php include template ('1');?> ", $ Template );

$ Template = preg_replace ("/([nr] +) t +/s", "1", $ template );
$ Template = preg_replace ("/<! -- {(. + ?)} -->/S "," {1} ", $ template );
$ Template = preg_replace ("/{($ [A-Za-z0-9 _ [] '" $. X7f-xff] +)}/s "," <? Php echo 1;?> ", $ Template );

$ Template = preg_replace ("/[nrt] * {evals + (. + ?)} [Nrt] */is "," <? Php 1;?> ", $ Template );
$ Template = preg_replace ("/[nrt] * {echos + (. + ?)} [Nrt] */is "," <? Php echo 1;?> ", $ Template );
$ Template = preg_replace ("/([nrt] *) {elseifs + (. + ?)} ([Nrt] *)/is "," 1 <? Php} elseif (2) {?> 3 ", $ template );
$ Template = preg_replace ("/([nrt] *) {else} ([nrt] *)/is", "1 <? Php} else {?> 2 ", $ template );

For ($ I = 0; $ I <5; $ I ++ ){
$ Template = preg_replace ("/[nrt] * {loops + (s +) s + (s +)} [nr] * (. + ?) [Nr] * {/loop} [nrt] */is "," <? Php if (is_array (1) {foreach (1 as 2) {?> 3 <? Php }}?> ", $ Template );
$ Template = preg_replace ("/[nrt] * {loops + (s +) s + (s +)} [nrt] * (. + ?) [Nrt] * {/loop} [nrt] */is "," <? Php if (is_array (1) {foreach (1 as 2 => 3) {?> 4 <? Php }}?> ", $ Template );
$ Template = preg_replace ("/([nrt] *) {ifs + (. + ?)} ([Nr] *) (. + ?) ([Nr] *) {/if} ([nrt] *)/is "," 1 <? Php if (2) {?> 345 <? Php }?> 6 ", $ template );
 }

$ Template = preg_replace ("/{([a-za-z_x7f-xff] [a-za-z0-9_x7f-xff] *)}/s", "<? Php echo 1;?> ", $ Template );
$ Template = preg_replace ("/?> [Nr] * <? Php/s "," ", $ template );

$ Template = preg_replace ("/" (http )? [W./:] +? [^ "] +? & [^ "] +? "/E", "transamp ('') ", $ template );
$ Template = preg_replace ("/<script [^>] *? Src = "(. + ?) "(. *?)> S * </script>/ise "," strips tutorial criptamp ('1', '2') ", $ template );

$ Template = "<? Php if (! Defined ('in _ site') exit ('Access denied! '); Checktplrefresh (' $ tplfile', ". time ().");?> N $ template ";

If (! $ Fp = @ fopen ($ objfile, 'wb ')){
Exit ("directory '$ objdir' not found or have no access! ");
 }
Flock ($ fp, lock_ex );
Fwrite ($ fp, $ template );
Flock ($ fp, lock_un );
Fclose ($ fp );
}

Function transamp ($ str ){
$ Str = str_replace ('&', '& amp;', $ str );
$ Str = str_replace ('& amp;', '& amp;', $ str );
$ Str = str_replace ('"', '"', $ str );
Return $ str;
}

Function stripscriptamp ($ s, $ extra ){
$ Extra = str_replace ('"', '"', $ extra );
$ S = str_replace ('& amp;', '&', $ s );
Return "<script src =" $ s "type =" text/webpage effects "$ extra> </script> ";
}
?>

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.