Simple template Class
A very very simple html template php4
by humancoder 2 years ago and tagged with: php template template-class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
<?php /** A very very simple html template php class (built for php4) Copyright (C) 2006 Alexandru Plugaru ( alexandru.plugaru[guess what is here]info.uaic.ro ) http://students.info.uaic.ro/~alexandru.plugaru This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** * Usage example: * * In php file: * * $obj= new template("template.html", "header.html", "footer.html"); //assinging current template + header + footer * $obj->assign_vars(array("VAR1"=>$content_var1, "VAR2" => $content_var2)); // setting up vars * $obj->assign_templates(array("SOME_HTML_BLOCK" => "other_template.html")); // other templates that should replace descriptors in current template * $obj->parse(); // parsing everything * $html = $obj->fetch(); // fetching the output string * echo $html; * * OR Simply: * * $obj->output(); // echo output string * * In template.html : * * ... content ... * {VAR1} - this will be replaced with $content_var1 * {VAR2} - this will be replaced with $content_var2 * {SOME_HTML_BLOCK} - this will be replaced with contens other_template.html * */ class template{ var $templates_path = TEMPLATES_PATH; //Templates path var $header=HEADER; //Header file (header.html) var $footer=FOOTER; //Footer file (footer.html) var $current_template; var $start="{"; var $end="}"; var $template_string=""; // This variable contains all the HTML that is needed for output var $header_string=""; // This var contains string that will be outputed instead of the header var $header_pattern="{HEADER}"; // This is the header pattern... use it in your templates like {HEADER} var $footer_string=""; // This var contains string that will be outputed instead of the footer var $footer_pattern="{FOOTER}"; // This is the footer pattern... use it in your templates like {FOOTER} var $variables = array(); // Variables in templates var $templates=array(); // Template variables in templates /** * Constructor with current template file(needed), header template(optional) and footer template(optional) as parameters * * @param String $current_template * @param String $header * @param String $footer */ function template($current_template,$header="", $footer=""){ if($header != ""){ $this->header=$header; //Assigning the header file } // Verifing if header file exists if(!file_exists($this->templates_path . $this->header)){ echo "Header file $this->header does not exist"; }else { $this->header_string=$this->read_file($this->templates_path . $this->header); //loading the header } if(!empty($footer)){ $this->footer=$footer;// Assigning the footer file } // Testing if footer exists if(!file_exists($this->templates_path . $this->footer)){ echo "Footer file $this->footer does not exist"; }else { $this->footer_string=$this->read_file($this->templates_path . $this->footer); } // Testing if template file exists if(!file_exists($current_template)){ $this->current_template=$current_template; //setting up the current template filename $this->template_string=$this->read_file($this->templates_path . $this->current_template); //loading the template } } /** * Return string of the readed * * @param String $file filename * @return String */ function read_file($file){ return file_get_contents($file); } /** * Here we are assigning template vars that will be parsed later * Example: * $obj->assign_vars(array("VAR1"=>$content_var1, "VAR2" => $content_var2, etc.)); * In template file you need to have {VAR1} and {VAR2} that will be replaced with $content_var1 and $content_var2 * * @param Array $variables */ function assign_vars($variables){ if(is_array($variables)){ foreach ($variables as $variable => $value){ $this->variables[$this->start . $variable . $this->end]=$value; //Adding start and end strings } }else { echo "Error! Template variables are not an array! "; } } /** * Here we are assigning template files. * Example: * $obj->assign_template(array("SOME_HTML_BLOCK" => "some_template.html")); * In template file you need to have {SOME_HTML_BLOCK} that will be replaced with some_template.html file * Also works with multiple files * * @param Array $templates */ function assign_templates($templates){ if(is_array($templates)){ foreach ($templates as $template_descriptor => $template_file){ $this->templates[$this->start . $template_descriptor . $this->end]=$this->read_file($this->templates_path .$template_file); } }else { echo "Error! Templates variables are not an array! "; } } /** * This method parses templates array and variables array and outputs the contents in a replaced form (html). */ function parse (){ $pattern="/\\$this->start\w+\\$this->end/"; //here we have the perl regexp needed for searching and replacing the contents // this loop replaces all the included templates with the contents of the included template foreach ($this->templates as $template_descriptor => $value){ $this->template_string=str_replace($template_descriptor,$value,$this->template_string); } // Here we will replace the header and the footer in our template $this->template_string=str_replace($this->header_pattern,$this->header_string,$this->template_string); $this->template_string=str_replace($this->footer_pattern,$this->footer_string,$this->template_string); // This loop replaces all the variables defined with $this->assign_vars() foreach ($this->variables as $variable_descriptor => $value){ $this->template_string=str_replace($variable_descriptor,$value,$this->template_string); } } /** * Output via echo */ function output(){ echo $this->template_string; } /** * Returns outputed string * * @return String */ function fetch(){ return $this->template_string; } } ?> |

Currently 0 comments