tags:

views:

88

answers:

2

Hello I have the following proble:

I have the action class of a view in Symfony, action.class.php, in there i have this code:

foreach ($images as $i => $img)
 {
   if (strstr($request->getFileType($img), 'image'))
   {
     $enter = true; 
     $name = Util::processImages($request, $img, 'app_uploads_temp_dir');

     if ($name != '')
     {
        $userimages[$i] = $name;
        $this->uploads[$i] = true;
        $this->message = 'Image loaded';
     }
   }
}

And in my view I want to render an upload field or a message depending on the case:

<div class="fotoupload">
   <?php if ( !isset($this->uploads[1]) ): ?>
   <?php echo label_for('photosec1', 'Imagen 2:') ?>
   <?php echo input_file_tag('photosec1') ?>
   <?php else: ?>
   <span><?php $this->message ?></span>
   <?php endif ?>
</div>

but $this->message is not set, neither is $message or any variable passed from the action, can someone tell me why this happen and how could I solve it??

A: 

Ok everybody, I've found the answer myself, sorry for posting the answer, I'll leave it in case someone has the same problem:

the whole idea is to use slots, the code would be like this:

foreach ($images as $i => $img)
 {
   if (strstr($request->getFileType($img), 'image'))
   {
     $enter = true; 
     $name = Util::processImages($request, $img, 'app_uploads_temp_dir');

     if ($name != '')
     {
        $userimages[$i] = $name;
        $uploads[$i] = true;
        $message = 'Image loaded';
     }
   }
}

$this->getResponse()->setSlot("message", $message);

and in the next code, to use the variable:

   <span><?php echo get_slot("message") ?></span>

Thanks anyways!

  • If an admin feels that this question should be deleted, please tell me and I will do it
David Conde
forgot to add the source:http://www.symfonydeveloper.com/2009/03/05/passing-variables-from-an-action-or-module-template-to-a-layout-template/this is where I found the info
David Conde
A: 

If you set $this->message in your action, it is available as $message in your view.

But in your case, you are setting $this->message inside a foreach(), so you are re-defining $this->message for every loop. This might explain why $message was not returning the value you expect.

Using a slot here is not the proper use case as all $this-> vars set in actions are already passed directly to the view/template.

John Kary
i tried setting the variable outside the foreach and didn't worked :(
David Conde