Created
April 5, 2015 12:10
-
-
Save nunocodex/469b04b9cbb0356e03cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Twig engine for KirbyCMS | |
* | |
* @author Pereira Ricardo <[email protected]> | |
* @license MIT | |
*/ | |
/** | |
* Installation: | |
* | |
* From root of your site you need to add twig from composer and require it. | |
* | |
* Usage: | |
* | |
* You can put easy into your KirbyCMS template file (example default.php or other.php) for loading the default settings. | |
* | |
* <code> | |
* <?php snippet('plugins/twig') ?> | |
* </code> | |
* | |
* With default options the cache is created on "cache/twig", the templates stay on "site/templates/twig", | |
* template name follow the same template name from KirbyCMS and the template extension is ".twig". | |
* | |
* To setup the options you can use the args when calling snippet function, if you want passing variables to twig you need | |
* to passing "data" array, follow this example: | |
* | |
* <code> | |
* <?php snippet('plugins/twig', ['data' => ['example' => 'Title Example']]) ?> | |
* </code> | |
* | |
* In this case the new variable is avariable into twig template with: | |
* | |
* <code> | |
* {{ example }} | |
* </code> | |
* | |
* You can setup the twig snippet with this array keys: | |
* | |
* - cache_dir: $kirby->roots()->cache() . DS . 'twig' | |
* - templates_dir: $kirby->roots()->templates() . DS . 'twig' | |
* - template: $page->template() . c::get('twig.templates_ext', '.twig') | |
* | |
* For setup this option is simple: | |
* | |
* <code> | |
* <?php snippet('plugins/twig', ['template' => 'another_template_name.twig']) ?> | |
* </code> | |
* | |
* You can setup all other options in the same way. | |
*/ | |
/** | |
* Local kirby instance. | |
*/ | |
$kirby = Kirby::instance(); | |
/** | |
* Require composer autoload. | |
*/ | |
require $kirby->roots()->index() . '/vendor/autoload.php'; | |
/**********************************************************************************************************************/ | |
/** | |
* Setup defaults options. | |
* | |
* twig.cache_dir | |
* twig.templates_dir | |
* twig.templates_ext | |
*/ | |
$defaults = [ | |
'cache_dir' => ((isset($cache_dir)) ? $cache_dir : c::get('twig.cache_dir', $kirby->roots()->cache() . DS . 'twig')), | |
'templates_dir' => ((isset($templates_dir)) ? $templates_dir : c::get('twig.templates_dir', $kirby->roots()->templates() . DS . 'twig')), | |
'template' => ((isset($template)) ? $template : $page->template() . c::get('twig.templates_ext', '.twig')) | |
]; | |
extract($defaults); | |
/**********************************************************************************************************************/ | |
/** | |
* Load twig integration. | |
*/ | |
$loader = new Twig_Loader_Filesystem($templates_dir); | |
$twig = new Twig_Environment($loader, [ | |
'debug' => c::get('debug'), | |
// Disable cache if debug is enabled or if used the kirby cache system. | |
'cache' => ((true === c::get('debug') or true === $kirby->site()->page()->isCachable()) ? false : $kirby->roots()->cache() . DS . 'twig') | |
]); | |
/** | |
* Twig debug enabled from kirby debug variable. | |
*/ | |
if (true == c::get('debug')) { | |
$twig->addExtension(new Twig_Extension_Debug()); | |
} | |
/**********************************************************************************************************************/ | |
/** | |
* kirbytext | filter. | |
*/ | |
$twig->addFilter(new Twig_SimpleFilter('kirbytext', function($string) { | |
return kirbytext($string); | |
}, ['is_safe' => ['html']])); | |
/** | |
* kt | filter. | |
*/ | |
$twig->addFilter(new Twig_SimpleFilter('kt', function($string) { | |
return kirbytext($string); | |
}, ['is_safe' => ['html']])); | |
/**********************************************************************************************************************/ | |
/** | |
* url() function. | |
*/ | |
$twig->addFunction(new Twig_SimpleFunction('url', function($route) { | |
return url($route); | |
})); | |
/** | |
* css() function. | |
*/ | |
$twig->addFunction(new Twig_SimpleFunction('css', function($css) { | |
return css($css); | |
}, ['is_safe' => ['html']])); | |
/** | |
* js() function. | |
*/ | |
$twig->addFunction(new Twig_SimpleFunction('js', function($css) { | |
return js($css); | |
}, ['is_safe' => ['html']])); | |
/** | |
* dump() function. | |
*/ | |
$twig->addFunction(new Twig_SimpleFunction('dump', function($var) { | |
return dump($var); | |
}, ['is_safe' => ['html']])); | |
/**********************************************************************************************************************/ | |
/** | |
* Add snippet data to template. | |
*/ | |
if (isset($data)) { | |
Tpl::set($data); | |
} | |
/** | |
* Default template data. | |
*/ | |
$template_vars = Tpl::get(); | |
/** | |
* Render template with default variables. | |
*/ | |
echo $twig->render($template, $template_vars); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you could do
require_once
instead ofrequire
.