A tumblelog CMS built on AJAX, PHP and MySQL.

themes.class.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #$this->l10n = l10n::getInstance();
  15. }
  16. function set($name, $value){
  17. $this->vars[$name] = $value;
  18. return true;
  19. }
  20. function remove($name) {
  21. unset($this->vars[$name]);
  22. return true;
  23. }
  24. //obtiene el contenido del tema ya con todos los valores sutituidos en las variables.
  25. function fetch($file){
  26. $this->exec($file);
  27. return $this->output;
  28. }
  29. //muestra el contenido del tema ya con todos los valroes sustituidos en las variables.
  30. function display($file){
  31. $this->exec($file);
  32. echo $this->output;
  33. }
  34. //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
  35. function exec($file){
  36. $this->file = $file;
  37. $output = file_get_contents($file);
  38. $this->output = $output;
  39. $this->registrar_vars();
  40. $this->__();
  41. $this->eval_control_structures();
  42. //evaluate All as PHP code
  43. ob_start();eval($this->output);
  44. $this->output = ob_get_clean();
  45. }
  46. function eval_control_structures(){
  47. //finding IFs sentences and converting to php code
  48. $this->output = "echo \"".addslashes($this->output)."\";";
  49. $this->output = preg_replace_callback("/{if ([^}]+)}/",create_function('$arr','return "\";if(".stripslashes($arr[1])."){echo\"";'),$this->output);
  50. $this->output = preg_replace("/{else}/","\";}else{echo\"",$this->output);
  51. $this->output = preg_replace_callback("/{elseif ([^}]+)}/",create_function('$arr','return "\";}elseif(".stripslashes($arr[1])."){echo\"";'),$this->output);
  52. $this->output = preg_replace("/{\/if}/","\";} echo \"",$this->output);
  53. //finding FOREACHs or BLOCKs sentences and converting to php code
  54. $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);
  55. $this->output = preg_replace_callback("/{block ([^}]+) as ([^}]+)}/",create_function('$arr','return "\";foreach(".stripslashes($arr[1])." as ".stripslashes($arr[2])."){echo\"";'),$this->output);
  56. $this->output = preg_replace("/{\/block}/","\";} echo \"",$this->output);
  57. //Converting the $this->vars[\'variable\'] format to {$this->vars['variable']}
  58. $this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']['$2']}",$this->output);
  59. $this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']}",$this->output);
  60. //Converting the {__(\'word\')} format to {__('word')}
  61. #$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']}",$this->output);
  62. }
  63. //sustituye las variables en el output del template
  64. function registrar_vars(){
  65. foreach($this->vars as $k=>$v){
  66. //pre($this->vars);
  67. if(is_array($v)){
  68. //Si es un arreglo, se intenta procesar un nivel mas adentro para sustituir en el theme por lo que tenga: nombredearreglo.dato
  69. foreach($v as $_k=>$_v)
  70. if(!is_array($_v))
  71. $this->output = str_replace('{'.$k.'.'.$_k.'}',$_v,$this->output);
  72. }else{
  73. // sustituimos directamente las variables {$variable}
  74. $this->output = str_replace('{'.$k.'}',$v,$this->output);
  75. }
  76. }
  77. //replacing {$key} format by $this->vars['key']
  78. //replacing {$array.key} format by $this->vars['array']['key']
  79. $patrones = array(
  80. '/{\$([^ \.}]+)}/s',
  81. '/{\$([^ \.}]+)\.([^ \.}]+)}/s'
  82. );
  83. $reemplazos = array(
  84. "\$this->vars['$1']",
  85. "\$this->vars['$1']['$2']"
  86. );
  87. $this->output = preg_replace($patrones, $reemplazos, $this->output);
  88. }
  89. //Utiliza gettext
  90. function __(){
  91. $patron = "/{__\((?:'|\")([^\)]+?)(?:'|\")\)}/s";
  92. preg_match_all($patron,$this->output,$out);
  93. foreach($out[1] as $k=>$v){
  94. $this->output = preg_replace("/{__\((?:'|\")$v(?:'|\")\)}/",__($v),$this->output);
  95. }
  96. }
  97. }
  98. ?>