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

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


limb3_2007_4:en:packages:macro:data_sources

How {{macro}} template gets data for displaying?

macro considers that it works with literal variables or regular arrays. If you pass an object to macro template, make sure that your object is supports ArrayAccess or Iterator interfaces depending on situation.

There are two common ways to get data inside template:

  • push - data are pushed into template with some simple interface like set($var, $name) or assign($var, $name).
  • pull - templates gets required data itself by using helpers or callbacks.

macro supports both these approaches.

Push approach

lmbMacroTemplate has set($variable_name, $value) method that sets a variable $variable_name in template. This variable will be available in template by {$#variable_name} or {$this→variable_name} expressions.

For example:

$macro = new lmbMacroTemplate('page.phtml');
$macro->set('title', 'Hello');

To render 'title' in macro template we can use to following expression:

{$#title}

There is also lmbMacroTemplate :: setVars($associative_array) that is equal to several set() method calls for every element of $associative_array. Note: setVars() removes previously assigned variables.

Pull approach

You can also use php-blocks right in macro templates to insert data retrieval operations, e.g.:

<? $products = lmbActiveRecord :: find('Product'); ?>
{{list using='$products' as="$product"}}
<ul>
  {{list:item}}
    <li>{$product.title}</li>
  {{list:item}}
</ul>
{{/list}}

Data from lmbController

If you create an application based on WEB_APP package of Limb3, you need to know what template also receives all attributes of currently executed controller just like you called lmbMacroTemplate :: set() for every controller attribute.

For example:

class MyController extends lmbController
{
  function doDisplay()
  {
    $this->title = 'Hello';
  }
}

In the corresponding macro template $title attribute will be available as:

{$#title}

Contexts in {{macro}}

macro have only 2 contexts or data scopes:

  • global - scope of the generated php-class of template
  • local - scope of one method of this class.

Rendering is always started from render() method of the generated php-class. Some tags like include or apply generate their code into separate methods and thus create other local scopes. include and apply allow to pass any variables into their local scopes with any number of extra attributes (see descriptions of these tags).

See also output expressions.

Обсуждение

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