D
D
Dmitry STEM2018-08-07 20:03:43
PHP
Dmitry STEM, 2018-08-07 20:03:43

template engine. How is it better?

I don’t even know how to sum it up correctly, so I’ll just give a couple of examples.
First option:

ob_start();
$items = $db.... // Просто пример
while($item = $items->fetch_assoc()) {
echo  $this->view->generate('catalog/company-item.html', $item);
}

$data = [
'companies' => ob_get_clean(),
'id' => $this->id
];

return $this->view->render('catalog/main.html', $data);

generate() - loads the template file into the buffer, with the added data ($data)
render() - the same, but adds the data to the base template, which already contains the menu / footer and so on.
And the second option is to get just a list of all elements in the form of an array, and in the template already use a loop and display the necessary data.
What are the pros and cons:
Option 1:
+ No need to run through the files and change some class of the element (for example) in each cycle, but just change in one file
- Over time, a bunch of files for the template appear and you can get confused (although, given that there is a separate folder for each "module", there will not be so many files to get confused in them)
Option 2:
+ Fewer files
(I'm not sure about the correctness of the judgment) + This option is faster
- Uses conditions and loops in the template file (I want to separate the logic from the presentation as much as possible)
I want to hear your opinion on both options.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Immortal_pony, 2018-08-07
@Immortal_pony

https://medium.com/@DonnaInsolita/the-evolution-of...
The first option is step 1.
The second option is ~step 4.
In short, both options are weak, but the second is closer to the correct one. Keep learning theory. Check out the right way . Understand how a similar architecture is implemented in popular frameworks.

O
OnYourLips, 2018-08-07
@OnYourLips

Of these options, the second is definitely better.
And look towards twig, learn how to use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question