-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
88 lines (71 loc) · 2.57 KB
/
lib.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
<?php
// Written at Louisiana State University
abstract class quick_template {
public static function render($template, $data= array(), $module='moodle', $custom_funs = array(), $output = true) {
global $CFG;
require_once(dirname(__FILE__) . '/smarty3/Smarty.class.php');
$path = isset($CFG->smartycachedir) ?
$CFG->smartycachedir :
"$CFG->dataroot/templates_c";
if (!is_dir($path)) {
mkdir($path);
}
$smarty = new Smarty();
$smarty->compile_dir = $path;
foreach ($data as $key => $value) {
$smarty->assign($key, $value);
}
// Lang string modifier
$lang = function ($var) use ($module) {
if (strpos($var, ":")) {
list($name, $use_module) = explode(":", $var);
} else {
$name = $var;
$use_module = $module;
}
return get_string($name, $use_module);
};
// Set the moodle string function
if (isset($custom_funs['modifier'])) {
$custom_funs['modifier']['s'] = $lang;
} else {
$custom_funs['modifier'] = array('s' => $lang);
}
foreach ($custom_funs as $key => $funs) {
$smarty_partial = self::map($key, $smarty);
foreach ($funs as $ident => $fun) {
// Check for closure objects
if (method_exists($fun, '__invoke')) {
$smarty_partial($ident, array($fun, '__invoke'));
} else {
$smarty_partial($ident, $fun);
}
}
}
if ($output) {
$smarty->display($template);
} else {
return $smarty->fetch($template);
}
}
private static function map($key, &$smarty) {
$plugin = array('function', 'block', 'compiler', 'modifier');
$filter = array('pre', 'post', 'output', 'variable');
if (in_array($key, $plugin)) {
return function($ident, $fun) use ($key, $smarty) {
$smarty->registerPlugin($key, $ident, $fun);
};
}
if (in_array($key, $filter)) {
return function($ident, $fun) use ($key, $smarty) {
$smarty->registerFilter($key, $ident, $fun);
};
}
if ($key == "object") {
return function($ident, $fun) use ($key, $smarty) {
$smarty->registerObject($ident, $fun);
};
}
throw new Exception("$key is not registerable in Smarty");
}
}