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??

2
  • The thing you failed to note is that it is not the view you're pasting code from. It is the layout. If using $this->message = "Test"; in your action, it will be available in your view as $message, but it won't be accessible in your layout unless you use slots, or simply set add the variable to the request using $this->getRequest()->setParameter('message', 'Test');. Either way: it is a workaround solution :) Commented Sep 4, 2010 at 16:16
  • thank you very much, I'm kinda newb to symfony, but its a great framework tough... thanks again for the advice Commented Sep 5, 2010 at 0:27

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

forgot to add the source: symfonydeveloper.com/2009/03/05/… this is where I found the info
2

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.

1 Comment

i tried setting the variable outside the foreach and didn't worked :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.