Symfony2 include js files via loop from assetic.yml - symfony

assets.yml:
assetic:
assets:
base_js:
inputs:
- '%kernel.root_dir%/Resources/public/jquery-1.10.2.min.js'
- '%kernel.root_dir%/Resources/public/bootstrap3/js/bootstrap.min.js'
output: 'js/scripts.js'
base_css:
inputs:
- '%kernel.root_dir%/Resources/public/bootstrap3/css/bootstrap.min.css'
output: 'js/styles.css'
config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: assets.yml }
#...
assetic:
debug: %kernel.debug%
use_controller: false
bundles: [MyAdminBundle]
#java: /usr/bin/java
filters:
cssrewrite: ~
And in template:
{% block javascripts %}
{% javascripts '#base_js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
However as output I get js/80e3acc_part_1.js and this file only includes the jquery code, boostrap is missing. Any ideas how to fix this?

sadly the output filename configuration of an asset collection is currently not being used by default if none is specified within the {% image %},{% stylesheets %} or {% javascripts %} tag if only one #-resource is present - this explains the filename js/80e3acc_part_1.js.
Further the asset() function does not work with the # - syntax.
use the following to fix the filename issue.
{% javascripts '#base_js' output='js/scripts.js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
or just include the output filename directly with the asset() function.
<script src="{{ asset('js/scripts.js') }}"></script>
This syntax is cleaner but leaves the downside of in case having to change the filename in configuration and the template if you want to rename the output file.
Please note that assetic in most cases does not automatically update the resources for cached collections if resources are added. Therefore always clear your cache using app/console cache:clear prior to dumping your assets.
That would explain why bootstrap was missing in the output file after dumping if you added it as second resource but did not clear the cache before using the assetic:dump command.

Related

Symfony 3.4 : Assetic 404 not found

I need to add another js script in a view.
So I created a new configuration in my assetic.yml file :
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles:
- AppBundle
filters:
cssrewrite: ~
uglifyjs2:
bin: '%kernel.root_dir%/Resources/node_modules/uglify-js/bin/uglifyjs'
uglifycss:
bin: '%kernel.root_dir%/Resources/node_modules/uglifycss/uglifycss'
less:
node_paths: ['%kernel.root_dir%/Resources/node_modules/', '%kernel.root_dir%/Resources/node_modules/less/node_modules/']
apply_to: '\.less$'
assets:
app_gestion_poste_js:
inputs:
- '#AppBundle/Resources/public/js/GestionPoste/gestion.js'
filters: [?uglifyjs2]
output: js/poste/gestion-poste.js
And I added my script in my twig file :
{% block foot_script_assetic %}
{{ parent() }}
{% javascripts
'#app_another_script_js'
'#app_gestion_poste_js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
I ran these commands :
php bin/console assets:install
php bin/console assetic:dump
A lot of files are dumped in the web folder. I can find my file in web/js/poste/gestion-poste.js, and also in web/bunde/...
But when I run my page, there are some 404 not found error :
> GET http://myapp/js/d6e0c99_part_1.js?00.02.00 404 (Not Found)
> GET http://myapp/js/d6e0c99_part_2.js?00.02.00 404 (Not Found)
> GET http://myapp/js/d6e0c99_part_1.js?00.02.00 404 (Not Found)
> GET http://myapp/js/d6e0c99_part_2.js?00.02.00 404 (Not Found)
Both files are not found.
Why the files are loaded twice ?
If I remove my new script from the assetic instruction, the first script works and is found :
{% block foot_script_assetic %}
{{ parent() }}
{% javascripts
'#app_another_script_js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
If I add the script into another view, I have the same issue. All the scripts of the page are not found.
I tried to :
change the rights of my JS file
change the js file content by a simple script like : console.log('hello');
copy/paste another js file which works and use it in the assetic.yml configuration
There is the same issue in the prod environment :(
Thanks !

In Symfony2 using the AsseticBundle tool, based on the .Less file to create all the .Css files to web directory, the program into an infinite loop?

I can't find the reason. Please help me.
I use this command generate .css file to web directory:
$sudo php app/console assetic:dump
Error information:
[Symfony\Component\Debug\Exception\OutOfMemoryException]
Error: Allowed memory size of 5368709120 bytes exhausted (tried to allocate 4205621120 bytes)
$ls /opt/webroot/symfony/web/css
//views/base.html.twig
{% block head_style %}
{% stylesheets filter='less,cssrewrite'
'#ScourgenPersonFinderBundle/Resources/public/less/style.less'
'#BloggerBlogBundle/Resources/css/style.css'
%}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="screen"/>
{% endstylesheets %}
{% endblock head_style %}
#app/config/config.yml
assetic:
debug: '%kernel.debug%'
# use_controller: '%kernel.debug%'
use_controller: false
bundles:
- ScourgenPersonFinderBundle
- MopaBootstrapBundle
- BloggerBlogBundle
filters:
cssrewrite: ~
# yui_css:
# jar: /usr/local/lib/node_modules/yuicompressor/build/yuicompressor-2.4.8.jarw
less:
node: /usr/local/bin/node
node_paths: [/usr/local/lib/node_modules, /opt/lessc]

Symfony 2: Deploy assets from non bundles

I wonder if there's a possibility to deploy assets from a custom folder.
I've separated my business logic and entities from the bundles and now I would like to have a centralized place to my custom js library.
Is there any configuration available for symfony or assetic to deploy a custom folder that doesn't belong to a bundle? TIA
Well, I found an answer without intention LOL.
Looking a repo on github I saw that Assetic can be configured to receive some input resources and generate a unique output like this:
assetic:
debug: %kernel.debug%
use_controller: true
filters:
cssrewrite: ~
yui_css:
jar: %kernel.root_dir%/../vendor/nervo/yuicompressor/yuicompressor.jar
yui_js:
jar: %kernel.root_dir%/../vendor/nervo/yuicompressor/yuicompressor.jar
less:
node: /usr/bin/node
node_paths: [/usr/local/bin/]
apply_to: "\.less$"
assets:
scripts:
inputs:
- %kernel.root_dir%/../src/MyResources/script.js
- #FoundationViewBundle/Resources/public/js/*
filters:
- cssrewrite
- yui_js
output: js/scripts.min.js
debug: %kernel.debug%
stylesheets:
inputs:
- %kernel.root_dir%/../src/MyResources/stylesheet.css
- #FoundationViewBundle/Resources/public/css/*
filters:
- less
- yui_css
output: css/style.min.css
debug: %kernel.debug%
Which can be used directly with:
<link href="{{ asset('css/style.min.css') }}" rel="stylesheet">
<script src="{{ asset('js/scripts.min.js') }}"></script>
or using Assetic aliases:
{% stylesheets '#stylesheets' %}
<link rel="stylesheet" type="text/css" media="screen" href="{{ asset_url }}">
{% endstylesheets %}
{% javascripts '#scripts' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

How can I use a Twig variable in Assetics 'javascript' tag?

I'm using Assetic to include my scripts and need to pass the current locale. How would I do that?
{% javascripts
'#MyBundle/Resources/public/components/moment/moment.js'
'#MyBundle/Resources/public/components/moment/lang/' ~ app.request.locale ~ '.js'
%}
String concatenation does not work and throws Unexpected token "operator" of value "~"
You can use assetic's variables as described in Ryan Weaver's answer here. The feature is kind of broken but will work in your case as you use the locale as variable.
The answer is that only two variables work by default (locale and env,
and their values are preconfigued in Symfony:
https://github.com/symfony/AsseticBundle/blob/master/DefaultValueSupplier.php#L31.
config_dev.yml
assetic:
use_controller: false
config.yml
You'll also need to set your assetic.variables.locale [...] to the
total possible combinations of your variable:
assetic:
variables:
locale: [en,fr,de]
... then use them inside the javaScripts tag after invoking assetic:dump.
template
{% javascripts
'bundles/my/components/{locale}.js'
%}
Workaround construction:
{%set myScript = '#MyBundle/Resources/public/components/moment/lang/' ~ app.request.locale ~ '.js' %}
<script src="{{ asset(myScript) }}"></script>

symfony2 yui_compressor output in subdomain

I'm using yui_compressor:
`
{% javascripts filter='?yui_js'
'#MyProjectBundle/Resources/public/js/libs/jquery/jquery-min.js'
'#MyProjectBundle/Resources/public/js/libs/bootstrap/bootstrap-modal.js'
'#MyProjectBundle/Resources/public/js/libs/bootstrap/bootstrap-tooltip-2.0.js'
'#MyProjectBundle/Resources/public/js/libs/bootstrap/bootstrap-popover.js'
'#MyProjectBundle/Resources/public/js/libs/bootstrap/bootstrap-dropdown.js'
'#MyProjectBundle/Resources/public/js/main.js' %}
<script src="{{ asset_url }}"></script>
<script>
window.basePath = "{{ url('index') }}";
</script>
{% endjavascripts %}
`
I'd like to have an output in subdomain, not in /js/xxx.js, probably one folder above than kernel_root (where my subdomain is pointing in).
Any idea about how could I solve this issue?
Thank you so much,
i use for my assets a cdn.
you can easily change it in the config_prog.yml
framework:
templating:
assets_base_urls:
http: [ http://cdn.domain.com ]
but you have to keep in mind also assets like images and so on are loaded from there...
besides it can be useful to bust your cache (e.g. your cdn has a long time expiration). you can do this by adding
framework:
templating: { engines: ['twig'], assets_version: v1 }
to your config.yml.

Resources