fork download
  1. <?php
  2.  
  3. /**
  4.  * @author Jan Kudlacek
  5.  *
  6. */
  7.  
  8. class Themizer
  9. {
  10.  
  11. var $template;
  12. var $template_path = "blocks/";
  13. var $suffix = ".html";
  14.  
  15. var $tag_pattern = '!\{\{(\w+)\}\}!';
  16.  
  17. var $replace_values;
  18.  
  19. function __construct($theme,$vars = null)
  20. {
  21.  
  22. $file_path = $this->template_path.$theme.$this->suffix;
  23.  
  24. if(file_exists($file_path))
  25. {
  26. $this->template = file_get_contents($file_path);
  27.  
  28. $this->replace_values = $vars;
  29. }
  30. else
  31. throw new Exception("template file doesn't exists");
  32. }
  33.  
  34. function isMultipleResult()
  35. {
  36. return @is_array($this->replace_values[0]);
  37. }
  38.  
  39. function getResult()
  40. {
  41.  
  42. $result="";
  43.  
  44. if($this->isMultipleResult())
  45. {
  46.  
  47. $array = $this->replace_values;
  48.  
  49. foreach($array as $value)
  50. {
  51. $this->replace_values = $value;
  52.  
  53. $result .= preg_replace_callback($this->tag_pattern, array($this,'replace_value'), $this->template);
  54. }
  55.  
  56. }
  57. else
  58. $result = preg_replace_callback($this->tag_pattern, array($this,'replace_value'), $this->template);
  59.  
  60. return $result;
  61. }
  62.  
  63. function replace_value($matches) {
  64. if(array_key_exists($matches[1],$this->replace_values))
  65. return $this->replace_values[$matches[1]];
  66. else
  67. return "";
  68. }
  69.  
  70. function getResultFromArray($keys)
  71. {
  72. $args = func_get_args();
  73.  
  74. unset($args[0]);
  75. $args = array_values($args);
  76.  
  77.  
  78. $biggest = max($args);
  79.  
  80.  
  81.  
  82. $i = 0; $result= array();
  83. foreach($biggest as $value)
  84. {
  85.  
  86. $result[$i] = array();
  87.  
  88. foreach($keys as $key => $val)
  89. {
  90. $result[$i][$val] = $args[$key][$i];
  91. }
  92. $i++;
  93. }
  94. $this->replace_values = $result;
  95. return $this->getResult();
  96. }
  97.  
  98.  
  99. }
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Standard output is empty