开发者

How can I easily pass all the variables from a template to a partial in Symfony with output escaping on?

开发者 https://www.devze.com 2023-01-02 17:59 出处:网络
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?

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 partia开发者_如何学Gol 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+


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.

0

精彩评论

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