Just a basic newbie question to understand the reasoning. Why should one use HTML helpers available in Yii2, or can't we just type the tags if we are strong in our HTML skills.
Example: Seen in the basic views...
<h1><?= Html::encode($this->title) ?></h1>
Why should I not just type...
<h1>My Title</h1>
It's really up to you.
But using the framework helpers, widgets and coding styles, you can keep code consistency, reduce errors, bugs and even lower the security risks.
Using your example.
Imagine that
$this->title
is set to the name of a user in your main layout file:Now, let's imagine that an user managed to set his username to
<script>console.log('I can steal your cookies now!');</script>NotAHacker
in the registration form (also because you decided to save directly to the database instead of using the framework).That will render the following:
And you will see just this:
NotAHacker
And in the javascript console will appear
I can steal your cookies now!
That's a major security risk! The bad people out there can steal your cookies information, record activity, steal passwords, etc.
But that could be easily fixed, using the framework way.
And that will render:
And you will see:
<script>console.log('I can steal your cookies now!');</script>NotAHacker
But nothing will be executed!
So, what's the point? Frameworks like Yii2 develop their helpers and widgets so you can trust them that nothing bad will happen while you use their methods (and because if you ever come across encoding problems, you will love this helpers classes). This is crucial in a production environment, when you have a lot of variables and you can't let anything slip through your fingers.
TL;DR: If you trust your framework, use their available methods everytime you can.