-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwig_plugin.php
60 lines (48 loc) · 1.64 KB
/
twig_plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
Plugin Name: Twig Plugin
Description: Include Twig Engine for make simple themes
Version: 1.4
Author: Sergio Guastaferro
Author URI: https://www.linkedin.com/in/sergio-guastaferro/
*/
//loading other dependencies
require_once dirname(__FILE__).'/twig_plugin/vendor/autoload.php';
//loading model class of the page
require_once dirname(__FILE__).'/twig_plugin/PageModel.class.php';
$thisfile=basename(__FILE__, ".php");
register_plugin(
$thisfile,
'Twig Plugin',
'1.3',
'Sergio Guastaferro',
'https://www.linkedin.com/in/sergio-guastaferro-5826a171',
'Include Twig Engine for make simple themes',
'',
''
);
// functions
// get an initialized twig instance
function getTwig(){
$dbWebsite = GSDATAOTHERPATH . 'website.xml';
$dataWebsite = getXML($dbWebsite);
$currThemePath = dirname(__FILE__) . '/../theme/' . $dataWebsite->TEMPLATE;
$loader = new \Twig\Loader\FilesystemLoader($currThemePath . '/views');
$twig = new \Twig\Environment($loader);
$getSimpleWrapFunc = new \Twig\TwigFunction("_gs", function ($function_name, array $params = [] ){
return call_user_func_array($function_name, $params);
}, ['is_variadic' => true]);
$twig->addFunction($getSimpleWrapFunc);
return $twig;
}
// get the page-model of the current page, this is the 'context' to pass to twig
function getPageModel(){
return new Labgua\Twig_Plugin\Page();
}
// shortcut to render a specific template ( defined in 'theme/MYTHEME/view/' ) with the current page-model
function render( $template_name ){
$template = getTwig()->loadTemplate( $template_name );
$pageModel = getPageModel();
return $template->render( $pageModel->export() );
}
?>