Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private function loadTemplates()
- {
- $di = new DirectoryIterator(TEMPLATES_PATH);
- foreach ($di as $location)
- {
- // skip all system files
- if (preg_match('/^(\.\w+|\.$|\.\.$)/i', $location) === 0)
- {
- // trim the x_ from x_folderName
- preg_match('/[0-9]+-(\S+)/', $location, $matches);
- $name = $matches[1];
- $path = TEMPLATES_PATH . '/' . $location . '/' . $name;
- $json = file_get_contents($path . '.json');
- $template = file_get_contents($path . '.php');
- $data = json_decode($json, true);
- $cachedCompiledFile = 'cache/compiled-' . $name . '.php';
- $cachedRenderedFile = 'cache/rendered-' . $name . '.html';
- if( $this->loadTemplatesFromCache && file_exists($cachedRenderedFile) )
- {
- $template = file_get_contents($cachedRenderedFile);
- }
- else
- {
- $compiledTemplate = LightnCandy::compile($template, Array(
- 'flags' => LightnCandy::FLAG_HANDLEBARS,
- 'helpers' => Array(
- 'parse_paragraph' => function ($text) {
- return preg_replace('/(\n\n|\r|\r\n)/mi', '</p><p>', $text);
- },
- 'parse_newlines' => function ($text) {
- return preg_replace('/(\r\n|\n|\r)/m', '<br>', $text);
- },
- 'gettext' => function ($text) {
- return gettext($text[0]);
- },
- 'svg' => function ($text) {
- return file_get_contents($text[0]);
- },
- 'random_background_position' => function () {
- $pos1 = rand(0, 100) . '%';
- $pos2 = rand(0, 100) . '%';
- return 'background-position: ' . $pos1 . ' ' . $pos2 . ';';
- }
- )
- ));
- // save compiled to file
- file_put_contents($cachedCompiledFile, $compiledTemplate);
- // render template and save this to file
- $renderer = LightnCandy::prepare($compiledTemplate);
- $template = $renderer($data);
- file_put_contents($cachedRenderedFile, $template);
- }
- array_push($this->templates, array(
- 'id' => $name,
- 'template' => $template,
- 'data' => $data,
- 'json' => $json
- ));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement