2017 © Pedro Peláez
 

yii2-extension yii2-js-register

Yii2 widget to register JS into view

image

richardfan1126/yii2-js-register

Yii2 widget to register JS into view

  • Thursday, July 7, 2016
  • by richardfan1126
  • Repository
  • 2 Watchers
  • 7 Stars
  • 4,392 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 21 % Grown

The README.md

yii2-js-register

Latest Stable Version Total Downloads GitHub stars GitHub issues, (*1)

Yii2 widget to register JS into view, (*2)

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(),, (*3)

For example, if you want to initialize a jQuery plugin, you do:, (*4)

// in view file







registerJS('$(".modal").modal({backdrop:"static",keyboard:"false"})');
?>

Obviously, this is a very bad way because:, (*6)

  1. Hard to read for human
  2. Hard to read for you editor (Means no syntax coloring for the JS code)
  3. 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!, (*7)

Getting Started

Installing

Install with Composer:, (*8)

composer require richardfan1126/yii2-js-register "*"

or, (*9)

php composer.phar require richardfan1126/yii2-js-register "*"

or add, (*10)

"richardfan1126/yii2-js-register":"*"

to the require section of your composer.json file., (*11)

How to use

In your view file, wrap your JS script with <script> tag, and wrap all the things with this widget., (*12)

example:, (*13)

// in view file

use richardfan\widget\JSRegister;














IMPORTANT Remember not to add anything out of the <script> block., (*15)

Configurations

There are 2 configurations for this widget, which are the parameters passed into \yii\web\View::registerJS() method. example:, (*16)

<?php JSRegister::begin([
    'key' => 'bootstrap-modal',
    'position' => \yii\web\View::POS_READY
]); ?>
<script>
    // JS script
</script>
<?php JSRegister::end(); ?>
  • 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

The Versions

07/07 2016

dev-master

9999999-dev

Yii2 widget to register JS into view

  Sources   Download

MIT

The Requires

 

extension yii2 javascript widget

29/06 2016

v0.0.1

0.0.1.0

Yii2 widget to register JS into view

  Sources   Download

MIT

The Requires

 

extension yii2 javascript widget