Answer the question
In order to leave comments, you need to log in
Is the implementation of templating bad and will it reduce the performance of the PHP project in general?
I needed a template engine, but only a simple one, without any heavy Smarty and Twig (PHP itself is a template engine).
In general, the Controller turned to the module, it worked out its own and now it's time to wrap it in a template:
<?
class controller {
static $class = false;
static function run (){
// Маршрутизация
Router::router();
self::$class = Router::$module;
// Шаблонизатор
self::show();
}
static function show (){
$data = array(
'title' => meta::$title,
'descr' => meta::$descr,
'key' => meta::$key,
'css' => css::style(),
'js' => js::getJs(),
'header' => Header::show(),
'buffer' => call_user_func(self::$class.'::show'),
'footer' => Footer::show(),
'other' => ''
);
view::tpl('index',$data);
}
}
foreach ($data as $var => $value){
str_replace("{$var}", $value, $tpl);
}
Answer the question
In order to leave comments, you need to log in
Yes, it's very bad. I propose two solutions:
1. Like yours, just use the strtr function:
2. Render using require wrapped in ob_start(); and ob_end(); Extract variables from the array using extract() and work with pure variables in the template file. For example:
$data = ['var1' => 1, 'var2' => 2];
View::render('index', $data);
extract($data);
require($viewFile);
<div class="container">
<?php echo $var1; ?>
<?=$var2 ?>
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question