-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.php
48 lines (41 loc) · 1.15 KB
/
utils.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
<?php
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
require_once(ROOT . '/mustache.php');
require_once(ROOT . '/markdown.php');
$mustache = new Mustache;
// $mustache->addHelper('_markdown', function($text) {
// return Markdown($text);
// });
$cache = array();
function convertMarkdown(&$array) {
// Credit: http://www.php.net/manual/en/function.array-walk.php#71901
// var_dump($array);
foreach ($array as $key => $value) {
if (is_array($value)) {
$res[$key] = convertMarkdown($value);
} else {
$a = explode('_', $key);
if (array_pop($a) == 'md') {
$res[$key] = Markdown($value);
} else {
$res[$key] = $value;
}
}
}
return $res;
}
function renderTemplate($data, $template) {
global $mustache;
// TODO validate the file exists - and error handle properly
if (!isset($cache[$template])) {
$cache[$template] = file_get_contents(ROOT . $template);
}
if (is_string($data)) {
$data = convertMarkdown(json_decode(file_get_contents(ROOT . $data), true));
} else {
$data = convertMarkdown($data);
}
$render = $mustache->render($cache[$template], $data);
return $render;
}
?>