📜 ⬆️ ⬇️

Php template engine

I don’t know how to write at all, usually only the code comes out, but I will try :)
I haven’t got used to the ability to use someone else’s ready-made solutions when developing websites and therefore I’m writing a lot myself, perhaps by analogy, but with full understanding and hope that in the future I can figure out this code. Somehow I got a question saying why are you writing your engine if you have a bunch of ready-made ones, take them and upgrade them for your purpose: connect the modules, tune and clog the database. As a matter of fact, I couldn’t answer, but I gave the idea - I like the code, and generally understand the new one.
Well, actually what I mean. In the next alteration of my engine I decided to write something like a template engine. There used to be an option, but not so convenient that you had to rule a lot, watch over many.

I read articles on this topic and somehow did not find anything new, except for one implementation.

The bottom line was that the template was a text file of the form
[*IF(blabla):*] present[*ELSE:*] [*ENDIF*]
.............

php, [* <? *] ?>
str_replace(array('[*','*]'), array('<?','?>'))

php .
ob_start();
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
include($templateCache);
error_reporting(E_ALL);
$text = ob_get_contents();
ob_end_clean();


,
ob_start();
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
eval('?>'.$codeFromTemplate.'<?');
error_reporting(E_ALL);
$text = ob_get_contents();
ob_end_clean();


- , , :) - , .
, "" , .
, .
, — .
foreach($templateVars as $k=>$v)
$$k = $v;


, Smarty . , , .
, demo, ...
[* *] . { }

{include 'last.tpl'}
{include ('last.tpl', ' ')}

, .
RegexBuddy( ) include

{\s*include\s*\({0,1}(\'{0,1}[$A-Za-z.0-9_\-]+\'{0,1})(?:(,{0,1}\s*\'{0,1}[$A-Za-z.0-9_\-]+\'{0,1})|\s*)\){0,1}\s*}

, :)
preg_replace
a , <?php w_include(${1}${2}); ?> .

w_include , .

, ( )
{if }{else} {/if}

{if }{/if}

, .

{if $a}
{if $b}
1
{/if}
2
{/if}

,
<? if($a){ ?>
{if $b}
1
<? } ?>
2
{/if}

, preg_replace . Smarty . .
,
<? if($a): ?>
...
<? endif; ?>


'/{\s*if\s*(.*?)\s*}/' => '<?php if(${1}): ?>'
'/{\s*else\s*}/' => '<?php else: ?>'
'/{\s*\/if\s*}/' => '<?php endif; ?>'


:)
, .

class t_wls_templater{
protected $dirTemplate = '';
protected $templateVars = array();
protected $templateFile, $templateCache;
// ---------------- preg
protected $wls_templater_preg = array(

// {include ('tpl', 'f')} {include 'tpl', 't'} { include 'tpl' } etc...
'/{\s*include\s*\({0,1}(\'{0,1}[$A-Za-z.0-9_\-]+\'{0,1})(?:(,{0,1}\s*\'{0,1}[$A-Za-z.0-9_\-]+\'{0,1})|\s*)\){0,1}\s*}/' => '<?php $this->w_include(${1}${2}); ?>',
// {if true } exit(); else {foo();} {/if}
'/{\s*if\s*(.*?)\s*}/' => '<?php if(${1}): ?>',
'/{\s*else\s*}/' => '<?php else: ?>',
// {for ($i=0;$i<$num;$i++)}echo $foo;{/endfor}
'/{\s*for\s*\((.*?);(.*?);(.*?)\)}/' => '<? for(${1}; ${2}; ${3}): ?>',
'/{\s*\/(for|if)\s*}/' => '<?php end${1}; ?>',
// { all text }
'/{(.*?)}/' => '<?${1}?>'
);
// ---------------- end preg
public function __construct($dir=''){
$this->dirTemplate = empty($dir)?.'./templates/':$dir;
}

public function assign($name,$value){
$this->templateVars[$name] = $value;
}

public function parse($filename,$innerTemplateName=''){
// create vars what's used in templates
$THIS_TEMPLATE_NAME = $innerTemplateName;
$THIS_TEMPLATE_FILE = $filename;
$THIS_TEMPLATE_DIR = $this->dirTemplate;
foreach($this->templateVars as $k=>$v){
$$k = $v;
}
// end create
if(!is_file($this->dirTemplate.$filename)){
return false;
}
$this->templateFile = $this->dirTemplate.$filename;
$this->templateCache = './templates_cache/_'.$_tmp['name'].'_'.$filename.'.php';

$orig_time = filemtime($this->templateFile);

if(is_file($this->templateCache)){
$cash_time = filemtime($this->templateCache);
if($cash_time>$orig_time){
$this->_deb->addEvent('info', 'Use cache for template '.$filename.' ');
ob_start();
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
include($this->templateCache);
error_reporting(E_ALL);
$text = ob_get_contents();
ob_end_clean();
return $text;
}
}

$text = file_get_contents($this->templateFile);
foreach($this->wls_templater_preg as $preg=>$replace){
$text = preg_replace($preg, $replace, $text);
}
$f = fopen($this->templateCache,'w');
fwrite($f,$text);
fclose($f);
ob_start();
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
eval('?>'.$text.'<?');
error_reporting(E_ALL);
$text = ob_get_contents();
ob_end_clean();
return $text;
}
protected function w_include($fname, $innerTemplateName = ''){
$_tmp = $this->parse($fname, $innerTemplateName);
echo $_tmp;
}
}

')

Source: https://habr.com/ru/post/24826/


All Articles