Skip to content
hertsch edited this page May 20, 2014 · 3 revisions

The Tools Service is a collection of functions which are needed by the TemplateTools itself or which are useful for Template Designers and does not fit to any other Service.

All of these functions are available within your php template script, some of the functions are also available as twig Function or Filter.

###addLanguageFiles()

The Template Tools support a Translation Service, you can use this function to add language files for your template to the Translator:

<?php $template['tools']->addLanguageFiles(TEMPLATE_PATH.'/locale'); ?>

will add all language files from the /locale directory to the Translator.

###ellipsis()

Parameter

  • string $text - text which should be shortened
  • integer $length - optional, default = 100, the max. length of the result

Usage php

<?php $template['cms']->ellipsis('Some text ... which is very, very long'); ?>

Usage twig (as Filter)

{{ 'Some text ... which is very, very long'|ellipsis }}

Usage twig (as Function)

{{ ellipsis('Some text ... which is very, very long') }}

This will output a string, which is shortened to the maximum $length and ... (three dots, ellipsis) at the end.

The ellipsis() function is in progress ... please check the documentation again in the near future!

###get_first_header()

Parameter

  • string $content - HTML content to parse

Usage php

<?php
    $content = $template['cms']->wysiwyg_content(12);    
    $header = $template['tools']->get_first_header($content);
    // do something with the header ...
    echo $header;        
?>

Usage twig (as Function)

{% set content = wysiwyg_content(12) %}
{% header = get_first_header(content) %}
{# do something with the header ... #}
{{ header }}

This function return the first <h1>, <h2> or <h3> header of the submitted HTML content without the embedding tags.

###humanize()

Parameter

  • string $text - text which should be humanized
  • boolean $prompt - optional, default = true (php only)

Usage php

<?php $template['tools']->humanize('EXAMPLE_IDENTIFIER'); ?>

Usage twig (as Filter)

{{ 'EXAMPLE_IDENTIFIER'|humanize }}

Usage twig (as Function)

{{ humanize('EXAMPLE_IDENTIFIER') }}

This will output the $text in a humanized form:

Example identifier

This function uppercase the first character of the string and lowercase all others. Underscores _ will be replaced by single spaces.

###readJSON()

Parameter

  • string $path - full path to the JSON encoded file

Usage php (Example):

<?php
    // read the JSON file - the CMS information in this case   
    $json = $template['tools']->readJSON(MANUFAKTUR_PATH.'/Basic/config/cms.json');
    
    // do something with the returned $json array
    echo "<pre>";
    print_r($json);
    echo "</pre>";        
?>

This function read a JSON file from the given path and return the content as associated array.

###remove_first_header()

Parameter

  • string $content - HTML content to parse

Usage php

<?php 
    $content = $template['cms']->wysiwyg_content(12);    
    $content = $template['tools']->remove_first_header($content);
    // do something with the content ...
    echo $content;        
?>

Usage twig (as Function)

{% set content = wysiwyg_content(12) %}
{% content = get_first_header(content) %}
{# do something with the content ... #}
{{ content }}

This function remove the first <h1>, <h2> or <h3> header of the submitted HTML and return the changed content.

###sanitizePath()

Parameter

  • string $path - path to sanitize

Usage php (Example):

<?php
    $path = $template['tools']->sanitizePath($origin_path); 
    
    // do something with the sanitized $path 
    ...
?>

This function will return a cleaned, sanitized path.

###sanitizeText()

Parameter

  • string $text - text to sanitize

Usage php (Example):

<?php
    $text = $template['tools']->sanitizeText($origin_text);
    
    // do something with the sanitized $text
    ...
?>

The function return a sanitized $text and convert HTML tags to their ISO-8859-1 equivalents and escape the the string, this is important to prevent a code injection.

See also: Code injection See also: unsanitizeText()

###unsanitizeText()

Parameter

  • string $text - text to unsanitize

Usage php (Example):

<?php
    $text = $template['tools']->unsanitizeText($origin_text);
    
    // do something with the unsanitized $text
    ...
?>

The function revert sanitized text to executable HTML or other code. In general all text information you read from the database will be sanitized and must be unsanitized before you can use the information.

See also: Code injection See also: sanitizeText()

Markdown Service | Translator Service