Symfony: Is it possible to setTemplate for components?

青春壹個敷衍的年華 提交于 2019-12-10 02:48:51

问题


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/)


回答1:


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.



回答2:


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




回答3:


To get around this, i'm doing:

echo get_component('module', 'action', $this->getVarHolder()->getAll());
return sfView::NONE;



回答4:


This worked for me:

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

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



来源:https://stackoverflow.com/questions/1612727/symfony-is-it-possible-to-settemplate-for-components

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!