Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.


Stack Overflow

The old fashioned way


March 11, 2009 – 1:55am Symfony: How to render a partial from an action

If you ever find yourself wanting to return the contents of a partial as the entire response for an action, Symfony makes it possible. Using symfony partials with ajax, you have a lot of options of how you want to arrange your templates, but here I’ll show you what has worked best for me.

Oftentimes I find myself wanting to return the contents of a partial as the entire response for an ajax request. I do this a lot when I am already using a partial in my template, and then I want to replace that section with the results of an ajax call. We already have a partial set up to display that content, so we might as well reuse it when we have to refresh that area with new content.

Imagine we have a page that presents a list of articles. In the right sidebar we’d like to feature a preview of the first article. We’d also like to allow a “quick look” for the other articles. Clicking on the quick look button should load a preview of the selected article in the right sidebar, replaced the default preview.

To do this, I set up a partial that contains the article preview. In the default list template (e.g. listSuccess.php) I include the partial along with the featured article.

<?php include_partial('preview', array('article'=>$article)) ?>

Then, when someone clicks the quick look button, we want to make an ajax call that returns the new preview content with that particular article. Our action might look something like this:

public function executeLoadPreview()
  $this->article = ArticlePeer::retrieveByPk($this->getRequestParamer('articleId'));

The most obvious thing to do now would be to set up a standard loadPreviewSuccess.php tempate and within that template simply include the partial just as you would normally.

// loadPreviewSuccess.php
<?php include_partial('preview', array('article'=>$article) ?>

This is totally fine, and you may like it, but there is another way to do it. We can just render the contents of the partial directly from our action. Here is what it looks like. (Note that I turn off the web debugging so that it doesn’t render within the ajax call!)

public function executeLoadPreview()
  sfConfig::set('sf_web_debug', false);
  $article = ArticlePeer::retrieveByPk($this->getRequestParamer('articleId'));
  return $this->renderText(get_partial('preview', array('article' => $article)));


Posted by in  Uncategorized   |  

7 Responses to Symfony: How to render a partial from an action

  1. Kevin says:

    Hey, cool article.

    Did you know that as of symfony 1.1 you can simply return $this->renderPartial(‘preview’, array(‘article’ => $article)));


  2. Éric Rogé says:

    You can directly use from an action $this->getPartial() and $this->getComponent() from an action.

    Here is the doc :

  3. naholyr says:

    Plus, the Web Debug Toolbar is automatically disabled when in an Ajax context, simply because decorator is disabled in this context (can be seen line 197 of sfViewConfigHandler.php).

  4. Niko says:

    very usefull blog, I was searching how to include partials from the actions.. even the comments were usefull. thanks for being online :–)

  5. halfer says:

    Thanks for the tip – just what I was looking for to use with symfony 1.0.

  6. i still use symfony 1.0 on one of my project and wanna use this feature.

    Usually i do this with ordinary include, and then return sfView::NONE. It works .., but it’s not an elegan solution ..

    Is there any better ways todo this on symfony 1.0 ?