Download the PHP package richardfan1126/yii2-js-register without Composer
On this page you can find all versions of the php package richardfan1126/yii2-js-register. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download richardfan1126/yii2-js-register
More information about richardfan1126/yii2-js-register
Files in richardfan1126/yii2-js-register
Package yii2-js-register
Short Description Yii2 widget to register JS into view
License MIT
Informations about the package yii2-js-register
yii2-js-register
Yii2 widget to register JS into view
Why do I need this?
How to register JS script into view in Yii2?
In Yii2, the way to register JS script into view is using \yii\web\View::registerJS(),
For example, if you want to initialize a jQuery plugin, you do:
Obviously, this is a very bad way because:
- Hard to read for human
- Hard to read for you editor (Means no syntax coloring for the JS code)
- You have to handle the single / double quote
This widget help you register JS script in Yii just as you write script block directly into HTML!
Getting Started
Installing
Install with Composer:
composer require richardfan1126/yii2-js-register "*"
or
php composer.phar require richardfan1126/yii2-js-register "*"
or add
"richardfan1126/yii2-js-register":"*"
to the require section of your composer.json file.
How to use
In your view file, wrap your JS script with <script> tag, and wrap all the things with this widget.
example:
IMPORTANT Remember not to add anything out of the <script> block.
Configurations
There are 2 configurations for this widget, which are the parameters passed into \yii\web\View::registerJS() method. example:
- key (optional)The key that identifies the JS code block. If null, it will use $js as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former. Default to null
- position (optional) The position at which the JS script tag should be inserted in a page. Default to \yii\web\View::POS_READY