The *Twig Service* enable a full access to the [Twig Template Engine](http://twig.sensiolabs.org/). *Twig* is modern, fast, flexible and a very effective supplement for the classical way to create Templates for [WebsiteBaker](http://websitebaker.org), [LEPTON CMS](http://lepton-cms.org) or [BlackCat CMS](http://blackcat-cms.org). [Twig](http://twig.sensiolabs.org/documentation) is well documented and comes to you with many predefined Tags, Filters, Functions, Tests and Operators. A good point to start is [Twig for Template Designers](http://twig.sensiolabs.org/doc/templates.html#twig-for-template-designers). ## Using Twig with the *TemplateTools* * [Introduction](#introduction) * [Using Namespaces](#namespaces) * [`$template['twig']->display()`](#display) * [`$template['twig']->render()`](#render) * [Constants](#constants) * [Additional Filters](#filters) * [Additional Functions](#functions) ###Introduction Using the *TemplateTools* and [Twig](http://twig.sensiolabs.org/) does *not* mean you *must* use *Twig* and if you are using *Twig* you are *not* forced to use *Twig* exclusive. You can always mix-up `php` code with `twig` parts. *Twig* is fast, the syntax is easy to learn and the Best of all: it is very simple to reuse your `twig` patterns, extend or fit them for your actual Template Project. Twig will speed up your projects. Example for a simple `index.php`: .. section $template['twig']->display('@Pattern/classic/head.simple.twig'); ?>
page_content(); ?>
###Namespaces Instead of typing long paths to access `twig` files, the *TemplateTools* are using Namespaces. Namespaces are predefined paths and indicated by a `@` at the beginning: * `@phpManufaktur => CMS_PATH./kit2/extension/phpmanufaktur/phpManufaktur` * `@thirdParty => CMS_PATH./kit2/extension/thirdparty/thirdParty` * `@Templates => CMS_PATH./templates` * `@Pattern => CMS_PATH./kit2/extension/phpmanufaktur/phpManufaktur/TemplateTools/Pattern` If you are not using a Namespace, i.e.: `$template['twig']->display('head.twig')` Twig will try to load the file `head.twig` from the *path of the current CMS Template*. You can also add a subdirectory or subdirectories: `$template['twig']->display('/twig/head.twig')` will try to load the file `head.twig` from the subdirectory `/twig` if the current Template path. `$template['twig']->display('@Pattern/classic/head.simple.twig')` will try to load the file `/classic/head.simple.twig` from the `@Pattern` path. If you need access to a path outside of the current Template and outside of the defined Namespaces, you can define an additional Namespace, using the function `$template['twig.loader.filesystem']->addPath()`: addPath('/path/to/add', 'myNamespace'); ?> Now you can use also the Namespace `@myNamespace`. ###`display()` Parameter * `string $template` - the template to parse * `array $parameter` - optional, default = `array()`, parameters to pass to the template Usage `php` (Example): display('my_template.twig', array( 'parameter_1' => 'value_1', 'parameter_2' => 'value_2' )); ?> Will load the template `my_template.twig`, pass the parameters `parameter_1` and `parameter_ 2` to the template, render it and prompt (output) the result. See also: [`render()`](#render) ###`render()` Parameter * `string $template` - the template to parse * `array $parameter` - optional, default = `array()`, parameters to pass to the template Usage `php` (Example): render('my_template.twig', array( 'parameter_1' => 'value_1', 'parameter_2' => 'value_2' )); // do something with the variable $rendered echo $rendered; ?> Will load the template `my_template.twig`, pass the parameters `parameter_1` and `parameter_ 2` to the template, render and return it without prompting, so you can use the rendered template as variable. See also: [`display()`](#display) ###Constants The *TemplateTools* add nearly hundred constants to the [Twig Template Engine](http://twig.sensiolabs.org/). You can easy access them as variables in all of your templates, for example: `{{ PAGE_DESCRIPTION }}` will return the description of the current page. Constants are always in UPPERCASE. Please have a look to the [[Constants]] page to get an overview of all in `twig` available constants. ###Additional Filters [Twig](http://twig.sensiolabs.org/) already comes with a few tens of [Filters](http://twig.sensiolabs.org/documentation) and the *TemplateTools* adds some additional Filters: * [`ellipsis`](Tools-Service#ellipsis) * [`humanize`](Tools-Service#humanize) * [`markdown`](Markdown-Service#html) * [`trans`](Translator-Service#trans) ###Additional Functions [Twig](http://twig.sensiolabs.org/) already comes with a dozen of [Functions](http://twig.sensiolabs.org/documentation) and the *TemplateTools* adds some additional Functions: * [`bootstrap_alert()`](Bootstrap-Service#alert) * [`bootstrap_breadcrumb()`](Bootstrap-Service#breadcrumb) * [`bootstrap_nav()`](Bootstrap-Service#nav) * [`bootstrap_pager()`](Bootstrap-Service#pager) * [`classic_breadcrumb()`](Classic-Service#breadcrumb) * [`command()`](Command-Service#execute) * [`droplet()`](Droplet-Service#execute) * [`ellipsis()`](Tools-Service#ellipsis) * [`file_exists()`](#file_exists) * [`humanize()`](Tools-Service#humanize) * [`markdown()`](Markdown-Service#html) * [`markdown_file()`](Markdown-Service#file) * [`page_content()`](CMS-Service#page_content) * [`page_description()`](CMS-Service#page_description) * [`page_image()`](CMS-Service#page_image) * [`page_next_id()`](CMS-Service#page_next_id) * [`page_previous_id()`](CMS-Service#page_previous_id) * [`page_title()`](CMS-Service#page_title) * [`page_url()`](CMS-Service#page_url) * [`register_frontend_modfiles()`](CMS-Service#register_frontend_modfiles) * [`register_frontend_modfiles_body()`](CMS-Service#register_frontend_modfiles_body) * [`show_menu2()`](CMS-Service#show_menu2) ###`file_exists()` Parameter * `string $path` - the full path to file to check Usage `twig` (as [Function](http://twig.sensiolabs.org/doc/templates.html#functions)) {% if file_exists(TEMPLATE_PATH ~ '/css/screen.css') %} {# do something with the file ... #} {% endif %} The function check if the given file at path exists and return boolean `true` or `false`. ⇐ [[Translator Service]] | [[Pattern]] ⇒