smoya/asset-management-bundle
最新稳定版本:1.0.0
Composer 安装命令:
composer require smoya/asset-management-bundle
包简介
Symfony2 bundle that provides an easy way for manage assetic packages inclusion in Twig Templates.
README 文档
README
An Asset Management Bundle for Symfony2
##TODO:
- Improve README Doc
- More functions
- Tests
- etc
This bundle provides an easy way for manage assetic packages inclusion in Twig Templates. Of course, We need to have previously installed the Assetic library .
This bundle allows you to print the code that includes assets (javascript and css) there in the place where desired. For example, after loading javascript jquery libraries already loaded at the end of the html code.
In example, this is posible:
Template 1 (Not extends from a base template):
{{ assets_add('assetic/foo.js', 'js') }} Base Template:
{{ assets_render('js') }} Getting as a result:
<script src="/assetic/foo.js" />
Installation
Add this entry to the deps file
[SmoyaAssetManagementBundle] git=https://github.com/smoya/AssetManagementBundle.git target=/bundles/Smoya/Bundle/AssetManagementBundle Register the bundle into your application Kernel
// app/AppKernel.php public function registerBundles() { return array( //.. new Smoya\AssetManagementBundle\SmoyaAssetManagementBundle(), //.. ); } In 2.1*
Smoya\Bundle\AssetManagementBundle\SmoyaAssetManagementBundle() Now update vendors:
$ ./bin/vendors
Now, we need to add this entry to the autoloader:
<?php // app/autoload.php $loader->registerNamespaces(array( // ... 'Smoya' => __DIR__.'/../vendor/bundles', ));
Use
First of all, we need to set the packages need to include. The guys of Sonata have a post with an example here.
Imagine the following case:
###We have 3 Templates
- ::base.html.twig
<!DOCTYPE html> <html> <head> <title>Test</title> {% stylesheets filter='cssrewrite' 'css/compiled/main.css' %} <link href="{{ asset_url }}" media="all" type="text/css" rel="stylesheet" /> {% endstylesheets %} </head> <body> {% block content %} This page exists for test SmoyaAssetManagementBundle {% endblock %} {% block javascripts %} <script src="{{ asset('assetic/main.js') }}"></script> {% endblock %} {% block extra %} {% endblock %} </body> </html>
- index.html.twig
{% extends '::base.html.twig' %} {% block content %} This page extends from '::base.html.twig' template and i can include code. {# We need to render a widget #} {% render AcmeTestBundle:Test:widget %} {% endblock %} {% block extra %} <script src="{{ asset('assetic/bar.js' }}" /> {% endblock %}
- widget.html.twig
{% block widget %} I am a widget and I need render Javascript at the bottom of the website code {% endblock %} {# This block extra is not the ::base.html.twig 'extra' block #} {# Because im not extending the ::base.html.twig template #} {% block extra %} <script src="{{ asset('assetic/another.js' }}" /> <script> alert('Widget loaded'); </script> {% endblock %}
###The problem and a solution: When using Twig render, and if the rendered template contains javascript, it will print where the've called. This is a problem if you are rendering before loading javascripts, especially if the code requires other libraries (eg jQuery).
For this we use the features of Twig adding this bundle as follows:
- ::base.html.twig
<!DOCTYPE html> <html> <head> <title>Test</title> {% stylesheets filter='cssrewrite' 'css/compiled/main.css' %} <link href="{{ asset_url }}" media="all" type="text/css" rel="stylesheet" /> {% endstylesheets %} </head> <body> {% block content %} This page exists for test SmoyaAssetManagementBundle {% endblock %} {% block javascripts %} <script src="{{ asset('assetic/main.js') }}"></script> {% endblock %} {# render managed assets #} {{ render_assets() }} {% block extra %} {% endblock %} </body> </html>
- index.html.twig
{% extends '::base.html.twig' %} {% block content %} This page extends from '::base.html.twig' template and i can include code. {# We need to render a widget #} {% render AcmeTestBundle:Test:widget %} {% endblock %} {# This one can do because we inherited from the template base, which contains this block below including javascript #} {% block extra %} <script src="{{ asset('assetic/bar.js' }}" /> {% endblock %}
- widget.html.twig
{% block widget %} I am a widget and I need render Javascript at the bottom of the website code {% endblock %} {# add_assets adds the inclusion html code for the passed assets in the place where the render_assets() function is called #} {{ add_assets('assetic/bar.js', 'js') }} {{ add_assets('alert("Widget Loaded");', 'inline_js') }}
##Options and parameters Add Assets:
{{ add_assets([$ASSETS], $FORMAT, {$ATTR}) }} The parameters:
- 1: ASSETS Array/Scalar An array of assets or a single asset. example: ['assetic/foo.js', 'assetic/bar.js']
- 2: FORMAT String The format of the assets (js, css, inline_js or inline_css)
- 3: ATTR Array Optional Associative array of attributes for the inclusion html tag. example for a css asset: {'media': 'screen'}
Print assets:
{{ render_assets($FORMAT) }} The parameters:
- 1: ASSETS String Optional The format of the assets to print.
smoya/asset-management-bundle 适用场景与选型建议
smoya/asset-management-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 111.69k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「javascript」 「css」 「asset」 「manage」 「assetic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 smoya/asset-management-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 smoya/asset-management-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 smoya/asset-management-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
统计信息
- 总下载量: 111.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04