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

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


limb3_2007_4:en:packages:macro:how_to_create_new_filter

How to create your own {{macro}} filters

macro filters are stored in files with .filter.php suffix in file name. Filters files can be found in limb/macro/src/filters/ folder and also in src/macro folders of Limb3 packages and Limb-based applications.

Filters core classes

You can select amount two classes for your filter as a parent class:

  • lmbMacroFunctionBasedFilter - used to create a macro filter that will apply generic PHP function or any other function to filtered value. The examples of such filters are: html, nl2br, trim and others. In the simplest form you just need to inherit from lmbMacroFunctionBasedFilter and to specify function name.
  • lmbMacroFilter - now generic class for filters. Can be used in more complex cases.

Filter annotations

In the head of any filter class file there is an annotation section, for example:

<?php 
/**
 * @filter number_format
 * @aliases number
 */ 
class lmbMacroNumberFormatFilter extends lmbMacrounctionBasedFilter
{
  [..]
}  

The following annotations currently can be used for filters:

  • filter - main filter name..
  • aliases - other filter names. Several aliases separated with commas.

Creating a filter based on lmbMacroFunctionBasedFilter

Let's consider an example of creating a macro filter that apply some quote($sting) function. In template we suppose to use the follwing syntax for applying this filter: {$var|quote}.

Let's call our filer as QuoteFilter. Here is the source code for this filter:

<?php 
/**
 * @filter quote
 */ 
class QuoteFilter extends lmbMacroFunctionBasedFilter
{
  protected $function = 'quote';
}  
?>

That it!

As result MACRO will compile expression {$title|quote} into <?php echo quote($title); ?>

If filter usage involves one or several parameters we can just specify them in template and lmbMacroFunctionBasedFilter automatically will pass them into function in the same order just after first argument. For example, the expression {$my_number|number_format:2,$del} will be compiled into the following PHP code:

number_format($title, 2, $del);

You may also want to use $include_file attribute of lmbMacroFunctionBasedFilter in order to require PHP file with function declaration:

<?php 
/**
 * @filter quote
 */ 
class QuoteFilter extends lmbMacroFunctionBasedFilter
{
  protected $function = 'quote';
  protected $include_file = '/path/to/quote_function_declaration.php';
}  
?>

Creating more complex filter

Any filter has a reference to so called $base. $base - is either an object of lmbMacroExpressionNode class or other filter in case if two or more filters applied to an expression. $base supports getValue() that returns a piece of PHP code with $base value.

Let's take a look at date filter that can be found in limb/macro/src/filters/core/date.filter.php. Although date() is just a regular PHP function we can't use lmbMacroFunctionBasedFilter as a parent class in this case since date() accepts $format argument first. That why we inherited from lmbMacroFilter:

/**
 * class lmbMacroDateFilter.
 *
 * @filter date
 * @package macro
 * @version $Id$
 */ 
class lmbMacroDateFilter extends lmbMacroFilter
{
  function getValue()
  {
    return 'date(' . $this->params[0].', ' . $this->base->getValue() . ')';
  }  
} 

Filter params are available by $params attribute. Parameters don't have any names so we have to reference them by index. Parameters are stores as is that why you don't need to escape while writing to compiled template. For example, $this→params[0] for expression like {$var|trim:«&»} will store exactly «&«.

Обсуждение

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