In this Yii2 programming series, I will guide readers to use the newly upgraded Yii2PHP framework. In this tutorial, I'll show you how to add custom JavaScript and CSS scripts and libraries to your Yii application. Yii uses a concept called AssetBundles to manage these resources more easily. For these examples, we'll continue to build on the simple state application from the previous tutorial. As a reminder, I do participate in the comment thread below. If you have no">
If you're asking "What is Yii?" check out my previous tutorial:Introduction to the Yii Framework, which reviews the benefits of Yii and provides an overview of the 2014-10 New features of Yii 2.0 released in March.Well>
In this Yii2 programming series, I will guide readers in using the newly upgraded Yii2 PHP framework. In this tutorial, I'll show you how to add custom JavaScript and CSS scripts and libraries to your Yii application. Yii uses a concept called Asset Bundles to make it easier to manage these resources.
For these examples we will continue to build on the simple state application from the previous tutorial.
As a reminder, I do participate in the comment thread below. I'd be particularly interested if you have a different approach, additional ideas, or would like to suggest topics for future tutorials.
Yii's resource bundles represent groups of JavaScript and CSS files that need to be included together on a specific page or an entire website. Resource bundles make it easy to group together specific scripts and styles for specific areas of your site. For example, in my Meeting Planner application, I can easily include the Google Places API on only the pages I need.
This is a simple example. We create a\frontend\assets\LocateAsset.php
file:
Copy after login
Then we load it into our view file - it's really simple:
Copy after login
When you view the source code of our pages, you will see the generated scripts as well as other Yii2 standard resources for forms, Bootstrap, etc.:
In this tutorial, I'll guide you through using a resource bundle to integrate character counting into our status form. We'll use this to enforce a character limit, similar to Twitter's 140 character maximum.
If you are interested in seeing this feature in Yii1.x, I implemented it in Building with the Twitter API: OAuth, Read and Post (Tuts).
In the\assets
directory, we createStatusAsset.php
:
* @since 2.0 */ class StatusAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; public $css = []; public $js = [ '/js/jquery.simplyCountable.js', '/js/twitter-text.js', '/js/twitter_count.js', '/js/status-counter.js', ]; public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', ]; }
I used a combination of the jQuery simpleCountable plugin, twitter-text.js (Twitter's text processing script based on JavaScript), and the script responsible for URL shaping: twitter_count.js; in Twitter, URLs are counted as 20 characters. These files are in\web\js
.
I also created a document ready function to call them in\web\js\status-counter.js
. Includingyii\web\YiiAsset
in our$depends
array will ensure that JQuery is loaded whenever we instantiate this resource.
$(document).ready(function() { $('#status-message').simplyCountable({ counter: '#counter2', maxCount: 140, countDirection: 'down' }); });
Instantiating a resource package is very simple, as shown in the following\views\status\_form.
:
= $form->field($model, 'message')->textarea(['rows' => 6]) ?>Remaining: 0
Copy after loginThat’s all you need to activate our Twitter-style character counter:
I find Yii Asset Bundles simple and easy to manage. They help me reuse parts of JavaScript and CSS in certain areas of the application in an organized way.
What’s next?
The Yii2 Definitive Guide describes many of the advanced features of Asset Bundles. You can control where the scripts are loaded for each package, e.g.
POS_HEAD
,POS_END
. You can set up asset mappings to load specific compatible versions of libraries. You can set JavaScript and CSS options to further conditionally load bundles. You can also use resource converters to compile LESS code to CSS or TypeScript to JavaScript.Check out the upcoming tutorials in my "Programming with Yii2" series as we continue to dive into different aspects of the framework. You may also want to check out my "Building Your Startup with PHP" series, which uses advanced templates for Yii2 as I build real-world applications.
I welcome feature and theme requests. You can post them in the comments below or send me an email on my Lookahead Consulting website.
If you would like to know when the next Yii2 tutorial is released, follow me on Twitter @reifman or check out my instructor page. My instructor page will immediately contain all articles from this series.
Related Links
The above is the detailed content of Mastering Yii2: Harnessing the Power of Resource Bundles in Programming. For more information, please follow other related articles on the PHP Chinese website!