dezkit Posted September 20, 2010 Share Posted September 20, 2010 Hey guys first of all I got 3 files - index.php require_once('/libs/template.class.php'); $template = new Template; $template->assign("title","test"); template.php <?php echo $title; ?> template.class.php function assign($var, $val) { $var = $val; } I'm trying to assign a variable in index.php, then recall it in the template.php, how would I go about this? Thank you! Link to comment https://forums.phpfreaks.com/topic/213947-trying-to-make-a-simple-template-system/ Share on other sites More sharing options...
dezkit Posted September 20, 2010 Author Share Posted September 20, 2010 Oh, and another thing, why is it that whenever I have this: index.php <?php $var = "test"; include("test.php"); ?> test.php <?php echo $var; ?> nothing comes up in index.php, why..? Link to comment https://forums.phpfreaks.com/topic/213947-trying-to-make-a-simple-template-system/#findComment-1113474 Share on other sites More sharing options...
vungee Posted September 21, 2010 Share Posted September 21, 2010 Why use php in your template file? I mean, how does that differ from your index.php code? Stripping PHP out of your template file can nicely separate your HTML code from your PHP code. Below is a rough outline of a useable token-replacement template class, which parses an html file. class.template.php class Template { public $template; public function __construct($template) { $this->template = file_get_contents($template); } public function assign($replacement, $new_content) { $this->template = str_replace("{$replacement}", $new_content, $this->template); } public function get_template() { return $this->template; } } index.php require_once('class.template.php'); $template = new Template('template.html'); $template->assign('{ReplaceThisText}', 'WithThisText'); echo $template->get_template(); template.html <html> <body> {ReplaceThisText} </body> </html> This is not meant to be used as is, but give you an idea of how to expand this further. Link to comment https://forums.phpfreaks.com/topic/213947-trying-to-make-a-simple-template-system/#findComment-1113500 Share on other sites More sharing options...
dezkit Posted September 21, 2010 Author Share Posted September 21, 2010 Well, what i wanted to do is have two templates for all pages, I want to have php inside the template to have full potential of php. But thanks for the reply though! I still am trying to figure out why the second post doesn't work... Link to comment https://forums.phpfreaks.com/topic/213947-trying-to-make-a-simple-template-system/#findComment-1113503 Share on other sites More sharing options...
dezkit Posted September 21, 2010 Author Share Posted September 21, 2010 bump Link to comment https://forums.phpfreaks.com/topic/213947-trying-to-make-a-simple-template-system/#findComment-1113816 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.