Twig Service - phpManufaktur/kfTemplateTools GitHub Wiki
The Twig Service enable a full access to the Twig Template Engine. Twig is modern, fast, flexible and a very effective supplement for the classical way to create Templates for WebsiteBaker, LEPTON CMS or BlackCat CMS.
Twig 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.
- Introduction
- Using Namespaces
$template['twig']->display()$template['twig']->render()- Constants
- Additional Filters
- Additional Functions
Using the TemplateTools and Twig 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:
<!DOCTYPE html>
<html lang="en">
<?php
// initialize the TemplateTools
require_once WB_PATH.'/kit2/extension/phpmanufaktur/phpManufaktur/TemplateTools/initialize.php';
// include a simple <head> .. </head> section
$template['twig']->display('@Pattern/classic/head.simple.twig');
?>
<body>
<div class="navigation">
<?php $template['cms']->show_menu2(); ?>
</div>
<div class="content">
<?php $template['cms']->page_content(); ?>
</div>
</body>
</html>
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():
<?php
// initialize the TemplateTools
require_once WB_PATH.'/kit2/extension/phpmanufaktur/phpManufaktur/TemplateTools/initialize.php';
// add a Namespace
$template['twig.loader.filesystem']->addPath('/path/to/add', 'myNamespace');
?>
Now you can use also the Namespace @myNamespace.
Parameter
-
string $template- the template to parse -
array $parameter- optional, default =array(), parameters to pass to the template
Usage php (Example):
<?php
$template['twig']->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()
Parameter
-
string $template- the template to parse -
array $parameter- optional, default =array(), parameters to pass to the template
Usage php (Example):
<?php
$rendered = $template['twig']->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()
The TemplateTools add nearly hundred constants to the Twig Template Engine. You can easy access them as variables in all of your templates, for example:
<meta name="keywords" content="{{ PAGE_DESCRIPTION }}" />
{{ 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.
Twig already comes with a few tens of Filters and the TemplateTools adds some additional Filters:
Twig already comes with a dozen of Functions and the TemplateTools adds some additional Functions:
bootstrap_alert()bootstrap_breadcrumb()bootstrap_nav()bootstrap_pager()classic_breadcrumb()command()droplet()ellipsis()file_exists()humanize()markdown()markdown_file()page_content()page_description()page_image()page_next_id()page_previous_id()page_title()page_url()register_frontend_modfiles()register_frontend_modfiles_body()show_menu2()
Parameter
-
string $path- the full path to file to check
Usage twig (as Function)
{% 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 ⇒