Customize JavaScript and CSS

As mentioned before, in this chapter, you will discover how to use frontend interaction. Using JavaScript and CSS is fundamental to customize frontend output.

Differently from Yii1, where calling JavaScript and CSS scripts and files was done using the Yii::app() singleton, in the new framework version, Yii2, this task is part of the yii\web\View class.

There are two ways to call JavaScript or CSS: either directly passing the code to be executed or passing the path file.

Note

When passing the code directly to be executed, we will use the Heredoc syntax provided by PHP to avoid handling strings escaping.

The registerJs() function allows us to execute the JavaScript code with three parameters:

  • The first parameter is the JavaScript ...

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.