
Probably most of us have encountered difficulties connected with default escaping mechanism in symfony templates. Yes, it is frustrating at times. But there are solutions to handle that.
single template solution
Suppose we have a executeShow action in our frontend module. We pass the object just retrieved from the db to the template.
public function executeShow(sfWebRequest $request) { $this->article = Doctrine::getTable('Article')->find(array($request->getParameter('id'))); }
Then we have to add one line at the beginning of the showSuccess.php template file:
$article = $sf_data->getRaw('article');
From this line, no data shall be escaped (and all formatting will be displayed properly).
nested templates solution
We can also have more complicated templates structure. Suppose we have a executeIndex action in our frontend module. We pass the list of objects to the template.
public function executeIndex(sfWebRequest $request) { $this->articles = Doctrine::getTable('Article') ->createQuery('a') ->execute(); }
Additionally, the indexSuccess.php file uses another template file:
<?php foreach ($articles as $article): ?> <?php include_partial('article/single', array('article' => $article)) ?> <?php endforeach; ?>
We have to use the getRaw method IN ALL template/partial files. Just like in the previous example, we have to add one line at the beginning of the indexSuccess.php template file:
$articles = $sf_data->getRaw('articles');
And one line shall be added at the beginning of the _single.php partial file (the same as in the first example):
$article = $sf_data->getRaw('article');
Above solutions shall be sufficient in most cases.