-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmfwTwigTemplate.php
116 lines (99 loc) · 2.39 KB
/
mfwTwigTemplate.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
require_once __DIR__.'/vendor/Twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
/**
* Customized Twig_Environment.
* - blockとtemplateのディレクトリを分離
* - テンプレート名にディレクトリセパレータも許容
* - cacheへの事前変換必須
*/
class mfwTwigEnv extends Twig_Environment {
const BLOCKMARK = 'block:';
protected $blockClassPrefix = '__TwigBlock_';
protected $blockdir;
protected $templatedir;
public function __construct(Twig_LoaderInterface $loader = null, $options = array())
{
if(!$loader){
$loader = new Twig_Loader_String();
}
parent::__construct($loader,$options);
$this->blockdir = APP_ROOT.'/data/blocks';
$this->templatedir = APP_ROOT.'/data/templates';
}
public function setBaseDir($dir)
{
$dir = rtrim($dir,'/');
$t = "{$dir}/templates";
$b = "{$dir}/blocks";
if(!is_dir($t)||!is_dir($b)){
return false;
}
$this->blockdir = $b;
$this->templatedir = $t;
return true;
}
protected function isBlock($name){
return strpos($name,self::BLOCKMARK)===0;
}
protected function blockName($name){
return substr($name,strlen(self::BLOCKMARK));
}
/**
* @override
*/
public function getTemplateClass($name){
$prefix = $this->templateClassPrefix;
if($this->isBlock($name)){
$prefix = $this->blockClassPrefix;
$name = $this->blockName($name);
}
return $prefix.strtr($name,'/','_');
}
/**
* @override
*/
public function getCacheFilename($name)
{
if($this->isBlock($name)){
$name = $this->blockName($name);
return "{$this->blockdir}/{$name}.php";
}
return "{$this->templatedir}/{$name}.php";
}
/**
* @override
*/
public function loadTemplate($name)
{
$cls = $this->getTemplateClass($name);
if(isset($this->loadedTemplates[$cls])){
return $this->loadedTemplates[$cls];
}
require_once $this->getCacheFilename($name);
if(!$this->runtimeInitialized){
$this->initRuntime();
}
return $this->loadedTemplates[$cls] = new $cls($this);
}
};
/**
* Twigを使ったテンプレートエンジン.
*/
class mfwTwigTemplate {
protected $twig;
protected $name;
public function __construct($name,$basedir=null)
{
if(!$basedir){
$basedir = APP_ROOT.'/data';
}
$this->twig = new mfwTwigEnv();
$this->twig->setBaseDir($basedir);
$this->name = $name;
}
public function build($params=array())
{
return $this->twig->render($this->name,$params);
}
}