Writing a Template System in PHP - Replacing Tags
(Page 6 of 9 )
Next came time to write the workhorse method of the class, the replace_tags method.
<?php function replace_tags($tags = array()) { if (sizeof($tags) > 0) foreach ($tags as $tag => $data) { $data = (file_exists($data)) ? join("", file($data)) : $data; $this->page = eregi_replace("{" . $tag . "}", $data, $this->page); } else die("No tags designated for replacement."); } ?> |
The method accepts an incoming array of references to replacement data keyed by place holder. A foreach statement cycles through the array.
With each iteration of the foreach, a check is made to see if the value is a filename. If it is then the file's contents is loaded as the replacement data; if it isn't then it's assumed the data was passed as straight text. This allows the method to be a bit more flexible for the end user.
Once it's determined what the replacement data is, the placeholders (along with it's delimiters) are swapped out with the corresponding information. While ereg_replace might be more appropriate, I chose to allow for case insensitivity again as a convenience factor.
Next: Including Dynamic Files >>
More Display Tutorials Articles
More By bluephoenix
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|