Answer the question
In order to leave comments, you need to log in
MVC. Generating html pages with a template engine and blocks (or collecting all the data for this)?
The request scheme is simple:
1. The user makes a request /news/1/
2. the router defines the controller and the method to be called based on the request ( news controller method show )
3. the controller calls the model that receives data on news No. 1
up to this point, everything great (here you can give the data to the template engine and insert it into the page, give it to the user)
BUT there are more "blocks" in the template, let's say a comment block, a popular news block. To get the result (draw these blocks), you need to call their controllers, which will pull out the data through the models.
I do not understand at what stage it is necessary to do this.
I imagine the whole model and the work of the "engine" so that all the data for drawing the page should already be in one place, which I will pass to the template.
In this case, I must know in advance which blocks will be on the page, but I don’t know how I will know this?
Answer the question
In order to leave comments, you need to log in
when setting the route configuration, you set a hard match, for example,
"/news/:id" -> controller = news, method = item
in this way you can get data and render only one html block. at the same time, according to the conditions of the task, several blocks must be rendered. output? and here:
"/news/:id" -> {
'main' -> controller = news, method = item,
'last_news' -> controller = news, method = last_items,
'now_logged_users' -> controller = users, method = last_logged,
}
etc. and in the template you mark the blocks 'main', 'last_news', 'now_logged_users'. and each block is rendered separately, and then inserted into the template. blocks are equal and independent. it is clear that you can assign some block by default, but it will work the same way - on an equal footing with other blocks. such work with blocks should be provided at the routing level, and if the engine does not allow it, then it is shit. there is a php CMS MODx, try it and see how snippets are organized in it. modx itself may not be suitable for high-end development, but it has the right idea of working with page structure and inserting content into pages.
You need to decide if you have a specific controller method that is responsible for only block output or page output. If the first, then do as @Masterme says , if the second, then walk around everything in the controller.
What prevents in the news control to call the methods of other controls to get additional information?
Create a separate entity - widgets - for separate blocks with narrow tasks "tag cloud", "last comments", etc. In them, take out the logic, and the representations of these blocks. You will insert these blocks into views, for example<?php echo $widget->render('tags'); ?>
@Webdesus @Masterme @ruFelix @BloodyHistory
Question about the controller using news as an example.
Explain what is the result of the controller's work in this case, the
$data array, var_dump of which will show us all the data related to the news page
or a ready-made page?
I want to do the following when generating the news page:
1. the controller requested data from the model, performed all the checks, the output received an $data array for the View (template) and the template name for the page (or it is inside $data, perhaps the response format will also be specified "html" or "json" for representation)
2. The view (View) received the page data and the name of the template (here either inserted this data into the template, or we are waiting for a check for blocks to insert all the data at once along with the block ones)
3. Now we are looking for blocks and find the "comments" block
4. launch the controller block, in response we get (data or html)
5. At this stage, we either
a) $ data array of all variables for rendering the page
b) or an array of html blocks
6. insert them into the template
Please advise the solution.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question