Skip to main content

Overview and Documentation

In A-Parser, the Template Toolkit is used for formatting queries and results. The template engine allows you to form the final string (for example, queries or results) according to the rules set 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 dates and times, generation of HTML elements...)

Example of using the template engine

Official documentation for Template Toolkit

A big 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 of writing templates
  • Directives - conditions, loops, filters, plugins, and macros
  • Variables - access and updating the values of variables, arrays, and hashes
  • Virtual methods - preset 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 dump of the data structure
    • 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 presenting data in table form
    • URL - for building links
    • Wrap - for formatting paragraphs
    • Iterator - for iterating over a data set. An iterator is implicitly automatically created by the FOREACH directive. This plugin allows you to explicitly create an iterator 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 a FOREACH loop

Display all links and their positions from the $serp array:

[% FOREACH item IN p1.serp;
loop.count _ ' - ' _ item.link _ "\n";
END %]

Example of a WHILE loop

Display 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 a condition

Display certain data depending on the value of a variable:

[% IF p1.totalcount < 1000;
query _ " - мало\n";
ELSIF p1.totalcount < 1000;
query _ " - нормально\n";
ELSE;
query _ " - много\n";
END %]