PHP Simple Template Technology

Source: Internet
Author: User
Tags php code

<?php

/**php Simple Template Technology
* Author: Cow Coax
* qq:455703030
* Statement: I am writing is not authentic, welcome to help me modify the master
* Free to spread, use, but please keep this information
*/

Class FLEA_VIEW_NHH
{
Template Storage Directory
public $viewDir = null;
Cache Storage Directory
public $cacheDir = null;
Whether to filter extra spaces, tabs, carriage returns in the cache
Public $filterCache = true;

Data to be displayed in the template
Private $data = Array ();

Public Function __construct ()
{
$viewConf = (array) flea::getappinf (' Viewconfig ');
if (empty ($viewConf)) {
Flea::loadclass (' Flea_exception_expectedfile ');
__throw (new flea_exception_expectedfile (' config file is not found: '. $viewConf));
}
$this->viewdir = $viewConf [' Viewdir '];
$this->cachedir = $viewConf [' Cachedir '];
$this->filtercache = $viewConf [' Filtercache '];
}

Public function assign ($var, $value)
{
$this->data[$var] = $value;
}

Public function display ($viewName)
{
$view = $this->viewdir. ' /'. $viewName. '. HTML ';
$cache = $this->cachedir. ' /'. $viewName. '. PHP ';
if (!file_exists ($view)) {
Flea::loadclass (' Flea_exception_expectedfile ');
__throw (New Flea_exception_expectedfile (' The view file is not found: '. $view));
}
if (Filemtime ($view) > @filemtime ($cache)) {
$this->parseview ($view, $cache);
}
if (file_exists ($cache)) {
if (!defined (' In_this_view ')) {define (' In_this_view ', TRUE);}
Include ($cache);
} else {
Flea::loadclass (' Flea_exception_expectedfile ');
__throw (' The cache file has not read in correctly: '. $cache));
}
}

Private Function Parseview ($view, $cache = null)
{
/* If the template does not exist, output the error message * *
if (!file_exists ($view)) {
Flea::loadclass (' Flea_exception_expectedfile ');
__throw (New Flea_exception_expectedfile (' The view file is not found: '. $view));
}
/* Read the template content as a string * *
$view = file_get_contents ($view);
/* Replace <!--{}--> with {{}}} *
$view = str_replace (Array (' <!--{', '}--> '), array (' {{'}} '), $view);
* * Clear Note/*
$view = Preg_replace ("/\{\{\*" (. *?) \*\}\}/", ', $view);
/* Analytic assign method introduced variable * *
$view = Preg_replace ('/\$ ([a-za-z0-9_]+)/', ' $this->data[\ ' \\1\ '] ', $view);
/* Machine template Internal definition variable *
$view = Preg_replace ('/\$\[([a-za-z0-9_]+) \]/', ' $\\1 ', $view);
/* Parse {{$var}}} basic variable output label * *
$view = Preg_replace ('/\{\{\s*\$ (. +?) \s*\}\}/', ' echo $\\1;?> ', $view);
/* Parse {include}} label * *
$view = Preg_replace ('/\{\{\s*include\s+ (. +?) \s*\}\}/', ' Include \\1;?> ', $view);
/* Parse {{html}} label * *
$view = Preg_replace ('/\{\{\s*html\s+ (. +?) \s*\}\}/', ' Echo file_get_contents (\\1);?> ', $view);
/* Parse {{code}} label * *
$view = Preg_replace ('/\{\{\s*code\s+ (. +?) \s*\}\}/', ' \\1?> ', $view);
/* Parse {{if}}} label * *
$view = Preg_replace ('/\{\{\s*if\s+ (. +?) \s*\}\}/', ' if (\\1) {?> ', $view);
/* Parse {{ElseIf}}} Label * *
$view = Preg_replace ('/\{\{\s*elseif\s+ (. +?) \s*\}\}/', ' } elseif (\\1) {?> ', $view);
/* Parse {{while}} label * *
$view = Preg_replace ('/\{\{\s*while\s+ (. +?) \s*\}\}/', ' while (\\1) {?> ', $view);
/* Parse {{foreach}} label * *
$view = Preg_replace ('/\{\{\s*foreach\s+ (. +?) \s*\}\}/', ' foreach (\\1) {?> ', $view);
/* Parse {for}} label * *
$view = Preg_replace ('/\{\{\s*for\s+ (. +?) \s*\}\}/', ' for (\\1) {?> ', $view);
/* Parse {ELSE}} label * *
$view = preg_replace ('/\{\{\s*else\s*\}\}/', ';} else {?> ', $view);
/* Parse {{/while}},{{/if}},{{/for}},{{}} Label * *
$view = preg_replace (' #\{\{\s*/[a-z]+\s*\}\}# ', ';}?> ', $view);
/* Recursive resolution of nested child templates * *
if (Preg_match_all) ('/\{\{\s*view\s+ (. +?) \s*\}\}/', $view, $viewsInView)) {
/* Iterate through all the child templates in this template and save the parsing results to an array $views * *
$viewInViews = Array ();
foreach ($viewsInView [1] as $viewInView) {
$viewInViews [] = $this->parseview (eval (' return '. $viewInView. '; '));
}
/* Replace the label of all the child templates in the template with their corresponding parsing results.
$view = Str_replace ($viewsInView [0], $viewInViews, $view);
}
* * Merge adjacent label Resolution results
$view = preg_replace ('/\?>\s*<\?/', ', ', $view);
/* If the cache filter is turned on, then filter out the extra space, Tab, enter * *
if ($this->filtercache = = True) {
$view = preg_replace ('/\s+/', ', ', $view);
}
/* If you are parsing a child template, return the result of the resolution * *
if ($cache = = null) {return $view;}
/* Add code to prevent direct access to cache * *
$view = ';? if (!defined ("In_this_view")) Exit ("The page is not found!"); ?> '. $view;
/* Write template resolution results to the cached file * *
@file_put_contents ($cache, $view);
}
}

/**
* One, configuration
* Configuration file Please modify in the Flea configuration file
* Format is:
* ' Viewconfig ' => Array (
* ' Viewdir ' => ',//template file path
* ' Cachedir ' => ',//cache file path
* ' Filtercache ' => true,//whether the cache is optimized after the template has been parsed
* );
*
* Second, the label
* template file suffix named. html
* The label supports both <!--{}--> or {{}}}, you can use it freely according to the situation
* 1, {{* * *}} To add comments, only support single line, multiline do not know how to write, depressed
* 2, {{$ABC}} output a variable, formatted output of the variable use the <{code}> tag to perform a function to format the output
* 3, {{$[ABC]}} output a variable defined inside a template, with the variable name enclosed in brackets
* 4, {include ' abc/bcd.php '}} contains a dynamic file in which you can use variables and templates that are defined internally by the Assign method, but you cannot use the tags supported by this engine
* 5, {{{html ' abc/bcd.html '}} contains a static file, which is available only under PHP5, and contains no variables or labels that can be used in files that are several times the performance of the include tag
* 6, {{code echo substr ($ABC, 1)}} executes a section of PHP code, including assignment, function, etc.
* 7, {{if $abc ==1}} and {{ElseIf $abc ==2}} and {{/if}}}, Branch
* 8, {{while $abc <1000}} and {{/while}}} while loop
* 9, {{for $i =0; $i <1000; $i + +}} and {{/for}}} for loop
* 10, {{foreach $abc as $k => $v}} and {{/foreach}} a Foreach Loop
* 11, {view ' abc/bcd.html '}} nested another template, support the infinite nesting of templates
*
Attention
* 1,include,html,view tags you can use constants to specify the path, which is more convenient
* The variable introduced by 2,assign takes a different namespace than the variables defined inside the template, so it can be duplicate
* 3, for the while,foreach,for loop need to introduce variables, it is best to use the template defined within the variable
* 4, template internal definition variable {{code $[i]=1}}}
* 5, all tags can not contain {} curly braces, or can not correctly parse
* 6, all the label parameters must be written as a single line, otherwise it will not parse correctly
* 7, when debugging, in order to facilitate the viewing of the results of the cache can turn off caching optimization, when published, recommended to open, to improve performance
* 8, this template engine can only be used under the flea, if you want to do not rely on the flea version, please modify
* 9, the parameters in the label using PHP syntax
* 10, the reference and output of the array {{$ABC [' ID ']}}
* 11, all labels are lowercase
* 12, the parameter of the label cannot contain {{or}}, or it will parse the error
*/

?>

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.