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 01-07-2009, 11:54 PM   #1 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default Template Phraser

What is the best method for outputting templates. At the moment I have:

Template Class:

PHP Code:
function load($viewFile,$data '')
{
    
$this->templateFolder APP_PATH 'templates/' TEMPLATE '/';  
        
    if(
file_exists($this->templateFolder '/views/' $viewFile '.php'))
    {
        
$file_output file_get_contents($this->templateFolder '/views/' $viewFile '.php');
        
        foreach(
$data as $key=>$val)
        {
            
$file_output str_replace('{' .$key '}',$val,$file_output);
        }
        
       echo 
$file_output;
    }

Controller:

PHP Code:
class index_controller extends controller
{
    function 
index()
    {
        
$data['welcome_message'] = 'This is the home page';
        
$this->core->template->load('account/test',$data);
    }
    
    function 
contact()
    {
        
$data['welcome_message'] = 'This is the contact page'
        
$this->core->template->load('account/test',$data);
    }

View File:

HTML Code:
{welcome_message}

That all works, but i need something a bit more advanced, where i can do things like:

HTML Code:
{if $core->user->isRegistered()}
    
    Hello {$core->user->info('username')}

{endif}
I don't want to use smarty or anything I ideally need just 1 function that will phrase it from that to real php. Does anyone know if there is a simpleish way of doing it.

Thanks in advance.
Scottymeuk is offline  
Reply With Quote
Old 01-08-2009, 12:37 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

php Code:
<?php if $core->user->isRegistered(): ?>
   
    Hello <?php $core->user->info('username'); ?>

<?php endif; ?>
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-08-2009, 01:05 AM   #3 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

Going back to the thread you replyed in before about application wide objects etc.

This means i have to do $core = core::init(); at the top of each view file which, sorry to say is not going to happen as its terrible and looks horrible also.
Scottymeuk is offline  
Reply With Quote
Old 01-08-2009, 01:40 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I don't remember creating an init() function in that other thread.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-08-2009, 01:58 AM   #5 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

I posted what im using. Kalle's class.
Scottymeuk is offline  
Reply With Quote
Old 01-08-2009, 07:03 AM   #6 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

Why make your own template parser when there are lots of open source Classes already made? I use H2O: http://github.com/speedmax/h2o-php/tree/master
sjaq is offline  
Reply With Quote
The Following User Says Thank You to sjaq For This Useful Post:
Scottymeuk (01-08-2009)
Old 01-08-2009, 04:22 PM   #7 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
Why make your own template parser when there are lots of open source Classes already made? I use H2O: http://github.com/speedmax/h2o-php/tree/master

I would use it but its to extensive. I need something that is tiny and possibly one one file.
Scottymeuk is offline  
Reply With Quote
Old 01-08-2009, 09:07 PM   #8 (permalink)
The Contributor
 
Join Date: Jan 2009
Posts: 40
Thanks: 10
Scottymeuk is on a distinguished road
Default

Sorry for the double post but id just like to say I'm going to use Wildhoney's method and use the following function:

PHP Code:
public function __set($index$value)
{
    
$this->vars[$index] = $value;

And the do foreach loop on $this->vars in the load() function
Scottymeuk 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
Template Class Issues WinSrev Advanced PHP Programming 20 11-04-2008 05:05 PM
Pure PHP template class. abiko Advanced PHP Programming 1 04-02-2008 05:45 PM
Template / Theme Parsing and Management TerrorRonin Advanced PHP Programming 6 03-18-2008 07:47 PM
Different style template system? Nor General 2 01-15-2008 01:10 PM
[Request tutorial] Template class Tanax Advanced PHP Programming 31 10-25-2007 06:56 PM


All times are GMT. The time now is 11:09 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