This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » Symfony 2.0 » Installing custom templating helper services.
Installing custom templating helper services. [message #95316] Thu, 11 March 2010 23:07 Go to next message
xdissent  is currently offline xdissent
Messages: 4
Registered: March 2010
Location: Nashville, TN
Junior Member

I haven't yet figured out how to hook my custom templating helper services into the global Symfony 2 container. Anyone have any ideas? I haven't really looked into this whole "dependency injection" system yet, but I imagine I should be able to register a class as a default template helper service from within my config file.
Re: Installing custom templating helper services. [message #95398 is a reply to message #95316 ] Fri, 12 March 2010 23:13 Go to previous messageGo to next message
lkrubner  is currently offline lkrubner
Messages: 297
Registered: July 2008
Location: Virginia, USA
Faithful Member
Are you talking about custom code you want to call in the templates? The auto-loading should still work, just like code that used to be stored in the lib/ folder. You will want to put your custom code in the src/ folder. I assume you've already read this:

http://symfony-reloaded.org/quick-tour-part-5

[Updated on: Fri, 12 March 2010 23:14]



Symfony Experts offers answers: http://www.symfonyexperts.com/
Re: Installing custom templating helper services. [message #95399 is a reply to message #95398 ] Sat, 13 March 2010 00:50 Go to previous messageGo to next message
xdissent  is currently offline xdissent
Messages: 4
Registered: March 2010
Location: Nashville, TN
Junior Member

I was speaking specifically about extending the WebBundle's templating service, by adding helper services to the defaults. I can do something similar from within a controller action, right before rendering a view:

$helper = new FooHelper('omgwtfbbq');
$this->container->getTemplatingService()->set($helper, 'foo');
$this->render('FooBundle::home');


In the view, $view->foo will access the helper correctly. There's gotta be a way to inject the helper into the templating service globally, through a config file or a bundle method, right?
Re: Installing custom templating helper services. [message #95418 is a reply to message #95399 ] Sat, 13 March 2010 15:19 Go to previous message
xdissent  is currently offline xdissent
Messages: 4
Registered: March 2010
Location: Nashville, TN
Junior Member

For the uninitiated, I was trying to load a helper for the web.templating service, so that all views could access this helper (not just views rendered by a particular controller) and I wanted to accomplish this in a lazy fashion that didn't load the whole templating service at bundle creation. This morning I've got it all working, and here's the rundown:

Add a config file to your bundle describing the services (templating.yml). In the bundle's buildContainer method, register a custom extension with the DependencyInjection loader. Add "<extension alias>.templating: ~" to your global config to actually load the extension. The extension class's templatingLoad method will now trigger, in which you should load your bundle's templating service config and then merge and return a BuilderConfiguration instance.

That's it! Now when your controller requests the templating service, it will be loaded with the custom helpers.

I love Symfony 2. Dependency injection FTW!
Previous Topic:What about object, database schema ?
Next Topic:Starting New Project Symfony 2
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software