Skip to main content

Overview and Documentation

In A-Parser, the Template Toolkit is used for formatting requests and results. The template engine allows the final string (e.g., requests or results) to be formed 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 variable methods (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

A big advantage of this template engine is the availability of full documentation in Russian and English; links to the most important sections of the documentation are provided below:

  • Introduction - general information
  • Syntax - rules and style for writing templates
  • Directives - conditions, loops, filters, plugins, and macros
  • Variables - accessing and updating variable, array, and hash values
  • Virtual Methods - predefined functions for processing variables, arrays, and hashes
  • Plugins and Filters - extended capabilities of the template engine, A-Parser supports the following plugins:
    • Date - for generating formatted strings with dates
    • 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 accessible through plugin loading
    • Math - for using any mathematical functions
    • String - implements additional methods for working with strings
    • Table - for representing data as a table
    • URL - for constructing links
    • Wrap - for formatting paragraphs
    • Iterator - for iterating over a dataset. The iterator is automatically created implicitly by the FOREACH directive. This plugin allows you to create an iterator explicitly with a 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 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 WHILE loop

Output 5 anchors from the array $serp, starting from the 3rd:

[% n = 2;
WHILE n < 7;
p1.serp.${n}.anchor _ "\n";
n = n + 1;
END %]

Example of condition

Outputting 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 %]