Overview and Documentation
A-Parser uses the Template Toolkit engine for formatting queries and results. The template engine allows you to form the final string (e.g., queries or results) according to the rules specified in the template. Template Toolkit has many features:
- Support for conditions and loops
- Support for regular variables (scalars), arrays, and hashes (associative arrays)
- Support for methods for variables (string length, search and replace, array size...)
- Support for plugins and filters (output and formatting of date and time, generation of HTML elements...)
Template engine usage example
Official Template Toolkit documentation
A major advantage of this template engine is the availability of full documentation in Russian and English; below are links to the most important sections of the documentation:
- Introduction - general information
- Syntax - rules and style for writing templates
- Directives - conditions, loops, filters, plugins, and macros
- Variables - accessing and updating values of variables, arrays, and hashes
- Virtual Methods - predefined functions for processing variables, arrays, and hashes
- Plugins and Filters - advanced features of the template engine; A-Parser supports the following plugins:
- Date - for generating formatted date strings
- Dumper - outputting a data structure dump
- Format - for creating formatting functions based on printf syntax
- HTML - for creating HTML elements and escaping HTML code
- Filter - for creating and using filters defined and available via plugin loading
- Math - for using any mathematical functions
- String - implements additional methods for working with strings
- Table - for representing data in table form
- URL - for building links
- Wrap - for formatting paragraphs
- Iterator - for traversing a data set. An iterator is implicitly created automatically by the FOREACH directive. This plugin allows creating an iterator explicitly with a specified name
Usage examples
Examples of frequently used templates
Math plugin usage example
Randomly select a specified number of links:
[% limit = 5;
USE Math;
FOREACH i IN [1..5];
n = Math.rand(intlinks.size);
intlinks.$n.link _ "\n";
END %]
FOREACH loop example
Output all links and their positions from the $serp array:
[% FOREACH item IN p1.serp;
loop.count _ ' - ' _ item.link _ "\n";
END %]
WHILE loop example
Output 5 anchors from the $serp array, starting from the 3rd one:
[% n = 2;
WHILE n < 7;
p1.serp.${n}.anchor _ "\n";
n = n + 1;
END %]
Condition example
Output specific data depending on the variable value:
[% IF p1.totalcount < 1000;
query _ " - few\n";
ELSIF p1.totalcount < 1000;
query _ " - normal\n";
ELSE;
query _ " - many\n";
END %]