Template Toolkit

Dec 25, 2015

  • 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...)
    Advantages of Template Toolkit existence of full documentation on English language, are given links to the most important sections of documentation below:
    • 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
      • 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

    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
    [​IMG]

    • 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 parser SE::Google SE::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 &quot;<b>Test</b> status&quot;, as determined by the&nbsp;..."
       }
    ]
    

    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')
    That as a result will save each link since a new line:
    Code:
    http://www.speedtest.net/
    http://www.speakeasy.net/speedtest/
    http://en.wikipedia.org/wiki/Test_cricket
    
    Snippets:
    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 &quot;<b>Test</b> status&quot;, as determined by the&nbsp;...[/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 &quot;<b>Test</b> status&quot;, as determined by the&nbsp;...[/lang]
    
    Links, anchors and snippets at the same time:
    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 &quot;<b>Test</b> status&quot;, as determined by the&nbsp;...[/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 &quot;<b>Test</b> status&quot;, as determined by the&nbsp;...[/lang]
    
    In format it is also possible to use the initial request, that allows to set compliance request - each array cell:
    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:

    Additional options for using templates(top)

    Templates when filtering results [​IMG]
    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[​IMG]
    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
    Example:
    Code:
    In total results on request $query: $totalcount\n
    
    To the place $query and $totalcount values of appropriate variables will be added, \n will be replaced with line break
    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:[​IMG]
    By default there already contains predetermining of object $datefile, which is used for formatting time for result file name