Answer the question
In order to leave comments, you need to log in
Is it bad to connect the same file 20 times?
Created an object, which is a block that is used on the site from 1 to 20 times. Previously, I had all the HTML in the class variable of the view of this object. In order to give it to the layout designer for work, I needed to create an HTML template for this block. Now it turns out that this template is included in the script 20 times. Is such a solution acceptable?
Code that is called 20 times:
$BlockData = $this->GetBlockData(); // Получаем данные, которые вставятся в блок
ob_start(); // Буферизуем
require('block.html'); // Загружаем шаблон, в него сразу подставляются все переменные из массива $BlockData
$BlockWithHTML = ob_get_contents(); // Сохраняем уже исполненный код в переменную
ob_end_clean(); // Очищаем буфер
return $BlockWithHTML; // Отдаем готовый HTML блока
Answer the question
In order to leave comments, you need to log in
Maybe something like this?
function getTemplate($tpl){
static $templates=array();
if (!isset($templates[$tpl])){
$templates[$tpl]=file_get_contents($tpl);
}
ob_start();
eval($templates[$tpl]);
return ob_get_clean();
}
This is absolutely normal if a template is included from a template.
Have you tried passing an array to the template and using loops? For example: www.smarty.net/docsv2/en/language.function.foreach
Maybe someone will be useful. Code execution time with 20 calls:
- HTML in a variable: 0.009 s
- 20 template connections: 0.012 s
- static variable + eval + file_get_contents: 0.011 s
For more calls, I recommend the solution proposed by BuriK666.
Code execution time with 100 calls:
- HTML in a variable: 0.037 s
- 20 template connections: 0.053 s
- static variable + eval + file_get_contents: 0.044 s
I stopped at 20 require's. Thanks everyone for the help!
Please advise better solutions.Consider using static templates (no executable code at all). After the template is loaded for the first time by the template engine, you can cache the contents of the template and, on subsequent downloads, serve it directly from the cache. More or less like this:
class SomeTemplater
{
protected $_templates = array();
public function loadTemplate($path) {
if (array_key_exists($path, $this->_templates)) {
$code = $this->_templates[$path];
}
else {
$code = file_exists($path)
? file_get_contents($path)
: false;
$this->_templates[$path] = $code;
}
return $code;
}
}
You are absolutely correct. I don’t know who came up with this, but making blocks on the page by including individual files is absolutely inefficient in terms of performance and clumsy in terms of architecture. A crutch, not a solution. It is logical to use functions or static class methods for this (bonus: we get autoloading and isolation of local variables). Template example:
class UserBlock {
public static function renderUserInfo($user) {
?>
... HTML code...
<?php
}
}
Usage example:
<?php foreach ($users as $user): ?>
{div}<?php UserBlock ::renderUserInfo($user); ?>{/div}
<?php endforeach ?>
To people who say it's bad to use functions in templates (why, by the way?), offer to come up with a simpler, smarter, faster, without the use of cumbersome libraries and a reliable solution, and then make sure that they fail.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question