A tumblelog CMS built on AJAX, PHP and MySQL.

themes.class.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. Class name: Themes
  4. Class autor: Victor De la Rocha http//mis-algoritmos.com/themes-class
  5. Email: vyk2rr [at] gmail [dot] com
  6. */
  7. class themes{
  8. var $registry;
  9. var $path;
  10. var $l10n;
  11. var $output;
  12. var $vars=array(); //variable para apilar las variables que se asignan a la plantilla
  13. function themes(){
  14. }
  15. function set($name, $value){
  16. $this->vars[$name] = $value;
  17. return true;
  18. }
  19. function remove($name) {
  20. unset($this->vars[$name]);
  21. return true;
  22. }
  23. //obtiene el contenido del tema ya con todos los valores sutituidos en las variables.
  24. function fetch($file){
  25. $this->exec($file);
  26. return $this->output;
  27. }
  28. //muestra el contenido del tema ya con todos los valroes sustituidos en las variables.
  29. function display($file){
  30. $this->exec($file);
  31. echo $this->output;
  32. }
  33. //corre el proceso de sustitucion de valores en las variables del theme y retorna todo en la variable $this->output para ser devuelto por: fetch o display
  34. function exec($file){
  35. $this->file = $file;
  36. $output = file_get_contents($file);
  37. $this->output = $output;
  38. $this->registrar_vars();
  39. $this->__();
  40. $this->eval_control_structures();
  41. //evaluate All as PHP code
  42. ob_start();eval($this->output);
  43. $this->output = stripslashes(ob_get_clean());
  44. }
  45. function eval_control_structures(){
  46. //finding IFs sentences and converting to php code
  47. $this->output = "echo \"".addslashes($this->output)."\";";
  48. $this->output = preg_replace_callback("/{if ([^}]+)}/",create_function('$arr','return "\";if(".stripslashes($arr[1])."){echo\"";'),$this->output);
  49. $this->output = preg_replace("/{else}/","\";}else{echo\"",$this->output);
  50. $this->output = preg_replace_callback("/{elseif ([^}]+)}/",create_function('$arr','return "\";}elseif(".stripslashes($arr[1])."){echo\"";'),$this->output);
  51. $this->output = preg_replace("/{\/if}/","\";} echo \"",$this->output);
  52. //finding FOREACHs or BLOCKs sentences and converting to php code
  53. $this->output = preg_replace_callback("/{block ([^}]+) as ([^}]+)=>[\$]([^}]+)}/",create_function('$arr','return "\";foreach(".stripslashes($arr[1])." as ".stripslashes($arr[2])."=>\$this->vars[\'".stripslashes($arr[3])."\']){echo\"";'),$this->output);
  54. $this->output = preg_replace_callback("/{block ([^}]+) as ([^}]+)}/",create_function('$arr','return "\";foreach(".stripslashes($arr[1])." as ".stripslashes($arr[2])."){echo\"";'),$this->output);
  55. $this->output = preg_replace("/{\/block}/","\";} echo \"",$this->output);
  56. //Converting the $this->vars[\'variable\'] format to {$this->vars['variable']}
  57. $this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']['$2']}",$this->output);
  58. $this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']}",$this->output);
  59. }
  60. //sustituye las variables en el output del template
  61. function registrar_vars(){
  62. foreach($this->vars as $k=>$v){
  63. //pre($this->vars);
  64. if(is_array($v)){
  65. //Si es un arreglo, se intenta procesar un nivel mas adentro para sustituir en el theme por lo que tenga: nombredearreglo.dato
  66. foreach($v as $_k=>$_v)
  67. if(!is_array($_v))
  68. $this->output = str_replace('{'.$k.'.'.$_k.'}',$_v,$this->output);
  69. }else{
  70. // sustituimos directamente las variables {$variable}
  71. $this->output = str_replace('{'.$k.'}',$v,$this->output);
  72. }
  73. }
  74. //replacing {$key} format by $this->vars['key']
  75. //replacing {$array.key} format by $this->vars['array']['key']
  76. $patrones = array(
  77. #'/{\$([^ \.}]+)}/s',
  78. #'/{\$([^ \.}]+)\.([^ \.}]+)}/s'
  79. '/{\$(\w+)}/s',
  80. '/{\$(\w+)\.(\w+)}/s',
  81. '/\$(\W)/s'
  82. );
  83. $reemplazos = array(
  84. "\$this->vars['$1']",
  85. "\$this->vars['$1']['$2']",
  86. "\\\\$$1"
  87. );
  88. $this->output = preg_replace($patrones, $reemplazos, $this->output);
  89. }
  90. //Utiliza gettext
  91. function __(){
  92. $patron = "/{__\((?:'|\")([^\)]+?)(?:'|\")\)}/s";
  93. preg_match_all($patron,$this->output,$out);
  94. foreach($out[1] as $k=>$v){
  95. $this->output = preg_replace("/{__\((?:'|\")$v(?:'|\")\)}/",__($v),$this->output);
  96. }
  97. }
  98. }
  99. ?>