开发者

Render a partial from a task in Symfony 1.4

I'm trying to render a partial in a Symfony task and having no luck. I found docs in 1.1 that say to just call get_partial() but apparently that's no longer readily available in 1.4. I tried loading the helper manually with sfLoader::getHelpers('Partial'); but I get "Class sfLoader not found". Any help would be greatly a开发者_运维百科ppreciated.

For reference what I'm trying to do is generate an HTML file called 'header.html' from my global header partial used in all of my layouts for inclusion in a third-party forum I'm integrating (Simple Machines/SMF).


First load:

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

then just call:

get_partial()


Don't forget to add an application name in your task's options. Look for this line:

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

Now $this->configuration returns the sfApplicationConfiguration that you need.


This will do the trick:

//load the Partial helper to be able to use get_partial()
$contextInstance = sfContext::createInstance($this->configuration);
$contextInstance->getConfiguration()->loadHelpers('Partial');


sfLoader was deprecated in symfony 1.2 - I think you need to look over the 1.4 API and the upgrade help from whichever version you're familiar with, as these are going to be resources you'll need to refer to a lot.

The trick to solving your problem is to load the helper with the loadHelpers() method provided by the sfApplicationConfiguration class - your task should hook this method in its configure() method. I've not done it before myself, mind...


Just called a partial in a task for use in sending free trial nag email here's the code:

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


you can access the current configuration with

$this->configuration

You can also define application command option in taskClass::configure() method (if you have used the symfony generate:task to generate the task class you should have frontend as default application option). Optionally you can pass an application using --application=appName from cli when calling your task.


Be sure to have the application option in your configure method:

protected function configure()
{
    $this->addOptions(array(
        new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend')
    ))
    ....

And then, in the execute method:

protected function execute($arguments = array(), $options = array())
{
    sfContext::createInstance($this->configuration)->getConfiguration()->loadHelpers('Partial');

On this way you will prevent these errors:

The "default" context does not exist.

and

Argument 1 passed to sfContext::createInstance() must be an instance of sfApplicationConfiguration

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜