I have this:
ShopBundle
Controller
Resources
public
images
marker.png
views
Default
index.html.twig
In my index.html.twig, I'd like to have this
<img src="{{ asset("images/marker.png") }}"/>
And I'd love people using my bundle who just want to use their own marker.png to just have to build a bundle inheriting mine and place their image just by following files structure:
MyShopBundle
Resources
public
images
marker.png
Is there any simple way to do this in Symfony2 ? The need seems so simple that I can't believe I didn't find answers already.
So,
How do you include an image asset in your bundle template from your bundle resources directory ? I already did a ./apps/hfr/console assets:install web but my template does not print the right url (/images/marker.png instead of /bundles/ShopBundle/Resources/public/images/png)
Is it possible to override the way I want or did I lost my way ?
Solution:
use the # syntax ...
{% image '#VendorMyShopBundleBundle/Resources/public/images/example.jpg'
output='/images/example.jpg' %}
<img src="{{ asset_url }}" alt="Example"/>
{% endimage %}
Please note that Vendor/YourBundle/Resources/public will NOT be accessible by your web server normally.
The assets:install command will therefore copy the assets to web/bundles/vendoryourbundle
The {{ asset('path/to/asset.jpg') }} function will adjust the url of your asset if youre using the dev environment:
http://hostname/app_dev.php
from
/path/to/asset.jpg
to
/app_dev.php/to/asset.jpg
[EDIT]
if you want more control over the assets maybe consider using asset collections.
You can configure them as follows:
# app/Resources/config/config.yml
assetic:
[...]
assets:
css_bootstrap:
inputs:
- %kernel.root_dir%/../src/Vendor/YourBundle/Resources/public/twitter-bootstrap/less/bootstrap.less
- [...]
filters:
- lessphp
- [...]
output: css/bootstrap.css
my_image:
inputs:
- %kernel.root_dir%/../path/to/image.png
filters:
- optipng
output: images/image-with-new-name.png
and use them afterwards in your template like this:
{% stylesheets '#css_bootstrap' %}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}">
{% endstylesheets %}
I am NOT sure right now if the assetic/assets/packagename/inputs configuration array supports the #VendorYourBundle syntax aswell and then uses bundle inheritance though.
Addition:
Before you can use these packages you will have to use the console command:
php app/console assetic:dump
Related
From a personal symfony 3 project, I want use the font-awesome library. In the config.yml file I added an asset like that :
awesomefont_css:
inputs:
- '%kernel.root_dir%/../web/library/font-awesome/css/font-awesome.css'
I call the asset from my index.php page :
{% stylesheets output="stylesheet.css"
'#bootstrap_css'
'#awesomefont_css'
"#style_css"
%}
<link rel="stylesheet" href="{{ asset_url }}"/>
{% endstylesheets %}
I cleared caches (via php bin/console cache:clear) but when I load my php page, I get the errors :
I understood the library font-awesome need use some another files. My question is : how can/must I add properly theses anothers files in a symfony 3 ?
Thanks !
I add samples symbolic links from mywebsite/web/library/font-awesome/fonts/* to mywebsite/web/fonts . I don't know if there is a better solution but I don't find another and that work
I included fortawesome/font-awesome via composer.
Then, you have to add additional entries to your config.yml:
fontawesome_css:
inputs:
- '%kernel.root_dir%/../vendor/fortawesome/font-awesome/css/font-awesome.css'
filters: [cssrewrite]
font-awesome-otf:
inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/FontAwesome.otf'
output: 'fonts/FontAwesome.otf'
font-awesome-eot:
inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/fontawesome-webfont.eot'
output: 'fonts/fontawesome-webfont.eot'
font-awesome-svg:
inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/fontawesome-webfont.svg'
output: 'fonts/fontawesome-webfont.svg'
font-awesome-ttf:
inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/fontawesome-webfont.ttf'
output: 'fonts/fontawesome-webfont.ttf'
font-awesome-woff:
inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/fontawesome-webfont.woff'
output: 'fonts/fontawesome-webfont.woff'
font-awesome-woff2:
inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/fontawesome-webfont.woff2'
output: 'fonts/fontawesome-webfont.woff2'
then only include the fontawesome_css (as you did already):
{% block stylesheets %}
{% stylesheets '#bootstrap_css' '#fontawesome_css' '#custom_css' %}
<link rel="stylesheet" type="text/css" media="screen" href="{{ asset_url }}"/>
{% endstylesheets %}
{% endblock %}
Finally, run
bin/console assetic:dump
and you're done! It is not necessary to mention the font files in your template, they are copied automatically.
I should add that the webpack/encore package is new and much better in my opinion! It replaces assetic.
You can also easily customize your bootstrap theme on the fly with this tool.
Im trying to include tinymce 4.5.5 to my Symfony project using Assetic
{% block javascripts %}
{% javascripts '#MyAmazingBundle/Resources/public/js/jquery.min.js'
...
'#MyAmazingBundle/Resources/public/js/tinymce.min.js' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
I downloaded TinyMCE Community and copied file tinymce.min.js to my Resources. I have got error:
TypeError: u is not a constructor
I researched that u is Theme so i tested some things and changed include by adding whole tinemce directory to resources + new declaration:
#MyAmazingBundle/Resources/public/js/tinymce/tinymce.min.js
I think Assetic breaking it because I tryied to include tinymce file from cloud and it worked but when i downloaded code from cloud and icluded from resources it was again broken in other way (not visible etc - something not worked)
I also tested some other ways to include it but nothing worked...
What is the correct way to add tinymce to symfony project/bundle and include to template?
// EDIT
I tryied include it by asset function and it worked so probably assetic breaking it by making copy of this file but anyway how to fix it?
<script src="{{ asset('bundles/myamazing/js/tinymce/tinymce.min.js') }}"></script>
I'm starting at creating my own bundles and upload them to github.
I have just created this: https://github.com/Ziiweb/FrontendBundle
Inside the bundle, there is an image here: https://github.com/Ziiweb/FrontendBundle/tree/master/Resources/public/images
And I'm loading that image this way below inside a template in the same bundle, in the same way as it said in the docs:
{% image '#ZiiwebFrontendBundle/Resources/public/images/ajax-loader.gif' %}
<img class="loader" src="{{ asset_url }}" alt="Example" />
{% endimage %}
The problem: after installing the bundle (composer) in another project, I get the error below when trying to request the template where the image is:
An exception has been thrown during the rendering of a template
("Unable to generate a URL for the named route "_assetic_0b1c853_0" as
such route does not exist.") in
ZiiwebFrontendBundle:Default:contact.html.twig at line 15.
I get the same message using this code:
{% image 'bundles/ziiwebfrontend/images/ajax-loader.gif' %}
<img class="loader" src="{{ asset_url }}" alt="Example" />
{% endimage %}
Yes, I have installed the assets using assets:install so my image is at web/bundles/ziiwebfrontend/images/ajax-loader.gif.
NOTE: I think I have Assetic installed correctly.. I have used these 3 steps.
I think you missed to add same bundle in config.yml
assetic:
debug: %kernel.debug%
use_controller: false
bundles: [YourBundleName]
I have a question about symfony2 and bootstrap. I dont understand why I can load icons image from environment "prod" but not from environment "dev". In dev, i get this error.
The route is "GET /img/glyphicons-halflings.png".
Image web/img/glyphicons-halflings.png is a symbolic link to ../../vendor/twitter/bootstrap/img/glyphicons-halflings.png
I get this error with
http://my.web.site/app_dev.php/img/glyphicons-halflings.png
And get image with
http://my.web.site/img/glyphicons-halflings.png
UPDATE
I include bootstrap in this way:
{% stylesheets '%kernel.root_dir%/../vendor/twitter/bootstrap/less/bootstrap.less' %}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}
When I use in prod this works
<span class="icon-edit"></span>
And I have this assetic configuration:
assetic:
debug: %kernel.debug%
use_controller: false
bundles: [ MyAwesomeBundle ]
filters:
lessphp:
file: %kernel.root_dir%/../vendor/leafo/lessphp/lessc.inc.php
apply_to: "\.less$"
I've also tried to create my collection:
assetic:
assets:
senso:
inputs:
- '../img/glyphicons-halflings.png'
outputs:
- 'img/glyphicons-halflings.png'
filters:
- ?lessphp
:dump create web/assetic/senso.png and web/assetic/senso_glyphicons-halflings.png but, ... how can works with senso*.png image?
config.yml
assetic:
use_controller: false
filters:
lessphp:
file: "%kernel.root_dir%/../vendor/leafo/lessphp/lessc.inc.php"
assets:
img_bootstrap_glyphicons_black:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/img/glyphicons-halflings.png"
output: "img/glyphicons-halflings.png"
img_bootstrap_glyphicons_white:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/img/glyphicons-halflings-white.png"
output: "img/glyphicons-halflings-white.png"
css_bootstrap:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/less/bootstrap.less"
output: "css/bootstrap.css"
filters:
- lessphp
js_bootstrap:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/js/*.js"
output: "js/bootstrap.js"
config_dev.yml
assetic:
use_controller: true
template
{% stylesheets '#css_bootstrap' %}
<link href="{{ asset_url }}" rel="stylesheet" type="text/css"/>
{% endstylesheets %}
{% javascripts '#js_bootstrap' %}
<script src="{{ asset_url }}" type="text/javascript"></script>
{% endjavascripts %}
after running assetic:dump --no-debug everything works flawlessly.
general info
the cssrewrite filter used i.e. for background-image: url("..") is incompatible with the #Bundle-syntax.
{% stylesheets
'#AcmeYourBundle/Resources/public/css/your.css'
filter="cssrewrite"
%}
doesn't work.
Furthermore, sources like url("../img/image.png") will fail in dev environment without cssrewrite and assetic.use_controller: true - possible workaround: use urls like url("/img/image.png") but not working with pages in subfolders.
you should keep in mind that ...
<img src="img/image.png">
... will for hostname/app_dev.php/ and all other routes except hostname/.
solution:
<img src="{{ asset('img/image.png') }}">
other solutions & tips
when including img,css or js into your page always use twig's asset(), {% stylesheets %} or {% javascripts %} function.
use relative urls in stylesheets i.e. url("../img/image.png")
try setting assetic.use_controller: false in your config_dev.yml and dump your assets ... as described here
create asset collections in config.yml, then dump your assets - see my answer here.
use the cssembedded filter to include your images in css using data-uris - reference here
configure your webserver to directly serve files inside img/css/js folders instead of routing through symfony
use a different hostname (i.e. project.dev ) for the dev environment using app_dev.php as directory-index resulting in url's without /app_dev.php/
you can find some good information about this topic in the answers to this question.
my personal workflow:
local wildcard dns server introducing automatically generated urls like project.dev , project.prod , ... ( no /app_dev.php/ problems )
creating asset collections in config.yml ( cleaner templates, more control over assets )
guard / grunt taskrunners instead of assetic:dump --watch ( re-dump on change but test and optimize aswell )
source-maps and chrome-devtools instead of assetic's assetic:dump --debug ( see the real source files for combined files,less,sass,coffeescript, ... )
tincr ( to save directly from devtools ) and livereload ( see changes instantly )
Is any posibilities to include service file with assetic filter definition as YML file in config.yml? Assetic expects XML structure...
bundle/Resources/config/services.yml:
assetic.filter.csslint:
class: IOKI\CommonBundle\Assetic\Filter\CssLintFilter
tags:
- { name: assetic.filter, alias: csslint}
config.yml
assetic:
filters:
csslint:
resource: %kernel.root_dir%/../src/Project/CommonBundle/Resources/config/services.yml
The discussion in a related issue against Assetic implies that you can point your resource key at an arbitrary XML file (the examples there use 'assetic.xml' and then it will be okay specifying your filter in services.yml as normal.
(I found that this only worked if I cleared my Symfony cache and did a "hard-refresh" in my browser - it wasn't until I tried that that this actually worked for me.)
Furthermore, if you only want to reference your filter directly from a template rather than in config.yml, you don't need to mention it in config.yml at all. You can just specify your filter in services.yml as you've done, and reference it in Twig. E.g:
{% stylesheets
'bundles/boardworksapp/less/custom.less' filter='csslint'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}