Code snippets for symfony 1.x


Render partials in a Task

I needed to send emails from a task using partials. The technique for SF 1.3/4 is explained in the docs, but how to get links to render properly is not. I found some good info on the web that was a bit hidden. Here is what it lead me to come up with.

To be able to use partials you need to set the context correctly:


Since tasks don't have a url as their base for the request, helpers like url_for and link_to won't work correctly and output things like "/symfony/symfony/mymodule/myaction".

to remedy this I created a helper:

class batchHelpers
  public static function url_for($str)
    $url = 'http://'.
            sfConfig::get('app_server_url','') .
    return $url;

the define for the "server_url" is in app.yml

Then in the partial I used this code to call get a correctly generated url:

batchHelpers::url_for('/home', true), array(

I also render the same partial (with the same url_for replacement) inside the web interface and since the urls are absolute, they work correctly.

by Alex Knol on 2010-10-26, tagged email  partial  task  urlfor 

Comments on this snippet

gravatar icon
#1 Damien ALEXANDRE on 2010-12-09 at 03:54

Don't like this solution :/ It's more easy to hack the global SERVER var :


Put that in your task, and all your absolutes URL's will be nicely formated.

gravatar icon
#2 Damien ALEXANDRE on 2010-12-09 at 04:20

To complete my previous post.

Your task MUST run in a "no_script_name" env, you can add one in your settings.yml. And what you need to override is :


That's all.