Overview and Documentation
A-Parser uses the Template Toolkit template engine for formatting requests and results. The template engine allows you to generate a final string (for example, requests 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...)
Example of using the template engine
Official Template Toolkit Documentation
One of the great advantages 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 of writing templates
- Directives - conditions, loops, filters, plugins, and macros
- Variables - access and update of variable, array, and hash values
- Virtual Methods - predefined functions for processing variables, arrays, and hashes
- Plugins and Filters - advanced capabilities of the template engine, A-Parser supports the following plugins:
- Date - for generating formatted date strings
- Dumper - output of data structure dump
- Format - for creating formatting functions based on the printf syntax
- HTML - for creating HTML elements and escaping HTML code
- Filter - for creating and using filters defined and available through plugin loading
- Math - for using any mathematical functions
- String - implements additional methods for working with strings
- Table - for representing data in the form of a table
- URL - for building links
- Wrap - for formatting paragraphs
- Iterator - for traversing a set of data. The iterator is implicitly created by the FOREACH directive. This plugin allows you to create an iterator explicitly with the specified name
Usage Examples
Examples of frequently used templates
Example of using the Math plugin
Randomly select the specified number of links:
[% limit = 5;
USE Math;
FOREACH i IN [1..5];
n = Math.rand(intlinks.size);
intlinks.$n.link _ "\n";
END %]
Example of using the FOREACH loop
Output all links and their positions from the $serp array:
[% FOREACH item IN p1.serp;
loop.count _ ' - ' _ item.link _ "\n";
END %]
Example of using the WHILE loop
Output 5 anchors from the $serp array, starting with the 3rd:
[% n = 2;
WHILE n < 7;
p1.serp.${n}.anchor _ "\n";
n = n + 1;
END %]
Example of using the IF condition
Output specific data depending on the variable value:
[% IF p1.totalcount < 1000;
query _ " - мало\n";
ELSIF p1.totalcount < 1000;
query _ " - нормально\n";
ELSE;
query _ " - много\n";
END %]