Results 1 to 10 of 10
Thread: my own template engine
-
my own template engine
24 May 2011 @ 08.11 Using a tutorial I have created my own template engine as follows
template.php
index.phpPHP Code:<?php
class Digitalp{
public $output;
public $file;
public $values = array();
function __construct($file){
$this->file = $file;
$this->output = file_get_contents($this->file);
}
function set($key, $value){
$this->values[$key] = $value;
}
function output(){
foreach($this->values as $key => $value){
$tagsToReplace = "[$key]";
$this->output = str_replace($tagsToReplace, $value, $this->output);
}
return $this->output;
}
}
?>
defult.tplPHP Code:<?php
require("template.php");
$dig = new Digitalp('defult.tpl' , 'form.tpl');
$dig->set ('title','my site title');
$dig->set ('form', 'include(form.php)');
echo $dig->output();
?>
I have a question,HTML Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> [title] <hr /> [form] <body> </body> </html>
as you can see in the index.php I am trying to call an external file and assign it the name 'form', this does not seem to work as obviously I want to position it where [form] is in the defult.php.. does anyone know how to make it work ?
thanks
-
24 May 2011 @ 10.13 You would need to write another function in your template.php
Something like the below after the set function:
Then call the include as:Code:function set($key, $value){ $this->values[$key] = $value; } function get_file($key, $file){ ob_start(); include($file); $contents = ob_get_contents(); ob_end_clean(); $this->values[$key] = $contents; }
Hope this works?Code:$dig->get_file('form', "form.php");
Cheers, MattLast edited by Frinkky; 24 May 2011 at @ 11.20. Reason: Forced signature removed. Please read the forum rules on signatures: http://bit.ly/leQSJI
-
24 May 2011 @ 10.49 no! no! no!
why can't form.php just contain a function which parses another template (containing the form HTML) and return this HTML?
form.php
index.phpPHP Code:function get_form_html() {
// create form html
return $html;
}
PHP Code:include('form.php');
$dig->set ('form', get_form_html());
-
24 May 2011 @ 11.01 I'm a little concerned about your no, no, no comment?
Nothing wrong with my solution and it is reusable - and answers the question as asked?
Not saying there is anything wrong with your solution.
Just wondering why you think there is something wrong with my solution?Last edited by Frinkky; 24 May 2011 at @ 11.18. Reason: Forced signature removed. Please read the forum rules on signatures: http://bit.ly/leQSJI
-
24 May 2011 @ 11.29 Sorry, should have explained....
Yes, it works, but it really is quite bad practice for a number of reasons:
- breaks the normal line of execution which makes the code very hard to debug.
- kills all chances of error handling.
- parameters would have to be global (you can't pass them normally).
- same with return parameters.
- code readability.
-
24 May 2011 @ 11.39 Thank you that did it, my only issue now would be passing variables from the function to the template, so for example if I had something like
it would pass the $forumsubmit over to default.php if $forumsubmit = myform.php for examplePHP Code:<?php function get_form_html() {
$html = '<form action=$formsubmit method="get"><input name="" type="text" /><textarea name="" cols="" rows=""></textarea></form>';
return $html;
}
?>
-
24 May 2011 @ 12.05 if you're using a template engine, then why have any HTML in your PHP at all?
form.tplPHP Code:<?php function get_form_html($formsubmit) { // add parameters which you need from index.php
$dig = new Digitalp('form.tpl');
$dig->set ('formsubmit',$formsubmit);
return $dig->output();
}
?>
Code:<form action=[formsubmit] method="get"><input name="" type="text" /><textarea name="" cols="" rows=""></textarea></form>
-
24 May 2011 @ 12.53 I dont know if you have ever messed with phpbb, but that forum software uses or used {COMMAND} as its templating system ( I was using phpbb2 years ago ) each {COMMAND} called individual parts of the site template elements, {CONTACT_FORM}, {IMAGE_GALLERY} , {LATEST_POSTS} etc etc, all call every piece of those elements that minimizes the amount of stuff you have to learn in order to create a custom forum template in that case, I am attempting the same only with a cms to make it simpler to create templates for it, replacing things like [content] and [twitter_link] etc all controlled from the admin panel, but hopefully making it also possible for users to be able to dynamically create custom [calls] for their templates too by defining html with pre-made [ commands ] to utilise the built in php of my cms ( like an email form ).
-
24 May 2011 @ 13.04 I have no experience developing templates for phpbb, but the concept sounds like a good.
-
6 Jun 2011 @ 13.47 Hows your CMS coming along?
Similar Threads
-
New Domain, New Template Design..
By saltedm8 in forum Design & LayoutReplies: 10Last Post: 10 Jan 2011, @ 09.56 -
How to design template?
By lizahellen in forum Design & LayoutReplies: 2Last Post: 26 Oct 2010, @ 09.16 -
How to design template?
By sellinageorge in forum Design & LayoutReplies: 6Last Post: 28 Sep 2010, @ 15.17 -
using a css file with smarty template
By sudhakararaog in forum PHP, ASP & JavaReplies: 0Last Post: 21 Oct 2009, @ 03.40 -
Corporate Site Template
By aazad in forum Free Reviews & ShowcaseReplies: 17Last Post: 6 Apr 2009, @ 17.03



LinkBack URL
About LinkBacks











Hello every one
Hi, I am new on this forum and saying hello.