10

There’s no setTemplate() for components! I know but maybe there is another way to do it ?

(The question seems to be about a php framework: http://www.symfony-project.org/)

Nickolay
  • 31,095
  • 13
  • 107
  • 185
Andrey
  • 101
  • 1
  • 3

4 Answers4

10

There is no setTemplate method on sfComponents. You essentially have 3 options:

  1. Name your component the same as the partial you'd like the component to render. This may not be possible if you have multiple components you'd like to share the same template.
  2. Create a partial with the same name of your component and include the partial there. That is, if you had a component with an executeFoo() method that you wanted to render the _bar.php template, simply call include_partial('bar', $vars) inside of _foo.php.
  3. Load the PartialHelper and render the partial manually inside of the components execute method and have the component return sfView::NONE.
Jeremy Kauffman
  • 10,293
  • 5
  • 42
  • 52
  • But beware returning sfView::NONE on a component will not cache the component, at least in 1.0 - it is definitely worth checking the cache file is still generated – timmow Nov 02 '09 at 23:05
  • Are you sure the component will still not be cached even if text is rendered inside of it? If that is true, then yes, do not use method 3 if you want a cache-able component (I've never used method 3, myself.) – Jeremy Kauffman Nov 03 '09 at 00:31
  • 1
    With Symfony 1.4 you have to use `include_partial('bar', $vars)` instead of `include_partial('bar', $args)`. – chiborg Jun 23 '11 at 20:30
6

Components don't handle templates, you can only use partials. If you need to return a specific partial from inside your components class you can do something like this:

return get_partial('module/action', array('paramName' => $paramValue));

Have a look into the symfony book, chapter 7 view layer

virtualize
  • 2,287
  • 2
  • 25
  • 36
6

To get around this, i'm doing:

echo get_component('module', 'action', $this->getVarHolder()->getAll());
return sfView::NONE;
jmoz
  • 7,846
  • 5
  • 31
  • 33
  • This saved me a bunch of empty template files. – Lg102 Mar 17 '14 at 16:47
  • I was struggling with `return get_partial('module/partial');` but that only wouldn't bypass the regular template for the component. Now thanks to this response, it works like this for me: `echo get_partial('module/partial'); return sfView::NONE;` – Aitor Jan 12 '21 at 12:21
0

This worked for me:

$this->setVar('template', 'templateName');

Obviously the template have to be in the exactly same module.

Tom Raganowicz
  • 2,169
  • 5
  • 27
  • 41