PHPのテンプレートエンジン

一般的に、私は実際に書く方法を知りません、通常はコードだけが出てきますが、私は試してみます:)
サイトを開発するときに他の誰かの既製のソリューションを使用することは私には根づきませんでした。このために、おそらく類推によって自分自身をたくさん書きますが、将来このコードを理解できることを完全に理解し、期待しています。 どういうわけか、既製のものがたくさんあるのに、なぜあなたがエンジンを書いているのかという質問を受けました。目的に合わせてモジュールを接続し、データベースを構成し、ハンマーで打ってアップグレードしてください。 本質的に答えることはできませんでしたが、アイデアを伝えました-新しいものをコーディングし、一般的に理解するのが好きです。
まあ、実際に私は何について話しているのですか。 エンジンの次の変更で、彼はテンプレートエンジンのようなものを書くことにしました。 以前はオプションがありましたが、あまり便利ではなかったため、多くを監視するために多くの編集を行う必要がありました。

私はこのトピックに関する記事を読みましたが、1つの実装を除いて、何とか新しいものは見つかりませんでした。

一番下の行は、テンプレートがフォームのテキストファイルであるということでした
[*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/J24826/


All Articles