TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Template System (http://www.talkphp.com/absolute-beginners/3812-template-system.html)

ChrisR 12-28-2008 10:25 AM

Template System
 
Hello,

I need some help i have been trying to make a template system but i am unsure on how to do it.

Like it will read all the content of a file and replace seleted things with php if,foreach etc.

EG of what i mean .tpl file

HTML Code:

<-- if $member->group == admin -->

adminCP link

<-- endif -->                                                 

<-- foreach $array as $value => $key -->

key: $key value: $value

<-- endforeach -->

i know it will need a preg_replace and eval but i am unsure on how do use them the right why to make it work.

xenon 12-28-2008 02:51 PM

A regex driven engine is pretty tough to make, and also is pretty slow than for instance, a php implementation of a templating system (php embedded templates). Besides of that, it also implies that you learn a new language, in between html and php. That can't be efficient. Why not use an existing templating system (like smarty, although I'm not a fan) and take a good look on how that was built.

Tanax 12-28-2008 04:48 PM

I would recomend PHPTAL instead of Smarty.

ChrisR 12-28-2008 10:44 PM

Hello,

Thank you for your reply's why i do not want to use a premade template system is i am trying to make my own sort of framework that ill use for all my projects. I would like it to be set out like this i know it mite be a bit slower but it does not bother me a lot here is what i have worked out so far but i am unsure again if this would be on the right track.

PHP Code:

<?php

class template 
{
    
    var 
$template_dir     '.templates/';
    var 
$file_ext         '.tpl';
    var 
$buffer;
    
    function 
buff_template ($file
    {
        if( 
file_exists$this->template_dir $file $this->file_ext ) )
        {
            
$this->buffer file_get_contents$this->template_dir $file $this->file_ext );            
            return 
$this->parse_variables($this->buffer);
        }
        else
        {
            die();
        }
    }
    
    function 
parse_variables($input)
    {    
        
$search preg_match_all('/<-- {.*?} -->/'$input$matches);
            
        foreach(
$matches as $value)
        {
            
$input preg_replace("'<-- if (.*) -->'","<? if(\\1) {?>"$input);
            
$input preg_replace("'<-- foreach (.*) -->'","<? foreach(\\1) {?>"$input);
        }
        
        
$content str_replace("<-- endif -->","<?}?>"$content);
        
$content str_replace("<-- endforeach -->","<?}?>"$content);
        
        return 
$input;
    }

}

$engine = new template;

echo eval(
"?>$engine->buff_template(login)<?");
?>


sketchMedia 12-28-2008 11:17 PM

why not just use PHP?
PHP Code:

<?php if($member->group == admin): ?>

adminCP link

<?php endif; ?>        

<?php foreach ($array as $value => $key ) : ?>

key: <?php echo $key?> value: <?php echo $value?>

<?php endforeach; ?>

Cuts out the need to waste resources parsing another language.

Tanax 12-28-2008 11:32 PM

Quote:

Originally Posted by sketchMedia (Post 20813)
why not just use PHP?
PHP Code:

<?php if($member->group == admin): ?>

adminCP link

<?php endif; ?>        

<?php foreach ($array as $value => $key ) :

key: <?php echo $key?> value: <?php echo $value?>

<?php endforeach; ?>

Cuts out the need to waste resources parsing another language.

Edit:

PHP Code:

<?php if($member->group == admin): ?>

adminCP link

<?php endif; ?>        

<?php foreach ($array as $value => $key ) : ?>

key: <?php echo $key?> value: <?php echo $value?>

<?php endforeach; ?>


ChrisR 12-29-2008 12:11 AM

Thank you sketchMedia,

Ill use this that i did not know you could use endforeach; and endif;

xenon 12-29-2008 11:00 AM

That's what I was recommending as well. Plus, by using directly PHP you also have all the language structures that PHP provides you: like initializing objects, calling they're methods, looping structures, and so on. That is:

1. hard to accomplish using another meta language for your templates
2. uselessly slow (I'm not talking about 1 template file with 100 lines. what do you do when you combine 7 different template files to make one big template, and then parse that (which let's say has 3000 lines). believe me, you will notice a decrease in performance there). you don't have to transform the php code to another language just so that you can transform it back into php later. I once tried to make a templating system myself, and finally got back to using php templates because of all the stuff that needed replication (like an if-else statement, or a while loop).

ChrisR: that's the "long form" of the for, foreach, while, switch.

Mathew 12-31-2008 03:45 PM

Actually Xenon, a lot of PHP templating libraries compile the templates, so you will only suffer a performance hit on the initial page load.

There's no overhead most of the time and they are usually easier for designers to use as they don't need to learn how to use all PHP functions.


All times are GMT. The time now is 09:22 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0