Overview and documentation
A-Parser uses the Template Toolkit engine for formatting queries and results. This template engine allows you to form the final string (e.g., queries or results) based on rules defined in a template. Template Toolkit offers a wide range of features:
- Support for conditions and loops
- Support for regular variables (scalars), arrays, and hashes (associative arrays)
- Support for variable methods (string length, search and replace, array size...)
- Support for plugins and filters (date and time output and formatting, HTML element generation...)
Template engine usage example
Official Template Toolkit documentation
A major advantage of this template engine is the availability of comprehensive 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 - for dumping data structures
- 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 a variable's value:
[% IF p1.totalcount < 1000;
query _ " - few\n";
ELSIF p1.totalcount < 1000;
query _ " - normal\n";
ELSE;
query _ " - many\n";
END %]