Symfony Partials: Accessing Raw Data defined in an Action
Sometimes we need to pass some message from the action to a partial via template. If this message only contains plain text there is no problem, we simply define the property in the action, and in the template we pass it to the partial, like this:
In the action:
// modules/mymodule/actions/actions.class.php class mymoduleActions extends sfActions { public function executeShow(sfWebRequest $request) { $this->msg = "My plain info message"; } }
In the template:
<!-- modules/mymodule/templates/showSuccess.php --> <hr /> <h1>My Module</h1> <?php include_partial('mypartial', array('msg' => $msg)); ?> <hr />
And in the partial:
<!-- modules/mymodule/templates/_mypartial.php --> <p><? echo $msg ?></p>
The problem comes when we include a little of html in the message, for example, lets change the action:
class mymoduleActions extends sfActions { public function executeShow(sfWebRequest $request) { $this->msg = "My <b>html</b> info message"; } }
Now, if we leave the template and the partial unchanged, you get this in the navigator:
My Module
My <b>html</b> info message
Ok, it seems that we forgot to get raw data, so lets change the template:
<!-- modules/mymodule/templates/showSuccess.php --> <hr /> <h1>My Module</h1> <?php include_partial('mypartial', array('msg' => $sf_data->getRaw($msg))) ?> <hr />
And now the result is:
My Module
My <b>html</b> info message
That is a little better, but it is not still the desired result. To get it, we have to also change the partial:
<!-- modules/mymodule/templates/_mypartial.php --> <p><? echo $sf_data->getRaw($msg) ?></p>
And finally we get what we wanted:
My Module
My html info message
The conclusion is that if we want to send raw data from the action to the partial, we will have to raw it in both, the template and the partial.
Category: Symfony | Tags: actions, partials, raw, Symfony 3 comments »

October 16th, 2009 at 8:41 pm
Hello from Russia!
Can I quote a post in your blog with the link to you?
October 19th, 2009 at 11:33 am
ok, send me a link to the quote when you finish
March 13th, 2010 at 4:49 am
Nice brief and this post helped me alot in my college assignement. Say thank you you for your information.