|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today! |
|
#1
|
|||
|
|||
|
Writing a Template System in PHP - Help Please
I was a little confused when reading the tutorial because I am new to the whole templating thing. Anyway, below I will have the error and code. Thanks in advance to anyone who helps or tries to help!
--- Error --- Fatal error: Cannot instantiate non-existent class: page in /home/whomikey/public_html/template system/index.tpl.php on line 4 --- index.tpl.php/template.tpl --- <? require_once('lib/template.php'); $page = new Page('std.tpl'); $page->replace_tags(array( 'title' => 'title', 'user_bar' => 'dat/user_bar.dat', 'navigation' => 'dat/navigation.dat', 'main' => 'dat/index.dat', 'affiliates' => 'dat/affiliates.dat',)); $page->output(); ?> |
|
#2
|
|||
|
|||
|
RE: Writing a Template System in PHP - Help Please
it almost looks like its not finding the class functions.
|
|
#3
|
|||
|
|||
|
RE: Writing a Template System in PHP - Help Please
I have the code for std.tpl which has all the functions if you need to see that. I will post it below to save time. It's the same as the one that was in the tutorial because I didn't want to change around or add anything untill I get it working from the way it was originally.
--- std.tpl --- <? class Page { var $page; function Page($template = 'std.tpl') { if (file_exists($template)) $this->page = join('', file($template)); else die("Template file $template not found."); } function parse($file) { ob_start(); include($file); $buffer = ob_get_contents(); ob_end_clean(); return $buffer; } function replace_tags($tags = array()) { if (sizeof($tags) > 0) foreach ($tags as $tag => $data) { $data = (file_exists($data)) ? $this->parse($data) : $data; $this->page = eregi_replace('{' . $tag . '}', $data, $this->page); } else die('No tags designated for replacement.'); } function output() { print($this->page); } } ?> |
|
#4
|
|||
|
|||
|
RE: Writing a Template System in PHP - Help Please
The tpl files are for your html templates. You need to put those functions in a php file and require_once that php file you put them in.
|
|
#5
|
|||
|
|||
|
RE: Writing a Template System in PHP - Help Please
I am a little confused with what you just said, lol. Would that mean std.tpl = std.php and index.tpl.php = index.php? If you can explain it a little more to me. Thanks!
|
|
#6
|
|||||
|
|||||
|
RE: Writing a Template System in PHP - Help Please
put the Page class in a separate file. Call it template.php
Now, use this code: php Code:
That will take std.tpl, and replace all of the tags in it that you want it to from above, and your Page class resides in template.php |
|
#7
|
|||
|
|||
|
RE: Writing a Template System in PHP - Help Please
Hehe, I tried to do it but I know I did it wrong because I am still confused and there was an error. When you get time it would be cool if you could checkout my code I will post it for each page below. Thanks for the help so far!
--- template.php --- <? class Page { var $page; function Page($template = 'std.tpl') { if (file_exists($template)) $this->page = join('', file($template)); else die("Template file $template not found."); } } ?> --- template.tpl --- <? require_once('template.php'); $page = new Page('std.tpl'); $page->replace_tags(array( 'title' => 'title', 'user_bar' => 'dat/user_bar.dat', 'navigation' => 'dat/navigation.dat', 'main' => 'dat/index.dat', 'affiliates' => 'dat/affiliates.dat',)); $page->output(); ?> --- std.tpl --- <? function parse($file) { ob_start(); include($file); $buffer = ob_get_contents(); ob_end_clean(); return $buffer; } function replace_tags($tags = array()) { if (sizeof($tags) > 0) foreach ($tags as $tag => $data) { $data = (file_exists($data)) ? $this->parse($data) : $data; $this->page = eregi_replace('{' . $tag . '}', $data, $this->page); } else die('No tags designated for replacement.'); } function output() { print($this->page); } ?> |
|
#8
|
|||
|
|||
|
RE: Writing a Template System in PHP - Help Please
*doh* lol....
The idea of the object oriented style is for the templating functions to be in the same class..... template.php: |
|
#9
|
|||
|
|||
|
RE: Writing a Template System in PHP - Help Please
I got it working finally! Thanks a lot for helping me.
|
![]() |
| Viewing: Codewalkers Forums > Other > Tutorials > Writing a Template System in PHP - Help Please |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|