これは、私が数年前に開発した1つの古いフレームワークで、preg_replace_callbackを使用して、単純なテンプレートパーサーを実行していた方法です。
基本的に、templateParserにソーステンプレートを提供し、コールバック関数内でトークンの発生を処理します。これはスケルトンです。もちろん、カスタム実装を作成し、HEADER *、*FOOTERなどのトークンに一致するように正規表現を設計する必要があります。
<?php
/**
* @param string $tpl
* The template source, as a string.
*/
function templateParser($tpl) {
$tokenRegex = "/your_token_regex/";
$tpl = preg_replace_callback($tokenRegex , 'template_callback', $tpl);
return $tpl;
}
function template_callback($matches) {
$element = $matches[0];
// Element is the matched token inside your template
if (function_exists($element)) {
return $element();
} else if ($element == 'HEADER*') {
return your_header_handler();
} else {
throw new Exception('Token handler not found.');
}
}
?>