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 code block to be registered
The second parameter is the position where the JavaScript tag should be inserted (the header, the beginning of the body section, the end of the body section, enclosed within the jQuery
load()
method, or enclosed within...