Clean variables before sending to template
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.
You can also create a helper (helpers are designed for this kind of operation).
- create a file stuffHelper.php in a lib/helper directory
- add your function clean_stuff() inside
- load it with use_helper('stuff') in your template
- echo clean_stuff($myStuff) in the template
But not in the model, it's a view method !
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.
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);
精彩评论