Inspired by
this article .
I would like to show how I work with resources and why I find such an approach devilishly convenient.
It just so happened that as a template engine, I liked Twig.
It’s just like I’m poured in - first on CodeIgniter, then I became friends with Yii through a slightly broken extension (reference to which is at the end of the article)
')
One of its thousands of cool features is template inheritance, like the one below:
layout.twig<html> <head><title>{% block title %}{% endblock %}</title></head> <body> <h1>, !</h1> {% block content %}{% endblock %} </body> </html>
index.twig {% extends 'layout.twig' %} {% block title %} {% endblock %} {% block content %} ?{%endblock%}
As a result, when we render our index.twig, we put the title and make the greeting more informal.
Now it would be nice to screw the scripts and the rest.
I do this as follows - I have
css ,
img ,
js folders inside the assets folder, which in turn is in the theme folder (if you publish css and img separately, the relative links in css will break). In the main template, I do this:
{% set assetsDir = Yii.app.publishFile('assets') %} {% call Yii.app.clientScript.registerScriptFile( assetsDir~'/js/chosen.jquery.min.js' ) %} {% call Yii.app.clientScript.registerCssFile( assetsDir~'/css/screen.css', 'screen') %}
publishFile was added as a wrapper over assetManager, to support relative paths based on the selected theme. Those. The Yii.app.publishFile ('assets') construction will publish the assets folder from the current theme directory - it's convenient for me.
At some point, having decided to connect an additional script to a specific representation, I only do in its twig file
{% call Yii.app.clientScript.registerScriptFile( assetsDir~'/js/myscript.js' ) %}
As a result, I get a remarkably readable (to me, in any case) pattern — I immediately see where it all connected.
layout.twig {% set assetsDir = Yii.app.publishFile('assets') %} {% call Yii.app.clientScript.registerScriptFile( assetsDir~'/js/chosen.jquery.min.js' ) %} {% call Yii.app.clientScript.registerCssFile( assetsDir~'/css/screen.css', 'screen') %} <html> <head><title>{% block title %}{% endblock %}</title></head> <body> <h1>, !</h1> {% block content %}{% endblock %} </body> </html>
index.twig {% extends 'layout.twig %} {% block title %} {% call Yii.app.clientScript.registerScriptFile( assetsDir~'/js/myscript.js' ) %} {% endblock %} {% block content %} ?{%endblock%}
In the controller, we pull for $ this-> render ('index') and voila!
For me it is very important in this approach that I control the connection of resources directly in the views.
Nuance - in templates that extend another template, variables defined by the parent template will be available only in {% block%} {% endblock%} constructions
Link to the used twig-extension for Yii:
https://github.com/yiiext/twig-rendererTrue, he, as I wrote above, is broken down - I didn’t want to be friends with the topics out of the box, plus calling procedures that do not return values, it was easier for me
{% call procedurename() %}
than hard to read
{{ void( procedurename() ) }}, twig.