TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-28-2008, 10:25 AM   #1 (permalink)
The Wanderer
 
Join Date: Dec 2008
Posts: 16
Thanks: 2
ChrisR is on a distinguished road
Default 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.
ChrisR is offline  
Reply With Quote
Old 12-28-2008, 02:51 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 12-28-2008, 04:48 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

I would recomend PHPTAL instead of Smarty.
__________________
Tanax is offline  
Reply With Quote
Old 12-28-2008, 10:44 PM   #4 (permalink)
The Wanderer
 
Join Date: Dec 2008
Posts: 16
Thanks: 2
ChrisR is on a distinguished road
Default

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)<?");
?>
ChrisR is offline  
Reply With Quote
Old 12-28-2008, 11:17 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 12-29-2008 at 01:49 AM. Reason: Missed off ?>
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
ChrisR (12-29-2008)
Old 12-28-2008, 11:32 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
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; ?>
__________________
Tanax is offline  
Reply With Quote
Old 12-29-2008, 12:11 AM   #7 (permalink)
The Wanderer
 
Join Date: Dec 2008
Posts: 16
Thanks: 2
ChrisR is on a distinguished road
Default

Thank you sketchMedia,

Ill use this that i did not know you could use endforeach; and endif;
ChrisR is offline  
Reply With Quote
Old 12-29-2008, 11:00 AM   #8 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 12-31-2008, 03:45 PM   #9 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 15
Thanks: 2
Mathew is on a distinguished road
Default

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.
Mathew is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Freelance Suite: Client & Project Management Software CLCook Show Off 2 09-14-2008 10:50 AM
PHP Remote Plugins System Clooth Advanced PHP Programming 1 04-24-2008 08:37 PM
Pure PHP template class. abiko Advanced PHP Programming 1 04-02-2008 05:45 PM
Different style template system? Nor General 2 01-15-2008 01:10 PM
Calendar script needs template system nonenone General 3 04-16-2005 04:15 AM


All times are GMT. The time now is 10:00 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design