views:

31

answers:

3

Hi!

On the project I am currently working, I had created an action that generates a csv file.

Here is some of my current template codem, which generated the csv file on-the-fly:

<?php header('Content-Disposition: attachment; filename="file_'.date("Y-m-d_H-i",time()) .'.csv"'); ?>
Branch:;<?php echo $branch; ?>;

The variable $branch, needs some formatting to be displayed on the csv file. For now, all the cleaning/formatting work is being done on the action itself but I do know it is not the most adequate place.

Should I create a private auxiliary function on the file that contains the action or it is recommended a more suitable place?

Note that I would like to avoid to perform the cleaning/formatting work on the template because some is quite extensive.

Thanks in advance for the help, Best regards!

A: 

You can do this one of several ways.

But the most "symfony" way would be to create a validator and implement your formatting in the doClean() method.

Peter Bailey
I forgot to mention. The variables which I desire to send to the template correspond to the filters of a a search form. As example, the date corresponding to the last process (domain model), if the process is finished or not, etc. These filters are not stored on the database but are useful to put on the first lines of the csv generate file, for contextualize the user when he is viewing the file ("these records corresponds are the finished processes", as example). Best regards.
Rui Gonçalves
A: 

Why not just create a class in your /lib/model directory. Symfony will automatically load it, and you can start using it in your code.

Put your cleanup functions inside that class and call it from your template.

e.g /lib/model/doctrine/cleanup.class.php

class cleanup
{
    public function cleanitup($txt) { // docleanup }
{

//$cleaner = new cleanup();
//$cleaner->cleaneitup($foo);
Jon
Why not make it abstract and create it has a static method if its a once only instance.... `cleanup::html($foo);` or `cleanup::escape($bar)`
RobertPitt
And what about creating a private method on the action itself? The formatting/cleaning function is not supposed to be used anywhere else.
Rui Gonçalves
A: 

You can also create a helper (helpers are designed for this kind of operation).

  1. create a file stuffHelper.php in a lib/helper directory
  2. add your function clean_stuff() inside
  3. load it with use_helper('stuff') in your template
  4. echo clean_stuff($myStuff) in the template

But not in the model, it's a view method !

JF Simon