Symfony World blog is not maintained anymore. Check new sys.exit() programming blog.

getRaw escaping problems

Scene from "Disney's Adventures of the Gummi Bears" by Jymn Magon & Art Vitello (introduced in 1985)

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.

session timeout in symfony 1.3, 1.4

Scene from "The Shining" by Stanley Kubrick (1980)

One of the main features to configure while developing a website that has access for users (in frontend, backend or both) is to set the session timeout. Its value defines time (measured in seconds) after which user session data will become out of date, if that user has performed no activity. For example, the default session expire time in symfony is 1800, which is 30 minutes (60*30), if a user is logged in and he won't do anything for at least 1800 seconds, he'll be automatically logged out.


solution

There are several blog posts in the internet describing where to change this value, but they refer only to symfony 1.0 (which is really old... and solutions described there do not apply to sf 1.3/1.4). So, the file is apps/APP_NAME/config/factories.yml. You need to add the following lines:

all:
  user:
    class: myUser
    param:
      timeout: 7200

where 7200 can be replaced with whatever unsigned integer value you want. Take a look at the main factories configuration file in symfony core: lib/vendor/symfony/lib/config/config/factories.yml. You'll find the default/user section there which holds all interesting stuff.

NetBeans code templates for Symfony

Scene from "The Smurfs" by Pierre Culliford (intruduced in 1958)

This short tutorial shows how to add code templates into the NetBeans IDE. It's based on version 6.8 of NetBeans including PHP support.


why using code templates?

Code templates fasten up the work of a programmer, when many templates are configured. You may define a combination of few keys to make the IDE insert a big piece of code for you. Tired of copy-pasting all the time? If so, code templates are just for you!


Go into the 'Tools' menu and choose 'Options':






Choose 'Editor' section and 'Code templates' tab:






Now you're ready to define your custom code templates. The abbreviation is the combination of keys you shall type to make the IDE insert your code (which is present in 'expanded text'). In the example above, you shall type the phrase 'sfexe' and then press tab to get action method template. Easy, isn't it? I hope you'll to find it useful!