Инструменты пользователя

Инструменты сайта


limb3_2007_4:en:packages:macro:php_code_in_templates

php-code in {{macro}} templates

As you may know macro compiles initial templates into regular php-class. That's why you can insert any php-blocks in macro templates, e.g.:

 <select name='my_selector'>
  <?php 
   foreach($this->items as $item) 
   {
     echo '<option value="'. $item['value'] . '" ';
      if(some_condition())
       echo 'selected';
     echo '>'. $item['title'] .'</option>';
   }
  ?>
 </select>

You should only understand that any php-block you insert into macro template will be placed into one of the methods of the generated class. In what exactly method is depending on if any of include, into, apply or wrap tags were used. That's why if you need some particular variables in your php-block you need to make them available globally (make them attributes of the generated class) or pass them into local scope using extra attributes of the mentioned tags.

When it's ok to use php-code in {{macro}} templates

Here is the list of situations when we prefer to use raw php-code in macro templates:

  • Conditional logic (if/else). MACRO does not have analogs of WACT <core:optional> and <core:default> tags at the moment.
  • Complex computations (remember: don't place business logic into templates).
  • Any non-standard situations when build-in macro tags are not suit and creating your own macro tags is time wasting.

Обсуждение

Ваш комментарий. Вики-синтаксис разрешён:
     __   _  __   ____   ___    _  __
 __ / /  | |/_/  / __/  / _ )  / |/ /
/ // /  _>  <   / _/   / _  | /    / 
\___/  /_/|_|  /_/    /____/ /_/|_/
 
limb3_2007_4/en/packages/macro/php_code_in_templates.txt · Последние изменения: 2010/11/10 10:02 (внешнее изменение)