Welcome to Gaia! ::

My Design :: Web and Graphic Design [HTML CSS PHP]

Back to Guilds

XHTML, CSS, jQuery, Javascript, PHP, mySQL, MORE! 

Tags: design, graphics, html, jquery, javascript 

Reply Server Side Programming
Template system :3

Quick Reply

Enter both words below, separated by a space:

Can't read the text? Click here

Submit

Shadow in the dark
Crew

PostPosted: Sun Aug 20, 2006 9:37 am


This si a small script that's been really helpfulto me with making my site and not having to go and recode everything.

templatesys.php

class template {
var $output = NULL;
function parseFile( $file ) {
if ( file_exists( $file ) ) {
$file = $file;
} else {
$file = "error.tpl";
}

return file_get_contents($file)
}

function parseTemplate( $templ, $vars ) {
$file = $this->parseFile($templ)

foreach ($vars as $variable => $data) {
$variable = '{' . $variable . '}';
$file = str_replace($variable, $data, $file)
}

$this->output = $file;
}

function display() {
print $this->output;
}
}

?>


How to call it:


include("templatesys.php")

//Any other code goes here

$content = 'This is content';
$news = 'Some news';

$data = array('CONTENT' => $content,
'NEWS' => $news)

$tpl = new template;
$tpl->parseTemplate('templates/page.tpl', $data)
$tpl->display()

?>


Then on the .tpl file (note i doesn't need to be a .tpl)


My page



(CONTENT}








{NEWS}



Enjoy. smile
PostPosted: Sun Aug 20, 2006 9:10 pm


You use "$ouput" at the top and "$output" later on. razz

Peng


Shadow in the dark
Crew

PostPosted: Sun Aug 20, 2006 9:23 pm


Peng
You use "$ouput" at the top and "$output" later on. razz

Fixeded :XP:
PostPosted: Mon Aug 21, 2006 10:08 pm


Thanks, those help me so much... so i can build my website separately with my coder.

Any tricks?

deniar


MiniLain

PostPosted: Fri Oct 06, 2006 9:36 pm


If your looking to do any amount to template work your going to need a bit more robust engine. While this will work for simple variable replacement, it has no looping or conditional support. I always recommend PHPs Smarty Template Engine User Image - Blocked by "Display Image" Settings. Click to show., its fast and well documented (in typical php fashion).
PostPosted: Sun Oct 08, 2006 1:21 pm


Minilain: Actually I have used it to loop before. You just need to play around with it.

Shadow in the dark
Crew


Aesto

PostPosted: Wed Oct 18, 2006 2:14 am


Why do you actualy 1- complicate things 2- slow your script ?

Php itself can be very template-friendly if you tweak your things up smile

ex.:


the function:
function parse($file, $data)
{
extract($data)
require ($file)
}



the main file (the controler):

$data['title'] = 'this is my title!';
while($d = mysql_fetch_array($res))
{
$dat[] = $d;
}
$data['news'] = $dat;
parse('my_view.php', $data)



the view:


<?=$title?>


The title:
The news body:



4x nothing and wayyy more quick to parse than the phpbb type system smile

if you still wishes to use/tweak your system, use include/require instead of fopen as it is way more quick to use. then you just need to so something like


ob_start()
include($myfile)
$content = ob_get_content()
ob_clean()
PostPosted: Tue Oct 24, 2006 5:34 am


nick phpBB2's templating system it's better smile

assign variables in and out, set loops, conditionals great biggrin

Noobarmy


Leona_McAllistre

PostPosted: Wed Feb 07, 2007 1:59 pm


gghgygigkn
If your looking to do any amount to template work your going to need a bit more robust engine. While this will work for simple variable replacement, it has no looping or conditional support. I always recommend PHPs Smarty Template Engine User Image - Blocked by "Display Image" Settings. Click to show., its fast and well documented (in typical php fashion).


I second the Smarty Template Engine!
Reply
Server Side Programming

 
Manage Your Items
Other Stuff
Get GCash
Offers
Get Items
More Items
Where Everyone Hangs Out
Other Community Areas
Virtual Spaces
Fun Stuff
Gaia's Games
Mini-Games
Play with GCash
Play with Platinum