代码之家  ›  专栏  ›  技术社区  ›  nortron

在Symfony 1.4中渲染任务的部分

  •  3
  • nortron  · 技术社区  · 14 年前

    我试图在Symfony任务中呈现一个partial,但是运气不好。我在1.1里找到文件说只要打电话就行了 get_partial() sfLoader::getHelpers('Partial'); 但我得到“找不到类加载程序”。任何帮助都将不胜感激。

    作为参考,我试图做的是从我的全局头生成一个名为“header.HTML”的HTML文件,这个文件在我所有的布局中使用,以包含在我正在集成的第三方论坛(Simple Machines/SMF)中。

    6 回复  |  直到 14 年前
        1
  •  7
  •   Faraona    13 年前

    sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
    

    那就打电话:

    get_partial()
    
        2
  •  3
  •   Guillaume Flandre    14 年前

    别忘了在任务的选项中添加应用程序名。寻找这条线:

    new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED,'The application name', 'frontend')
    

    $this->configuration 返回 sfApplicationConfiguration 你需要的。

        3
  •  3
  •   Tilman    14 年前

    这将达到以下目的:

    //load the Partial helper to be able to use get_partial()
    $contextInstance = sfContext::createInstance($this->configuration);
    $contextInstance->getConfiguration()->loadHelpers('Partial');
    
        4
  •  1
  •   Raise    14 年前

    sfLoader

    loadHelpers() 方法由 sfApplicationConfiguration configure() 方法。我以前没做过,注意。。。

        5
  •  1
  •   Robert Speer    13 年前

    刚刚在一个任务中调用了一个partial,用于发送免费试用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'));
    
        6
  •  0
  •   ftassi    14 年前

    $this->configuration
    

    还可以在taskClass::configure()方法中定义应用程序命令选项(如果使用symfony generate:task生成任务类,则应将frontend作为默认应用程序选项)。 或者,您可以在调用任务时使用cli中的--application=appName传递应用程序。

        7
  •  0
  •   JuliSmz    5 年前

    确保在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必须是实例 应用程序配置