A
A
Arman2019-01-11 11:21:03
Yii
Arman, 2019-01-11 11:21:03

Where and how best to collect a model show?

The template is advanced, but it probably won't matter from version 3.
There are ordinary AR models in common, in the frontend and backend they are practically not replaced,
almost naked, i.e. As we store in a DB so are given.
Now you need to show it all in view, give it json or something else. For the API, it would probably be appropriate to give as is +/- small helpers, but for HTML (frontend / backend) what is the best way to do it?
1. Store views directly in the model

<?php
echo $post->publicDate(); // <date format="***">23 янв 2019</date>
echo $post->icon(); // <span class="icon icon-hot"></date>
echo $post->shortCardView(); // <artcile>.$title..$descr..$date</artclie>

2. Inherit from \yii\helpers\Htmland use something likePostHtml
<?php
echo PostHtml::publicDate($post);
echo PostHtml::icon($post);
echo PostHtml::shortCardView($post);

$postHtml = PostHtml::create($post);
echo $postHtml->publicDate();
echo $postHtml->icon();
echo $postHtml->shortCardView();

3. Through the creation of widgets:
<?php
echo PostWidget::publicDate($post);
echo PostWidget::icon($post);
echo PostWidget::shortCardView($post);

I realized that there are a lot of views in copy-pastes and I don’t really want to send html to the model, but how would the models belong to applications (frontend / backend) and maybe it’s not scary to store layout here?
Or is there an easier option?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2019-01-15
@Arik

Definitely not, if for some reason you don’t want to see it in the view, then we pack it either in the Helper or in the Widget.
The choice between a widget and a helper depends on the complexity of the layout and / or the presence of dependencies (js files, fonts, etc.).
In the context of this question and now it does not matter if it is advanced or basic or some other template.
I can’t imagine a situation in which we transfer data from the controller in Json format. This is only for ajax requests, but then there is no view as such.
Well, if this is a REST api, then there is a different controller and models have additional methods. I don't quite understand what "as is" means.

D
Daniil Kozlov, 2019-01-11
@Kozlovdaniil

Greetings. In models, the layout to store is in any case bad. Violation of MVC in its purest form :)
I transfer such actions to widgets.
Between a helper and a widget, I would still choose a widget. This is if some kind of logic should be in the code. Type, selection of the active element.
Otherwise, you can put it in a separate view, somewhere in common and render in other views

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question