Override templateNameParser class with the custom bundle's config file? - symfony

I want to override the templating.name_parser.class but I would like to do some within the custom config bundle which is loaded after the FrameworkBundle. Right now what I do is:
place the config file inside the customBundle/Resources/config/
Re-run the templating pass by adding this to the custom bundle:
$container->addCompilerPass(new TemplatingPass());
I wonder if this is the best way to do so? (I know I can place the config inside the app/config) but I would like to place this config inside the customBundle if possible to make sure all the bundle's specific config is placed inside the package.

Related

Is there a way to override bundle DI extension file in Symfony?

I'm having a hard time figuring out how to override the DI extension file in Sf2.
The reason I want to override this file is that I'm using a 3rd party bundle where they define certain services, check for certain params in this file. However, since I want to customize these settings and store them in a different way I need a way to either override or skip the loading of this file.
I have searched high

Are you allowed to create seperate ASP bundles for resources in the same directory?

Suppose I have a directory with multiple css files. Is there a way to create and reference multiple ASP bundles for the same directory?
Let's say you have a folder with several stylesheets.
I know you can include specific files by their virtual paths like this:
//Bundle1
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery.ui.core.css",
"~/Content/themes/base/jquery.ui.resizable.css",
"~/Content/themes/base/jquery.ui.selectable.css",
"~/Content/themes/base/jquery.ui.accordion.css",
"~/Content/themes/base/jquery.ui.autocomplete.css"));
What if I add another bundle, and this one includes stylesheets that are in the same directory as the previous bundle?
//Bundle2
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery.ui.button.css",
"~/Content/themes/base/jquery.ui.dialog.css",
"~/Content/themes/base/jquery.ui.slider.css",
"~/Content/themes/base/jquery.ui.tabs.css",
"~/Content/themes/base/jquery.ui.datepicker.css",
"~/Content/themes/base/jquery.ui.progressbar.css",
"~/Content/themes/base/jquery.ui.theme.css"));
Now that I've created the bundles, I want to reference them.
I know I can call the following if there was just one bundle for the directory
#Styles.Render("~/Content/themes/base/css", "~/Content/css")
The problem I'm having with Styles.Render though is that it only accepts a virtual path to a path. Lets say you create more than one bundle in the same directory (if that is even possible). You can't use Styles.Render to select which bundle you want. You can only give it a path. Is there another way to reference a bundle you create in BundleConfig without Styles.Render?
Or is Styles.Render is the only way to reference any style bundle in the HTML?
Are you allowed to create seperate ASP bundles for resources in the
same directory?
Yes
How would I reference these bundles with the Styles.Render method?
#Styles.Render("~/Content/CssBundle1")
#Styles.Render("~/Content/CssBundle2")
The important thing to illustrate here is the reasoning behind bundling and the intended use of it. Bundling exists to reduce the number of requests and improve the load time of your application. You could do this manually, but you would have spaghetti code to manage. The bundling is there to keep everything modularized and easily organized.
Where I think there might be confusion
StyleBundle("~/Content/magicaunicornsdacningonrainbowsthisnameisrelative")
When you create a bundle, the virtual path can be named whatever you want. You bundle it in categories that makes sense.
//Bundle1
bundles.Add(new StyleBundle("~/Content/pets").Include(
"~/Content/dog",
"~/Content/cat",
//Bundle2
bundles.Add(new StyleBundle("~/Content/cities").Include(
"~/Content/memphis.css",
"~/Content/bejing.css",
//Bundle3
bundles.Add(new StyleBundle("~/Content/people").Include(
"~/Content/joseph.css",
"~/Content/michael.css",
With the bundles created, you make sure to call the bundles in the order you need them, loading only what is needed for each page.
#Styles.Render("~/Content/pets", "~/Content/cities")
* LOTS OF STUFF LOADED HERE! BUT YOU DO NOT NEED THE PEOPLE BUNDLE RIGHT AWAY *
#Styles.Render("~/Content/people")
You would simply create a different bundle for that references all the needed files, and you would call that bundle as needed. If you need to break up the order in which a file renders scripts or styles then you have multiple bundles. MVC is a lot about proper modularization, so you're always working up towards a root or singularity.
Getting more advanced
Obviously you can kick it up a notch. The next steps include using .less or .sass preprocessors for your style bundles. Those will help with very detailed modularization. Next, you can start using variables and conditions to determine which bundles should be run, linked below.
Or is Styles.Render is the only way to reference any style bundle in
the HTML?
There are other ways to reference css files via code. For instance, you could write a simple for loop to accomplish the task of writing the needed css scripts. Additionally, you could use razor variables.
Resource
Variables in Styles.Render
http://www.asp.net/mvc/overview/performance/bundling-and-minification

define custom filesystem path for twig templates

I've already read How does Symfony2 detect where to load templates from? and another ton of articles on the web
The question was quite similar to mine, but the answer wasn't comprehensive.
I need to override a bundle-defined template inside another custom bundle. In other words I want specify another path where symfony have to look before looking in app/Resources when loading templates.
Workflow should be something like this:
first: /src/My/Bundle/Resources/views/example.html.twig (or /src/My/Bundle/OriginalBundle/views/example.html.twig)
then: /app/Resources/OriginalBundle/views/example.html.twig
finally: /src/Original/Bundle/Resources/views/example.html.twig
The standard app/Resources -> src/Original/Bundle isn't enough.
sorry for my poor english and thank you
There's a native feature to do exactly what you want in a nice way. Escentially you can add a twig namespace to the twig configuration in app/config.yml like this:
twig:
# ...
paths:
"%kernel.root_dir%/../vendor/acme/foo-bar/templates": foo_bar
This creates an alias to the folder vendor/acme/foo-bar/templates and then you can use it to render your templates either from the controllers:
return $this->render(
'#foo_bar/template.html.twig',
$data
);
or from other twig templates
{% include '#foo_bar/template.html.twig' %}
Source: official cookbook http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html
To add a directory for twig templates I do this:
chdir(__DIR__);
$this->container->get('twig.loader')->addPath('../../../../web/templates/', $namespace = '__main__');
this allows me to put twig templates in a folder called 'templates' in the web folder and symfony 2.3 has no issues loading them.
The class responsible for loading twig templates is stored at the service id twig.loader, which by default is an instance of Symfony\Bundle\TwigBundle\Loader\FilesystemLoader, and the class for locating templates is stored at the service id templating.locator and by default is an instance of the class Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator (which itself is injected as the first parameter to twig.loader)
So, in theory, all you would need to do is write your own class that implements Symfony\Component\Config\FileLocatorInterface (or extends Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator) and then inform the configuration to use your class (which I was going to look up how to do this for you, but Symfony's websites are down right now)

Extending parent routing when extending bundles

In Symfony2 (2.0.3) I have a BetaBundle that is set as the parent of a AlphaBundle. Is it possible to override some routes while still keeping the parent originals routing definition ?
I have tried importing the parent routing.yml inside the child routing.yml file but it naturally result in a circular reference exception.
Is there any standard way to achieve this using yml and files named routing.yml in the same relative path ?
When you override your AlphaBundle anything that uses the #AlphaBundle shortcut will look in #BetaBundle first. The only way I've found to solve this kind of problem is to have the extending bundle (BetaBundle) quit using the # shortcut, and include your AlphaBundle's route using it's path.
As an alternative you could try renaming your BetaBundle's route file so that it doesn't override AlphaBundle's file, and then configure your app/config/routing.yml to include your BetaBundle's renamed routing file.

Using directives in new files

Is it possible to have additional "using" directives automatically added to my new aspx.cs files so that I do not have to keep typing the same ones over and over again (i.e. custom namespace using directives)
You can edit the files that are used by the template. Better yet, create your own. File + Export Template.
You don't have to type them in. When writing the code, type in the name of the class (without the namespace). Then hit CTRL+.. That will open up the resolve type intellisense helper. It will add the using statement to the top of the file. No scrolling necessary.

Resources