Symfony2 - Assetic: Declaring multiple images as assets - symfony

Since we use assetic to manage the site's assets, we need to dump all images regardless if they are declared on a twig template due to most of them are used dynamically.
The way to afford this, I think, is declaring them as assets on assetic configuration, but it is a tedious task.
Does anybody know how to declare multiple image as assets in an elegant way?
Thanks

Related

Organizing assets in Symfony3

I still have some problems to handle my assets in symfony. The best practices say, I should store my assets in web/.
But I dont like to store my raw sass files there, because its a public folder and I think only compiled or static files should be stored there.
Thats why I store them (js and sass) currently in app/Resources. And my assetic.read_from is app/Resources. But then there are some bundles, that are symlinked by assets:install to web/bundles/.
And now, when I want to include this bundle-assets in my twig files, I have to go there by ../../web/bundles/.. in the stylesheets block. That doesnt look very clean, so I did a symlink app/Resources/bundles->web/bundles/ and that works.
But I still think its too much fiddle and I would like to know if there is a cleaner way that better collects my assets in one place.
Don't use AsseticBundle, it was even removed in default symfony-standard 2.8. Managing frontend assets with php is workaround for someone who really don't want to use "the right tools"
I personally keep my source files in /assets/ and with Grunt JS I compile them to /web/assets/ which later is served from assets.somedomain.com through CDN
2 years ago I wrote post about managing assets with symfony, it's still valid and up to date. You might want to check it out.
http://konradpodgorski.com/blog/2014/06/23/better-way-to-work-with-assets-in-symfony-2/
I should extend post by things I learned since then but always not enough time :)
I don't see why you can't use web/ folder for your assets.
I often work with less and other file format which are afterward processed and minified.
The solution to your problem seems simple to me: Use two folder in the web/ folder.
The first folder would be your source/ folder. In which you would place all your sass files. You will add a .htaccess file to this same folder, and deny all access (you can copy from the .htaccess file in the src/ folder).
Then a second folder, lets call it assets which will hold all your compiled and minified assets.
That should do the trick... ;)
You may be interested in this topic as well. It may help to hide futher the existence of your source/ folder. ;)
If you really don't want to have your sources files in the web/ folder, then loot at this, it should help you place your sources files in your bundle.

place for general assets and layout

Bonjour,
i am working on a symfony 2.7 project, I have the general template of the site with its css and js and fonts etc ... i wondered where do i have to put them because it does no belong to a specific bundle. i put the general layout in app/ressources/views and i try to create a public folder in app/ressources/public but the command assets:intall just copy public ressource from existing bundle so i deduct that it should not be the good place for general assets. So i had the idea of create a bundle just for generals templates with its css images etc but i dont know if it is a good idea or if there is a better solution. what do you think about that ?
thank u and sorry for my english.
If you have a common set of assets that you plan to reuse in other projects, then a separate bundle is the way to go.
But if you are only talking about assets specific to this project, then you should put them in your AppBundle (which, as per the "Best Practices" book, should be the only bundle in your project).
Why don't you just create a self-contained Bundle with all your templates/resources and then expand your other bundles on it?
The proper place for your project-specific assets is the web/ folder. There is no need for copying them around.

How to make Assetic scan Twig templates outside bundle

TLDR: How do I make Assetic scan for assets in Twig templates outside bundle?
I've got few registration wizards. Each of these wizards has it's own view directory, the file structure looks like this:
/SiteBundle/Wizard/General/Resources/views
/SiteBundle/Wizard/CountrySpecific/Resources/views
/SiteBundle/Wizard/[...several more...]/Resources/views
In config.yml I defined these paths for twig so I can use #general_wizard/template.html.twig paths:
twig:
paths:
"%kernel.root_dir%/../src/MyWeb/SiteBundle/Wizard/General/Resources/views": general_wizard
"%kernel.root_dir%/../src/MyWeb/SiteBundle/Wizard/CountrySpecific/Resources/views": country_specific_wizard
The problem is that assets used in these templates (inside the Wizard directories) are not dumped using assetic:dump. When I move the view sources to regular SiteBundle/Resources/views, then all the assets are correctly dumped.
Is there a way to make Assetic check the external templates too?
It is not possible with the stock assetic:dump (SF 2.3), as the /Resources/views/ path is hardcoded in the GeneratorBundle, which again provides the list of files to process to assetic. Of course, you could write your own command, but you would basically reinvent the wheel.
I would recommend to keep with the Resources/views convention, and create the subcategories below that:
/SiteBundle/Resources/views/General
/SiteBundle/Resources/views/CountrySpecific
That would have the same effect, and would not require you to write your own commands and mess around with SF2 internals.
I encountered the same problem after moving all templates to templates in the project root, as it will probably come to be in future versions of Symfony.
The simplest solution is to configure all assets in the configuration file and to locate the output files in the web directory, e.g., all bootstrap CSS files into web/css/bootstrap.css. A remaining difficulty is that referencing the assets with the javascripts tag doesn't work anymore because those tags have to be scanned by the Assetic Bundle to work. You have to do this the old-fashioned way, e.g., via {{ asset('css/bootstrap.js') }}.
With Bootstrap I encountered difficulties to make Glyphicons work, since the cssrewrite filter doesn't work as before. I went for Fontawesome at that point, but including your own copy of Glyphicons could also work.

How can one cache bust files referenced in a LESS file when using Symfony2, Twig, and Assetic?

I have a web site built on Symfony2 which uses twig templates, LESS, and assetic.
In order to cache bust assets, I'm simply using this in my config.yml:
framework:
templating:
engines: ['twig']
assets_version: 'asset-version-here'
And then I use the asset() function to load the asset and the cache busting is handled for me.
However, the concern I have is when I load my LESS (css) file, there are references to other files, and I would like to know how these files can be cache busted as well.
Example:
.someSelector { background:url('../images/filename.png'); }
How can I make sure that the referenced file, filename.png is cache busted upon deployment?
The asset files referenced in Twig using asset() are cache busted automatically upon deployment (I use a deployment script hook that updates the assets_version in the framework's config), but those referenced in a stylesheet are not.
How can I do this?
I didn't manage to find a natural solution to this issue. In the end because I needed to move forward, I just added a certain tag in the query string, e.g. {ASSETS_VERSION}, and then I search/replace this with the revision number of the project at deployment time as part of my deployment script.
I'm not proud of this solution but it did solve my problem in the short term until I can find a more elegant solution.

What is the purpose of cached Assetic config?

I'm doing some research into Assetic and asset-management generally in Symfony. One thing I've noticed in the cache folder for my project (which uses Assetic) in the dev environment is the folders assetic\config, with that config folder having subfolders 0-9 and a-f.
Inside each of those subfolders are a number of PHP files with hashes for filenames and contents like this (the comment is part of the file):
// MyBundle:FolderName/twigfilename.html.twig
return array (
);
General questions: What is the point of these files? What do they do? Will they ever have more interesting content than this? (I've never found any that do.)
More specific question: Unless use_controller is set to false (ref), assets are loaded dynamically every time, so - as I understand it - you shouldn't need to clear the cache to force an update of your assets. My general rule-of-thumb is "don't bother clearing the cache for things to do with assets, just do that for Twig or PHP-file things". Given that Assetic obviously puts something in the cache, is this still a good rule-of-thumb?

Resources