I have searched high and low and could not find any example on how to write custom formula loader for assetic. Basically, I want to add a twig function (if possible, or do I have to use tag?) named add_asset. I need to somehow let assetic know that it must parse this function inside twig templates to gather assets to dump.
I have a hint that it has something to do with the AsseticTokenParser and TwigFormulaLoader but so far I have not figured that out yet. I have spent the whole weekend looking at this code and still have not figured out anything useful so far. Any direction is much appreciated.
This is a part of the quest to delay loading and processing assets till the very end of the response processing on Symfony 2.
I also posted a question on Dynamically adding assets via php which is also related to this question (but that one is for rendering assets, this one is for dumping assets)
Related
I use the jsqueeze assets filter for my project and digged in the code.
For me, it seems like the squeeze() function is beign called on the js files given.
Now i want to set the renaming variables argument on the squeeze function, but it is a third-party-bundle and i haven't found a solution to configure it in the config.yml where the filter is defined.
I found out where the magic happens.
All possible configurations are stored in the symfony assetics-bundle:
vendor>symfony>assetics-bundle>Resources>config>filters
For each filter, there is an xml file with the parameters you are able to change.
It took me a lot of time to find this, i think it is not documented in the symfony documentation.
As my Meteor project grows and I add more templates, partials and helpers, it gets harder to figure out what will be the data context for it. Then I'll have to console.log(this) inside a helper function to figure out what's the data I'm dealing with.
Does anybody have a naming scheme or any other strategy to handle this?
Or is this mess just a sign I'm failing to modularize stuff properly and should refactor everything?
For me, each module has a folder. Each folder contains helpers.js, events.js, the "ons" (onCreated.js, onDestroyed.js, onRendered.js) and finally templates.html. If your project is big, break these out into individual subfolders for the necessary CRUD actions (I have a create folder and an update folder because reading & deleting happens in the update templates.
My template names are long and verbose, but that's OK, WebStorm does a good job of guessing what I want. For example, if I had some infowindow that listed all the addresses associated with a client: clientMap, clientMapPopup, clientMapPopupLocationList clientMapPopupLocationListItem.
Regarding data context, it's usually pretty easy to see since my helper is the one that added something to the context. Although I honestly try to avoid using that unless I'm in an {{#each}} because IMHO things like grabbing grandparent context is neither elegant nor robust. Instead, I have a temporary module object that I create & destroy on route changes.
So if my global object is Global = {}. In the onCreated and onDestroyed I write Global.Module = {} Then, I can create all the module-scoped variables I want (ReactiveVars, ReactiveDicts, local collections, primitives, client markers object, etc).
All that said, doing what you do & looking at my schema js (e.g. collections/clients.js) is still the fastest/thoughtless way to see what you want & what you're currently getting.
Is it possible to add or remove scripts from a bundle based on the debug setting of the application?
I.e. Given a bundle definition of-
var jQueryBundle = new ScriptBundle("~/js/main").Include(
"~/Scripts/jquery-1.{version}.js",
"~/Scripts/jquery-migrate-{version}.js",
"~/Scripts/script.js");
Remove the jquery-migrate reference when the site is not running in a debug profile, or conversely add it under debug.
Nb. I actually came up with a solution to this whilst formulating the question, I have tested it and it seems to work. I have answered it but left it unaccepted for now should a better solution present. In the mean time the answer will at least now be available for reference should anyone want to make use of it.
I actually came up with an answer to this whilst writing out the question. Since I hadn't been able to find an answer through searching I decided to post the question anyway and answer it - both so it is available as reference for anyone else wanting to do the same thing, and to get feedback from wiser minds than mine if this the most performant way of achieving this - or even a sensible thing to do.
var jQueryBundle = new ScriptBundle("~/js/main").Include(
"~/Scripts/jquery-1.{version}.js",
"~/Scripts/script.js");
if (HttpContext.Current.IsDebuggingEnabled)
{
jQueryBundle.Include("~/Scripts/jquery-migrate-{version}.js");
}
bundles.Add(jQueryBundle);
I have tested this and whilst debugging the script is listed (I have retained the defaults of bundling and minification left off in debug mode) and whilst running normally the contents of the migration script were not in the combined minified script. Over the next couple of days I will profile this to compare to "vanilla" bundling to see what (if any) performance impact this has on application start.
I'm not sure how to put that question. It's a bit fuzzy, but if you encountered the Corpus Delicti, you'll know what I mean.
When I first came across how Symfony2 maps bundles onto paths, few days ago, I already felt bit puzzled about why they chose to map a Bundle name of
AxxBxxBundle onto .../Axx/BxxBundle/...
To me this decision seems arbitrary and confusing, don't get me wrong this is not a serious obstacle and you get used to it within five minutes, but still I think it smells funny.
Now I just came across this quote in the Book:
BundleName:ControllerName:TemplateName
This is the logical name of the template, which is mapped to a
physical location using the following convention.
/path/to/BundleName/Resources/views/ControllerName/TemplateName
And this is wrong I think, there would have to be slash between Bundle and Name.
But this quote shows me that even in the inner Symfony2 circle some people don't find that intuitive.
So what is the "philosophy" bind this bundle thing and its mapping logic?
This doesn't exactly answer the question about the reasoning, but it provides some more information on the Bundle-topic. To get an answer on the why, you should perhaps contact Fabien Potencier who wrote the Symfony\Component\HttpKernel\Kernel class which relies on this convention.
Coming from Zend Framework, it's not that bad for me. All those classes start with Zend_ Thinking of it as the owner/creater's name makes it easier. In java most libraries start with com.foobar
Quoting Fabien: "Symfony2 relies on well-know design patterns, and best practices coming from the Java world."
Your statement does not really make sense or I'm missing your point:
And this is wrong I think, there would have to be slash between Bundle
and Name.
Did you mean: Between Acme and DemoBundle? It does feel like a duplication, but
Quoting Fabien: "A bundle is a structured set of files that implements a single feature and can be easily shared with other developers."
I can use a blog bundle from Acme, then I find a similar bundle from Emca. Most likely their bundles will have the same model&controller&route names. Using them both: this way nothing gets overridden using another library. So the first name for the creator/owner is to prevent duplication.
Since it's a pain to use, I always call mine My/DemoBundle for quicker use :p Or you can give it a custom name during the console bundle generation (if you don't like it)
Also:
All core bundles are treated the exact same as your bundles and
everything is a bundle. Bundles are easily portable and configurable.
They are really the key to Symfony2’s real power. A bundle can extend
another bundle. It can be distributed independently from it’s
application.
Since the web assets are in the bundle, I believe they managed to get it right. That will allow easy configuration and reusability, e.g. bigger (scope) improvements can be made to the symfony core without effecting your app.
Is it good to make Velocity Template Object Static?
The situation is im going to use this object in (Servlet) multi user environment populating different data for each user-request with same template.
You can make it static, only it would mean that you have to redeploy your app every time you make changes to your templates. If you are worrying about performance (and reading template from file every time is indeed slow) better solution would be to enable template caching which helps a lot, that way you still can make changes to templates on the fly without sacrificing performance.
file.resource.loader.cache = true/false
file.resource.loader.modificationCheckInterval = <seconds between checks>