Edit: Harika noktaları çevresinde, özel çiftleşmiş dil açıkçası gitmek için yoludur. Teşekkürler! Em>
Ben PHP ile çiftleşmiş yapmak için bu hızlı sınıf yazdı - Ben kullanıcılara çiftleşmiş açmak hiç olsaydı, bu kolayca işletilebilir olup olmadığını merak (değil acil planı, ama yolda düşünme) oldu.
class Template {
private $allowed_methods = array(
'if',
'switch',
'foreach',
'for',
'while'
);
private function secure_code($template_code) {
$php_section_pattern = '/\<\?(.*?)\?\>/';
$php_method_pattern = '/([a-zA-Z0-9_]+)[\s]*\(/';
preg_match_all($php_section_pattern, $template_code, $matches);
foreach (array_unique($matches[1]) as $index => $code_chunk) {
preg_match_all($php_method_pattern, $code_chunk, $sub_matches);
$code_allowed = true;
foreach ($sub_matches[1] as $method_name) {
if (!in_array($method_name, $this->allowed_methods)) {
$code_allowed = false;
break;
}
}
if (!$code_allowed) {
$template_code = str_replace($matches[0][$index], '', $template_code);
}
}
return $template_code;
}
public function render($template_code, $params) {
extract($params);
ob_start();
eval('?>'.$this->secure_code($template_code).'<?php ');
$result = ob_get_contents();
ob_end_clean();
return $result;
}
}
Örnek kullanım:
$template_code = '<?= $title ?><? foreach ($photos as $photo): ?><img src="<?= $photo ?>"><? endforeach ?>';
$params = array('title' => 'My Title', 'photos' => array('img1.jpg', 'img2.jpg'));
$template = new Template;
echo $template->render($template_code, $params);
Burada fikir, ben veritabanındaki şablonları (PHP kodu) depolamak ve daha sonra (eğer, vb) yalnızca izin izin yöntemleri için normal ifadeler kullanır sınıfı aracılığıyla çalıştırmak istiyorum olmasıdır. Herkes bu istismar ve keyfi PHP çalıştırmak için belirgin bir yol görüyor musun? Eğer öyleyse, muhtemelen böyle Smarty gibi bir çiftleşmiş dilin daha standart yol gidersiniz ...