Answer the question
In order to leave comments, you need to log in
Why in yii2 classes and objects for generating html?
Sorry, but I just can't understand why the hell do we need all sorts of classes like Modal or Html?
I may be misunderstanding something, but somehow it’s more familiar and clearer to see something like:
echo '<a class="btn btn-success" href="/index.php?r=/client/create">Добавить клиента</a>';
<?= Html::a('Добавить клиента', ['create'], ['class' => 'btn btn-success']) ?>
Answer the question
In order to leave comments, you need to log in
Well, you give, you call one of the sensible parts of the framework.
In a large and complex project, as a rule, there is a large and complex logic, and large and complex logic implies large and complex conditions. Fencing conditions in a view is a thankless task, because a view is just a view and nothing more, and the simpler it is, the better.
Next on the list.
You made a mistake in your HTML example that will cost you flexibility.
A url generator should be used here, but then the code becomes more complicated and it won’t work to trump “simplicity and clarity”, right? This allows, for example, at the initial stage, not to worry about “beautiful” URLs, referring to the action / controller, but towards the end, set up routing throughout the project at a time, without changing anything in the view.
Try to draw checkbox in html. Not simple, but the choice of which will depend on the parameter of the model. I'll suggest:
Not so beautiful anymore?
You can also draw a select to really make sure you need an Html helper.
At this point, you might be talking about putting HTML generation into files, but I'll reassure you. Firstly, this will either generate a lot of files and a bunch of conditions (one form with a custom select, another with a stock one, but with a label, the third one has something else), and connecting this bunch of files will take longer, and without compiling templates - constantly longer. Secondly, you lose flexibility again.
There are problems with Yii, but the Html helper is not one of them.
Helper classes are needed to dynamically generate html. Let's say if this or that attribute has parameters dependent on the situation. You will not build noodles from php and html... although who knows :-) .
As well as a bonus, this is the stability and testability of the widgets. Because these helpers are used in widgets, and the developers know 100% how they behave, because unit tests have already been written for them
just a week ago, while making this widget, I recalled a similar question on the yii forum.
https://github.com/zelenin/yii2-recaptcha-widget/b...
imagine how you would collect this div and its attributes depending on how they are specified in the settings.
div' . (isset($this->dataKey) ? ' data-key="blabla"' : ''). ' and so on, all attributes, classes, IDs, etc. It's inconvenient, unusable, hard to track down bugs.
Rather a matter of taste. Although there are situations when there is a lot of php in the attributes of the same tag "a" and it will be more convenient through Html::a.
There are also many methods for generating bootstrap elements, which greatly simplifies life in case of developing some kind of admin panel, if you are not a frontender)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question