Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,621 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,911 people online right now. Registration is fast and FREE... Join Now!





PHP Template

Fast and easy Template parser. can use arrays, or not.

Submitted By: SpaceMan
Actions:
Rating:
Views: 1,338

Language: PHP

Last Modified: January 7, 2007
Instructions: will not do multidementional arrays, you will have to pull data into a single array then all it.
can do mutiple tag types.
examples in the comented out header.

Snippet


  1. <?php
  2. /**
  3. wrote by dave husk, easyphpscripts.com
  4. free software, no warantees.
  5. $tags1 = array("TESTa1"=>"Testing once array","TESTa2"=>"<b>testing twice array</b>");
  6. $tags = array("TEST3","TEST4");
  7. $text = array("Testing once 3","testing twice 4");
  8. //echo "<br>test brackets<br>";
  9. $string = "
  10. <br>test string<br>
  11. {TEST1} <b>{TEST2}</b>
  12. <br>
  13. {TIME}";
  14. echo "<br><br>test class<br>";
  15. $temp=new EasyPHPTemplate();
  16. $temp->load("test.tmp",$string);
  17. //$temp->encase("%");
  18. $temp->replace("TIME",date("H-i-s"));
  19. $temp->replace("TEST1","Testing once");
  20. $temp->replace("TEST2","testing twice");
  21. $temp->replace($tags1);
  22. //change the type of template tags.
  23. $temp->encase("[","]");
  24. $temp->replace($tags,$text);
  25. $temp->render();
  26. */
  27. class EasyPHPTemplate {
  28.         public $template;
  29.         private $encased_l = "{";
  30.         private $encased_r = "}";
  31.  
  32.         function load($filepath,$string='') {
  33.             if(is_file($filepath)){
  34.                         $f=fopen($filepath, 'r');
  35.                         $this->template = fread($f, filesize($filepath));
  36.                         fclose($f);
  37.             }
  38.             if($string) {
  39.                 $this->template .= $string;
  40.             }
  41.          
  42.         }
  43.         function encase($var,$val='') {
  44.             if($val != ''){
  45.                 $this->encased_l = $var;
  46.                 $this->encased_r = $val;
  47.             } elseif($val == ''){
  48.                 $this->encased_l = $var;
  49.                 $this->encased_r = $var;
  50.             }
  51.         }
  52.          
  53.         function replace($var,$val='') {
  54.                 if(!is_array($var) && !is_array($val)) {
  55.                     $this->template = str_replace($this->encased_l.$var.$this->encased_r, $val, $this->template);
  56.                  
  57.                 } elseif($val == '' && is_array($var)) {
  58.                     foreach($var as $var => $val)
  59.                         $this->template = str_replace($this->encased_l.$var.$this->encased_r, $val, $this->template);
  60.                  
  61.                 } elseif(is_array($var) && is_array($val)) {
  62.                     foreach($var as $idx => $varable)
  63.                         $this->template = str_replace($this->encased_l.$var[$idx].$this->encased_r, $val[$idx], $this->template);
  64.                 }
  65.         }
  66.         function render() {
  67.             echo("{$this->template}");
  68.             unset($this->template);
  69.         }
  70.  
  71. }
  72.  
  73. ?> 

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month