-
Join our Telegram chat: https://t.me/a_parser_enDismiss Notice
Template Toolkit
-
Contents
Description and links to documentation(top)
In A-Parser is applied to formatting of requests and resultsTemplate Toolkit. This allows to create finite string (for example requests or results) by rules given in a template. Template Toolkit has a set of opportunities:
- Support of conditions and cycles
- Support variable (scalars), arrays and hashes (associative arrays)
- Support of methods for variables (length of string, search and replacement, array size...)
- Support of plug-ins and filters (output and formatting of a date and time, generation of HTML elements...)
- Introduction - general information
- Syntax - rules and style of writing templates
- Directives - conditions, cycles, filters, plug-ins and macroses
- Variables - access and up-dating of values of variables, arrays and hashes
- Virtual methods - the preset functions on processing of variables, arrays and hashes
- Plug-ins and Filters - enhanced features of Template Toolkit, A-Parser supports the following plug-ins:
- Date - for generation of the formatted strings with dates
- Dumper - a data structure dump output
- Format - for creation of the formatting functions on the basis of syntax printf
- HTML - for creation of HTML elements and screening of HTML code
- Filter - to create and use filters that are defined and accessible through the plugin download
- Math - for use of any mathematical functions
- String - realizes additional methods for operation with strings
- Table - for data representation in the form of table
- URL - for creation of links
- Wrap - for formatting of paragraphs
- Iterator - to bypass the data set. The iterator is implicitly automatically created by the FOREACH directive. This plugin allows you to create an iterator explicitly with the specified name
Example of using plugins (Math)(top)
Randomly select the specified number of links:
Code:[% limit = 5; USE Math; FOREACH i IN [1..5]; n = Math.rand(intlinks.size); intlinks.$n.link _ "\n"; END %]
Templates testing tool(top)
For fast check of templates the Template tester tool is built in A-Parser, it contains the preset results for all parsers of test requests, that allows to execute a formatting template without actual parsing
- Testing of templates availablly to each parser separately, for this purpose it is necessary to select the preset project
- Results are provided in the JSON format, that allows to make if necessary changes to basic data (in more detail about JSON on WikiPedia)
- After change of a template necessary to apply it - the button Run in the upper right corner
- If the template is made incorrectly that in the field Result there will be a detailed error
- the Preset projects can't be saved, for saving changes it is necessary to add the new project ()
Feature of work templates in A-Parser(top)
.format method for arrays(top)
In a parser the majority of results is presented in the form arrays with nested elements, speaking technical language - results are presented in the form of an array of hashes, where each hash has the fixed keys. We will sort an example on a parserSE::Google, it contains an array serp in results with elements link, anchor and snippet:
Code:"serp" : [ { "link" : "http://www.speedtest.net/", "anchor" : "Speedtest.net by Ookla - The Global Broadband Speed <b>Test</b>", "snippet" : "<b>Test</b> your Internet connection bandwidth to locations around the world with this <br>interactive broadband speed <b>test</b> from Ookla." }, { "link" : "http://www.speakeasy.net/speedtest/", "anchor" : "Speakeasy Speed <b>Test</b>", "snippet" : "Speakeasy Speed <b>Test</b> - Broadband Speed <b>Test</b>. Go to MegaPath Speed <b>Test</b> ... <br>02:38:36 PM Your IP: The Speakeasy Speed <b>Test</b> requires Flash v7 or higher." }, { "link" : "http://en.wikipedia.org/wiki/Test_cricket", "anchor" : "<b>Test</b> cricket - Wikipedia, the free encyclopedia", "snippet" : "<b>Test</b> cricket is the longest form of the sport of cricket. <b>Test</b> matches are played <br>between national representative teams with "<b>Test</b> status", as determined by the ..." } ]
For convenient bypass of such array .format method was created, which allows unite all array cells on a certain format, for example all links through line break:Code:$serp.format('$link\n')
Code:http://www.speedtest.net/ http://www.speakeasy.net/speedtest/ http://en.wikipedia.org/wiki/Test_cricket
Code:$serp.format('$snippet\n')
Code:[lang=ru-RU]<b>Test</b> your Internet connection bandwidth to locations around the world with this <br>interactive broadband speed <b>test</b> from Ookla.[/lang][lang=en-US][code]<b>Test</b> your Internet connection bandwidth to locations around the world with this <br>interactive broadband speed <b>test</b> from Ookla.[/lang] [lang=ru-RU]Speakeasy Speed <b>Test</b> - Broadband Speed <b>Test</b>. Go to MegaPath Speed <b>Test</b> ... <br>02:38:36 PM Your IP: The Speakeasy Speed <b>Test</b> requires Flash v7 or higher.[/lang][lang=en-US]Speakeasy Speed <b>Test</b> - Broadband Speed <b>Test</b>. Go to MegaPath Speed <b>Test</b> ... <br>02:38:36 PM Your IP: The Speakeasy Speed <b>Test</b> requires Flash v7 or higher.[/lang] [lang=ru-RU]<b>Test</b> cricket is the longest form of the sport of cricket. <b>Test</b> matches are played <br>between national representative teams with "<b>Test</b> status", as determined by the ...[/lang][lang=en-US]<b>Test</b> cricket is the longest form of the sport of cricket. <b>Test</b> matches are played <br>between national representative teams with "<b>Test</b> status", as determined by the ...[/lang]
Code:$serp.format('Link: $link, Anchor: $anchor, Snippet: $snippet\n')
Code:[lang=ru-RU]Link: http://www.speedtest.net/, Anchor: Speedtest.net by Ookla - The Global Broadband Speed <b>Test</b>, Snippet: <b>Test</b> your Internet connection bandwidth to locations around the world with this <br>interactive broadband speed <b>test</b> from Ookla.[/lang][lang=en-US]Link: http://www.speedtest.net/, Anchor: Speedtest.net by Ookla - The Global Broadband Speed <b>Test</b>, Snippet: <b>Test</b> your Internet connection bandwidth to locations around the world with this <br>interactive broadband speed <b>test</b> from Ookla.[/lang] [lang=ru-RU]Link: http://www.speakeasy.net/speedtest/, Anchor: Speakeasy Speed <b>Test</b>, Snippet: Speakeasy Speed <b>Test</b> - Broadband Speed <b>Test</b>. Go to MegaPath Speed <b>Test</b> ... <br>02:38:36 PM Your IP: The Speakeasy Speed <b>Test</b> requires Flash v7 or higher.[/lang][lang=en-US]Link: http://www.speakeasy.net/speedtest/, Anchor: Speakeasy Speed <b>Test</b>, Snippet: Speakeasy Speed <b>Test</b> - Broadband Speed <b>Test</b>. Go to MegaPath Speed <b>Test</b> ... <br>02:38:36 PM Your IP: The Speakeasy Speed <b>Test</b> requires Flash v7 or higher.[/lang] [lang=ru-RU]Link: http://en.wikipedia.org/wiki/Test_cricket, Anchor: <b>Test</b> cricket - Wikipedia, the free encyclopedia, Snippet: <b>Test</b> cricket is the longest form of the sport of cricket. <b>Test</b> matches are played <br>between national representative teams with "<b>Test</b> status", as determined by the ...[/lang][lang=en-US]Link: http://en.wikipedia.org/wiki/Test_cricket, Anchor: <b>Test</b> cricket - Wikipedia, the free encyclopedia, Snippet: <b>Test</b> cricket is the longest form of the sport of cricket. <b>Test</b> matches are played <br>between national representative teams with "<b>Test</b> status", as determined by the ...[/lang]
Code:$serp.format('$query: $link\n')
Code:test: http://www.speedtest.net/ test: http://www.speakeasy.net/speedtest/ test: http://en.wikipedia.org/wiki/Test_cricket
Available variables(top)
In case of execution of a template the following variables are available:
- When formatting queries
- When formatting results
- When forming the file name results
- When filtering results
Additional options for using templates(top)
Templates when filtering results
Check of finding in link of initial request as file (excludes any other entrances, for example folder/index.php/)
Templates when using the option Parse custom result
Collection of the following 2 words in the snippets going after request
Pay attention that when filtering results and in the option Parse custom result interpolation of variables through $ character is disconnected, the template needs to be specified obviously through tags [% %]
Interpolation of variables(top)
By default templates register between tags [% and %], all that outside tags - the plain text, which will be transferred to result as is
- In A-Parser interpolation of variables is in addition turned, that allows to address to variables in the text through $ character
- In addition it is also interpolated \n as explicit line break
Code:In total results on request $query: $totalcount\n
The equivalent record without interpolation use:Code:In total results on request [% query %]: [% totalcount, "\n" %]
Pay attention that in templates variables register without $ prefix
Setup preset macros(top)
In A-Parser it is possible to set up macroses and the preset variables, which will be available globally to all templates, it is possible to specify global macroses in Settings -> Advanced settings:
By default there already contains predetermining of object $datefile, which is used for formatting time for result file name