nortron
nortron

Reputation: 3891

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 appreciated.

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

Upvotes: 4

Views: 11895

Answers (7)

JuliSmz
JuliSmz

Reputation: 1146

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

Upvotes: 0

Faraona
Faraona

Reputation: 1700

First load:

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

then just call:

get_partial()

Upvotes: 7

Robert Speer
Robert Speer

Reputation: 192

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

Upvotes: 1

Tilman
Tilman

Reputation: 1863

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

Upvotes: 3

Guillaume Flandre
Guillaume Flandre

Reputation: 8980

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.

Upvotes: 3

ftassi
ftassi

Reputation: 335

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.

Upvotes: 0

Raise
Raise

Reputation: 2034

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...

Upvotes: 1

Related Questions