Trying to simplify code and reduce duplication in yii2

In trying to simplify the code and reduce the duplications as much as possible in the development code in yii2 a doubt arose in the structure of the layouts and I wonder:

Why do we need "<head></head>, <body></body>, etc… tags when in php $this->beginXXX(), $this->endXXX() should do that job?

What do you think?

Because you want to have the freedom to put something between <body> and the content generated in $this->beginBody().
But mainly I believe you think about the problem in wrong way. The goal isn’t to “merge every possible mergable content together”, but have code what servers its purpose, which should by clear and practical. And purpose of the “beginXXX” isn’t outputting pieces of layout.
Hide generating “<body>” into beginBody() isn’t actually helpful, it makes things unclear.

I understand your efforts, it has definitely place in many situations, but force it everywhere without thinking about it is counterproductive.

1 Like

What I meant was
when you write code

<?= $this->beginBody() ?>
<?= $content ?>
<?= $this->endBody() ?>

It could be just

<?= $this->beginBody() ?>
<?= $content ?>
<?= $this->endBody() ?>

Wouldn’t it be better if beginBody() could generate the tag?
Less code to write and less confusion for beginners.
its like:

<here's a door>
<?= here->begin the door() ?>
<?= the door Itself ?>
<?= here->ends the door() ?>
<here's door no more>

It is Just an idea…

My question would be why do you want to mess up with layouts?
Once you get them right it is a forgotten thing.

Simple, imagine a multitenant app, deployed as SAAS where you a need to make a lot of layout versions. if you have to generate them dynamically from a CMS, if you have layouts like base, navbar, footer, content for each that are different for more than two views in frontend, as themes or view templates.

I have dealt with quiet complex SaaS app and we had no need to change layout.
Can you give a use case where you actually need to be changing layout files?

Unless it is a CMS, where you have to design some sort of layout engine for templates, I’m yet to meet such requirement. Am not saying it is not there, but never met it!

For example, a SaaS queue management system, where each client changes the layouts for the views of the arrival ticket portico of their consumers, the TV dashboard to show the call order, the ticket-line in the mobile webappView, the dashboard of each department, and so on.
But that’s not the point here.
What I pointed out is, to discuss that in the layouts or anywhere else, it wouldn’t make sense to have duplicates in the code, or repetition, or extra code justo because. That’s what jumped out at me in the layouts, in my opinion it doesn’t make sense to use html code for tag and php for start end… Even if you only use php we will have HTML::tag() and ->begin/end. it adds more lines of code and unnecessary complexity, as we have a function that states the begin and end why not make it do the complete job.

I second in this.

I love SIMPLICITY because it make things easy and clear. “Keep It Simple, Stupid” is my favorite words.

And, IMO, DRY principle generally serves simplicity to some degree, but 2 things aren’t the same. Too much dry makes things complicated and unclear.

1 Like

In that case what about creating segment of shareable code and put them in a file then use Render function to render in whichever view they are needed?