Reply to Re: [PHP] A couple questions about templating

Your name:

Reply:


Posted by James Williams on 05/10/05 03:07

On 5/9/05, Rory Browne <rory.browne@gmail.com> wrote:
> > I made a second one, which reads the file into an array,
> > changes the variables the same way, but instead compiles the finished
> > product into a compile directory and then reads the compiled file back
> > to the page. the benefit to this being that I can easily create a
> > simple cache system now.
>
> All I'd change here, is that instead of reading the file into an
> array, I'd load it into a string.
>

That was a mistake on my part, it is loaded into a string.

> > 2nd question) Like I said before, I've mastered the art of variable
> > replacement, but how would one go about adding support for if {}
> > statements as well as loops. I'm a little bit lost when it comes to
> > this.
>
> You could use a full featured tokeniser/parser(and you'd probably IIRC
> find one in Pear), but it may perhaps be simpler, to simply use preg
> to replace something like {foreach:var:list}, or whatever your
> perfered syntax.
>

The problem is that, I have no problem replacing the keyword with the
proper php syntax for a loop or whatever, the problem is that it just
shows up as a string when all is said and done.

If you want to see the code I have right now, here it is (below). I'm
thinking I'm going to have to combine the load() and run() methods,
and then make assign() create an array of stuff that must be assigned,
then do pretty much all the work in the combined method or load() and
run(). That probably sounded pretty confusing, I hope you understand
what I'm sayin.


<?php
class tpl {
//
// PRIVATE CLASS VARIABLES
//
var $file;
var $template;
var $compiled;

//
// LOAD A TEMPLATE FILE
//
function load($template) {
$this->template = 'tpl/' . $template . '.tpl';
$this->compiled = 'compiled/' . $template . '.tplc';
if (file_exists($this->template) && filemtime($this->template) >
filemtime($this->compiled)) {
$file = fopen($this->template,'r');
$this->file = fread($file,filesize($this->template));
fclose($file);
} elseif (file_exists($this->compiled) &&
filemtime($this->template) < filemtime($this->compiled)) {
readfile($this->compiled);
} else {
print('</p>' . $this->template . ' Does not exist</p>');
}
}

//
// ASSIGN THE VARIABLES
//
function assign($var,$value) {
if (isset($var) && isset($value)) {
$this->file = str_replace('{' . $var . '}',$value,$this->file);
}
}

//
// DISPLAY THE TEMPLATE
//
function run() {
if (filemtime($this->template) > filemtime($this->compiled)) {
$file = fopen($this->compild,'w');
fwrite($file,$this->file);
fclose($file);
readfile($this->compiled);
}
}
}
?>


--
jamwil.com

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация