Wednesday, February 3, 2010

Re: How to include an element in a view.

Hi Pau,
Are you using 1.2? If so, $this->renderElement is deprecated and you
should use $this->element instead. You can read up on elements at
http://book.cakephp.org/view/97/Elements.

But this specific situation sounds like a perfect task for a helper.
Helpers (see http://book.cakephp.org/view/101/Creating-Helpers) are
great for reusing code that will be used in multiple views across your
app. Doing so keeps you from repeating that code in lots of view
files.

Start by create a helper and placing the processing code there. Then
call the helper function from your view and pass in the data to be
processed. The helper should simply return the string for final
output, which you'd then echo in the view.

Does that make sense? Hope it works for you.

-Lance

On Feb 3, 2:33 am, Pau <pdomingu...@gmail.com> wrote:
> Hi.
> I'm traying to display in a view a string asociated to a field value.
>
> <?php
>   $i = 0;
>   foreach ($datas as $data):
>     $class = null;
>     if ($i++ % 2 == 0) {
>       $class = ' class="altrow"';
>     }
>
>    switch ($data['Model']['field'])
>     {
>     case 'A':
>          $varp = __('String1',true);
>          break;
>     case 'B':
>          $pasta = __('String2',true);
>          break;
>     case 'C':
>          $pasta = __('String3',true);
>          break;
>     };
>
>      .....
>     <td> ..  echo $varp ..</td>
>
> endforeach;
>
> ?>
>
> I need to reuse this switch statement in other views. I create an
> element with this code and store this file in the views/elements
> folder.
>
> <?php
>   $i = 0;
>   foreach ($datas as $data):
>     $class = null;
>     if ($i++ % 2 == 0) {
>       $class = ' class="altrow"';
>     }
>
>    $this->renderElement('mycode');
>
>      .....
>     <td> ..  echo $varp ..</td>
>
> endforeach;
>
> ?>
> If I use renderElement doesn´t work because the code is a Swicht
> estatement and render the element for each bucle cicle.
> I tryed a include('mycode.ctp') by I don´t know how to reach this
> folder.
>
> How is the best way to do this?
>
> Thanks.
> Pau.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments:

Post a Comment