views:

30

answers:

1

It there an easy way to pass all the variables a template file has access to onto a partial when I have output escaping on?

I tend to create a template file, then refactor things into a partial at some point and it would seem that there would be an easy way to just pass all the same variables from the template to the partial and be done with it.

I have output escaping on and I can't just pass in $sf_data.

It look like calling a partial from within another partial is very simple...just pass in the variable $vars.

Edit: This is in regards to Symfony 1.2+

+2  A: 

Which version of Symfony are using?

TIP New in symfony 1.1: Instead of resulting in a template, an action can return a partial or a component. The renderPartial() and renderComponent() methods of the action class promote reusability of code. Besides, they take advantage of the caching abilities of the partials (see Chapter 12). The variables defined in the action will be automatically passed to the partial/component, unless you define an associative array of variables as a second parameter of the method.

so if you just do not pass the second argument of include_partial(), I guess you're done...

EDIT: completely wrong. Let's see what is done in renderPartial() : there is a call to getPartial(), which does this :

$vars = null !== $vars ? $vars : $this->varHolder->getAll();

So now, you can create a variable with all variables in your action:

  public function executeStackOverflow()
  {
    $this->testVar = 42;
    $this->allVars = $this->varHolder->getAll();
  }

Now you can call your partials and give them $allVars as second argument. Access granted to all variables.

greg0ire
Symfony version 1.2+
Failpunk
But can you render a partial to a variable in an action, then display that variable in a template file?
Failpunk
I guess so, but why would one do that?
greg0ire
I have an action that executes a template and that template has a couple of partials. If you use renderPartial() it ONLY renders the partial and not the template.
Failpunk
Ok, it seems I confused a lot of things, I thought the tip I gave you could be used directly in the view but... no.But you can still the piece of code in renderPartial(). I'm going to edit my answer.
greg0ire
Cool, I just have to call $sf_data->getRaw('allVars') since I have output escaping on.
Failpunk