Posted By

Piotrek290 on 09/06/09


Tagged

php template


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Piotrek290


Template System


 / Published in: JavaScript
 

  1. // Author: Piotr Filipek
  2. // Site: www.dealis.pl
  3. // Version: 1.9.2
  4.  
  5. class Template {
  6. private $toChange;
  7. private $str;
  8.  
  9. public function Template($AFileName){
  10. $this->str = file_get_contents($AFileName);
  11. }
  12.  
  13. public function set($AKey, $AValue=''){
  14. if(is_array($AKey)){
  15. foreach($AKey as $BValue => $BKey){
  16. echo $BValue." => ".$BKey."<br />";
  17. $CKey = '{'.$BValue.'}';
  18. $this->toChange[$CKey] = $BKey;
  19. }
  20. } else {
  21. $AKey = '{'.$AKey.'}';
  22. $this->toChange[$AKey] = $AValue;
  23. }
  24. }
  25.  
  26. public function display(){
  27. if (count($this->toChange)>0){
  28. $tmpKeys = array_keys($this->toChange);
  29.  
  30. foreach($tmpKeys as $currentKey){
  31. $this->str = str_replace($currentKey, $this->toChange[$currentKey], $this->str);
  32. }
  33. }
  34. echo $this->str;
  35. }
  36. }
  37.  
  38.  
  39. // SIMPLE CODE - FIRST
  40. include 'includes/class.template.php';
  41.  
  42. $tpl = new Template("themes/default/szablon.html");
  43. $tpl->set("SITE:TITLE", "Simple title");
  44. $tpl->set("SITE:COPY", "My footer");
  45.  
  46. $tpl->display();
  47.  
  48. // SIMPLE CODE - SECOND
  49. include 'includes/class.template.php';
  50.  
  51. $tpl = new Template("themes/default/szablon.html");
  52. $tpl->set(
  53. array(
  54. "SITE:TITLE" => "Simple title",
  55. "SITE:COPY" => "My footer"
  56. )
  57. );
  58.  
  59. $tpl->display();

Report this snippet  

You need to login to post a comment.