Advertisement
Guest User

Untitled

a guest
Jun 19th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. private function loadTemplates()
  2. {
  3. $di = new DirectoryIterator(TEMPLATES_PATH);
  4.  
  5. foreach ($di as $location)
  6. {
  7. // skip all system files
  8. if (preg_match('/^(\.\w+|\.$|\.\.$)/i', $location) === 0)
  9. {
  10. // trim the x_ from x_folderName
  11. preg_match('/[0-9]+-(\S+)/', $location, $matches);
  12. $name = $matches[1];
  13.  
  14. $path = TEMPLATES_PATH . '/' . $location . '/' . $name;
  15.  
  16. $json = file_get_contents($path . '.json');
  17. $template = file_get_contents($path . '.php');
  18.  
  19. $data = json_decode($json, true);
  20.  
  21. $cachedCompiledFile = 'cache/compiled-' . $name . '.php';
  22. $cachedRenderedFile = 'cache/rendered-' . $name . '.html';
  23.  
  24. if( $this->loadTemplatesFromCache && file_exists($cachedRenderedFile) )
  25. {
  26. $template = file_get_contents($cachedRenderedFile);
  27. }
  28. else
  29. {
  30. $compiledTemplate = LightnCandy::compile($template, Array(
  31. 'flags' => LightnCandy::FLAG_HANDLEBARS,
  32. 'helpers' => Array(
  33. 'parse_paragraph' => function ($text) {
  34. return preg_replace('/(\n\n|\r|\r\n)/mi', '</p><p>', $text);
  35. },
  36. 'parse_newlines' => function ($text) {
  37. return preg_replace('/(\r\n|\n|\r)/m', '<br>', $text);
  38. },
  39. 'gettext' => function ($text) {
  40. return gettext($text[0]);
  41. },
  42. 'svg' => function ($text) {
  43. return file_get_contents($text[0]);
  44. },
  45. 'random_background_position' => function () {
  46. $pos1 = rand(0, 100) . '%';
  47. $pos2 = rand(0, 100) . '%';
  48. return 'background-position: ' . $pos1 . ' ' . $pos2 . ';';
  49. }
  50. )
  51. ));
  52.  
  53. // save compiled to file
  54. file_put_contents($cachedCompiledFile, $compiledTemplate);
  55.  
  56. // render template and save this to file
  57. $renderer = LightnCandy::prepare($compiledTemplate);
  58. $template = $renderer($data);
  59. file_put_contents($cachedRenderedFile, $template);
  60. }
  61.  
  62. array_push($this->templates, array(
  63. 'id' => $name,
  64. 'template' => $template,
  65. 'data' => $data,
  66. 'json' => $json
  67. ));
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement