$title = 'My title' ; include( 'templates/index.html' ); * This source code was highlighted with Source Code Highlighter .
$title = 'My title' ; include( 'templates/index.html' ); * This source code was highlighted with Source Code Highlighter .
$title = 'My title' ; include( 'templates/index.html' ); * This source code was highlighted with Source Code Highlighter .
$title = 'My title' ; include( 'templates/index.html' ); * This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- < html > < head > < title > <? php echo $ title ? > </ title > </ head >
- <! - ... ->
* This source code was highlighted with Source Code Highlighter .
- class STempException extends Exception {}
* This source code was highlighted with Source Code Highlighter .
- class STemp
- {
- / **
- * Directory names templates are located.
- *
- * @var string.
- * @access private.
- * /
- private $ path;
- / **
- * Name of the template.
- *
- * @var string.
- * @access private.
- * /
- private $ template;
- / **
- * Where assigned template vars are kept.
- *
- * @var array.
- * @access private.
- * /
- private $ variables = array ();
- / **
- * Parameters of the template engine.
- *
- * @var array.
- * @access private
- * /
- private $ params = array (
- 'xss_protection' => true
- 'exit_after_display' => true
- 'endofline_to_br' => false
- );
- / **
- * File that include in template.
- *
- * @var string.
- * @access private.
- * /
- private $ include_file;
- / **
- * The class constructor. Set where the templates are located.
- *
- * @param where the templates are located, the default 'templates /'.
- * @access public.
- * /
- public function __construct ($ path = 'templates /' )
- {
- $ this -> path = $ path;
- }
- / **
- * Set parameters of template engine.
- *
- * @param string $ param name of the parameter.
- * @param bool $ value value of the parameter.
- * @return bool TRUE if parameter set, FALSE if didn't set.
- * @access public.
- * /
- public function setParam ($ param, $ value )
- {
- if (isset ($ this -> params [$ param])) {
- $ this -> params [$ param] = $ value ;
- return true ;
- }
- return false ;
- }
- / **
- *
- * @param string $ include_file path to include file.
- * @access public.
- * /
- public function setIncludeFile ($ include_file)
- {
- $ this -> include_file = $ this -> path. $ include_file;
- if (! file_exists ($ this -> path. $ include_file))
- throw new STempException ( 'Include file' . $ this -> include_file. 'not exitst' );
- }
- / **
- * Assigns values to template variables.
- *
- * @param string $ name the template variable name.
- * @param mixed $ value.
- * @access public.
- * /
- public function assign ($ name, $ value )
- {
- $ this -> variables [$ name] = $ value ;
- }
- / **
- * Executes and displays the template results.
- *
- * @param string $ template the template name.
- * @access public.
- * /
- public function display ($ template)
- {
- $ this -> template = $ this -> path. $ template;
- if (! file_exists ($ this -> template))
- throw new STempException ( 'Template file' . $ template. 'not exitst' );
- require_once ($ this -> template);
- if ($ this -> params [ 'exit_after_display' ])
- exit;
- }
- / **
- * Get value of template variable.
- *
- * @param string $ name the template variable name.
- * @return mixed variable FALSE if variable not set.
- * @access private.
- * /
- private function __get ($ name)
- {
- if (isset ($ this -> variables [$ name])) {
- $ variable = $ this -> variables [$ name];
- if ($ this -> params [ 'xss_protection' ])
- $ variable = $ this -> xssProtection ($ variable);
- if ($ this -> params [ 'endofline_to_br' ])
- $ variable = $ this -> endoflineToBr ($ variable);
- return $ variable;
- }
- return NULL;
- }
- / **
- * Include file
- *
- * @access private
- * /
- private function includeFile ()
- {
- if (! file_exists ($ this -> include_file))
- throw new STempException ( 'Include file' . $ this -> include_file. 'not found' );
- require_once ($ this -> include_file);
- }
- / **
- * For the formation of endings of words.
- *
- * @param int $ value number.
- * @param string $ word0 word in the singular.
- * @param string $ word1 word in the plural (2, 3).
- * @param string $ word2 word in the plural.
- * @param string $ separator separator, default ''.
- * @return string formed words
- * @access private.
- * /
- private function morph ($ value , $ word0, $ word1, $ word2, $ separator = '' )
- {
- if (preg_match ( '/ 1 \ d $ /' , $ value ))
- return $ value . $ separator. $ word2;
- elseif (preg_match ( '/ 1 $ /' , $ value ))
- return $ value . $ separator. $ word0;
- elseif (preg_match ( '/ (2 | 3 | 4) $ /' , $ value ))
- return $ value . $ separator. $ word1;
- else
- return $ value . $ separator. $ word2;
- }
- / **
- * For protection from XSS.
- *
- * @param mixed $ variable data for protection.
- * @return mixed protected data.
- * @access private.
- * /
- private function xssProtection ($ variable)
- {
- if (is_array ($ variable)) {
- $ protected = array ();
- foreach ($ variable as $ key => $ value )
- $ protected [$ key] = $ this -> xssProtection ($ value );
- return $ protected ;
- }
- return htmlspecialchars ($ variable);
- }
- / **
- * Inserts HTML line breaks before all newlines in a string.
- *
- * @param mixed $ variable data for protection.
- * @return mixed data where string with <br /> inserted before all newlines.
- * @access private.
- * /
- private function endoflineToBr ($ variable)
- {
- if (is_array ($ variable)) {
- $ protected = array ();
- foreach ($ variable as $ key => $ value )
- $ protected [$ key] = $ this -> endoflineToBr ($ value );
- return $ protected ;
- }
- return nl2br ($ variable);
- }
- }
* This source code was highlighted with Source Code Highlighter .
- $ stemp = new STemp ();
- $ stemp-> assign ( "title" , $ article [ 'title' ]);
- $ stemp-> assign ( "article" , $ article);
- $ stemp-> assign ( "comments" , $ comments);
- try {
- $ stemp-> setIncludeFile ( "article.tpl.php" );
- $ stemp-> display ( "index.tpl.php" );
- } catch (STempException $ e) {
- die ( 'STemp error:' . $ e-> getMessage ());
- }
* This source code was highlighted with Source Code Highlighter .
- < html >
- < head >
- < title > <? php echo $ this- > title? > </ title >
- </ head >
- < body >
- <? php $ this- > includeFile ()? >
- </ body >
- </ html >
* This source code was highlighted with Source Code Highlighter .
- < h1 > <? php echo $ this- > article ['title']? > </ h1 >
- <? php $ this- > setParam ('xss_protection', false); $ this- > setParam ('endofline_to_br', true)? >
- < div class = "content" >
- <? php echo $ this- > article ['content']? >
- </ div >
- < p > <? php echo $ this- > morph (count ($ this- > comments), 'comment', 'comment', 'comments')? > : </ p >
- <? php $ this- > setParam ('xss_protecttion', true)? >
- <? php foreach ($ this- > comments as $ key = > $ value) {? >
- < p class = "user" > <? php echo $ value [ 'username' ]? > : </ p >
- < p class = "comment" > <? php echo $ value [ 'text' ]? > </ p >
- <? php }? >
Source: https://habr.com/ru/post/45259/