我正在尝试在Symfony任务中渲染部分内容,并且没有运气。我在1.1中找到了只调用get_partial()
的文档,但显然在1.4中不再可用。我尝试使用sfLoader::getHelpers('Partial');
手动加载帮助程序,但出现“找不到类sfLoader”的提示。任何帮助将不胜感激。
作为参考,我正在尝试从所有布局中使用的全局标头部分生成一个名为'header.html'的HTML文件,以包含在我正在集成的第三方论坛中(Simple Machines / SMF )。
首次加载:
sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
然后致电:
get_partial()
不要忘记在任务选项中添加应用程序名称。寻找这行:
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED,'The application name', 'frontend')
现在$this->configuration
返回所需的sfApplicationConfiguration
。
这将解决问题:
//load the Partial helper to be able to use get_partial()
$contextInstance = sfContext::createInstance($this->configuration);
$contextInstance->getConfiguration()->loadHelpers('Partial');
sfLoader
在symfony 1.2中已弃用-我认为您需要查看1.4 API和来自您熟悉的任何版本的升级帮助,因为这些将成为您需要大量参考的资源。
解决问题的技巧是使用loadHelpers()
类提供的sfApplicationConfiguration
方法加载帮助程序-您的任务应将此方法挂接到其configure()
方法中。请注意,我还没有做过...
在发送免费试用版nag电子邮件的任务中称为部分任务,这是代码:
//combines the partial with passed variables and puts the results in a string variable
$contextInstance = sfContext::createInstance($this->configuration);
$contextInstance->getConfiguration()->loadHelpers('Partial');
$partial_in_a_string = $contextInstance->getController()
->getAction('module_name', 'action_name')
->getPartial('module_name/partial_name', array('var_name'=>'var_value'));
您可以通过以下方式访问当前配置
$this->configuration
您还可以在taskClass :: configure()方法中定义应用程序命令选项(如果您已使用symfony generate:task生成任务类,则应将前端作为默认应用程序选项)。您可以选择在调用任务时使用cli中的--application = appName传递应用程序。
请确保您的configure方法中具有application选项:
protected function configure()
{
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend')
))
....
然后,在execute方法中:
protected function execute($arguments = array(), $options = array())
{
sfContext::createInstance($this->configuration)->getConfiguration()->loadHelpers('Partial');
通过这种方式,您将防止这些错误:
“默认”上下文不存在。
和
传递给sfContext :: createInstance()的参数1必须是一个实例的sfApplicationConfiguration