Re: Adding Loop / If support to a simple template engine

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thanks everybody, but is this not already a really simple parser?

<?php
class tpl {
//
// USER ACCESIBLE VARIABLES
//

//
// PRIVATE CLASS VARIABLES
//
var $_template;

//
// USER DEFINED VARIABLES
//
var $tpl_directory; // REQUIRED - LOCATION OF THE TEMPLATES
var $var_brace_open; // OPTIONAL - BRACE AROUND TEMPLATE KEYWORDS, DEFAULTS TO {
var $var_brace_close; // OPTIONAL - BRACE AROUND TEMPLATE KEYWORDS, DEFAULTS TO }

//
// LOAD THE TEMPLATE PAGE
//
function load($tpl) {
if (file_exists($this->tpl_directory . $tpl)) {
$code = file($this->tpl_directory . $tpl);
$this->_template = implode('<!-- New Line -->',$code);
} else {
print('<p>The Template File Doesn\'t Exist</p>');
}
}

//
// ASSIGN A VARIABLE
//
function assign($var,$value) {
if (isset($this->_template)) {
if (!isset($this->var_brace_open) || !isset($this->var_brace_close)) {
$this->var_brace_open = '{';
$this->var_brace_close = '}';
}
$var = $this->var_brace_open . $var . $this->var_brace_close;
$this->_template = str_replace($var,$value,$this->cls_template);
} else {
print('<p>Please load the template file before assigning variables. use $tpl->load(\'file\'); to do this</p>');
}
}

//
// DISPLAY THE PAGE
//
function display() {
$this->_template = explode('<!-- New Line -->',$this->cls_template);
foreach ($this->cls_template as $line) {
print($line);
}
}
}
?>


Drewcore wrote:
a parser is a program that i will take some preformatted text, extract
the information from it, and then do work with that information. xml
programs users parses to pull data from between the <> tags.

in your case, you'd have to have some sort of declaration in your code
that sets apart code blocks you want replaced, invent some sort of
system of replacing that code on demand, then write that code that
implements your system. a little difficult, and out of my scope, i'm
sure. unless i spent two weeks with minimal sleep.

drew

On Apr 6, 2005 9:37 PM, James Williams <php@xxxxxxxxxx> wrote:

Richard Lynch wrote:

On Wed, April 6, 2005 7:04 pm, James Williams said:


Hey fellas, I'm makin a forum right now and as a sub project, I'm making
a template engine / class.  Right now all it does is variable
replacement, however I am curious as to how I can implement support for
loops and if statements.  I would appreciate any ideas that you may
have, thank-you!


You'll have to write a parser.

Or use something like bison/flex/yacc to create one for you.

This is not something you want to try to knock out in an afternoon because
you're bored. :-)


Thank-you, but what exactly does a parser do?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux