TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Template Phraser (http://www.talkphp.com/advanced-php-programming/3845-template-phraser.html)

Scottymeuk 01-07-2009 11:54 PM

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.

Wildhoney 01-08-2009 12:37 AM

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

<?php endif; ?>

Scottymeuk 01-08-2009 01:05 AM

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.

Wildhoney 01-08-2009 01:40 AM

I don't remember creating an init() function in that other thread.

Scottymeuk 01-08-2009 01:58 AM

I posted what im using. Kalle's class.

sjaq 01-08-2009 07:03 AM

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

Scottymeuk 01-08-2009 04:22 PM

Quote:

Originally Posted by sjaq (Post 21066)
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 01-08-2009 09:07 PM

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


All times are GMT. The time now is 11:27 PM.

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