PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Sep 21, 2018 7:34 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri May 26, 2006 1:41 pm 
Offline
Forum Contributor

Joined: Thu Dec 08, 2005 8:10 pm
Posts: 246
Syntax: [ Download ] [ Hide ]
<?php

class Template{

    static private $path2function = array();

   

    private static function load($path){

        $code = file_get_contents($path);

        $name = create_function('$_', "?>$code<?php ");

        self::$path2function[$path] = $name;

    }

   

    static function show($path, $info = array()){

        $path = "templates/$path.inc";

        if (!isset(self::$path2function[$path])) { self::load($path); }

        $name = self::$path2function[$path];

        if (function_exists($name)) {

            $name($info);

        } else {

            throw new Exception("Template function is not avalabale");

        }

    }

   

    static function get($path, $info = array()){

        ob_start();

        self::show($path, $info);

        $block = ob_get_contents();

        ob_end_clean();

        return $block;

    }

}

?>


Last edited by Gambler on Fri May 26, 2006 4:01 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 3:24 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
usage? code examples?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 3:55 pm 
Offline
Forum Contributor

Joined: Thu Dec 08, 2005 8:10 pm
Posts: 246
Usage:
Syntax: [ Download ] [ Hide ]
<?php

Template::show('article/plain', array('text' => 'tihs is my text', 'title' => 'The Article'));

?>


get works in the same way, but it returns output instead of echo'ing it.

Template file:
templates/article/plain.inc
Syntax: [ Download ] [ Hide ]
<h2><?=$_['title']?></h2>

<?=$_['text']?>


Templates are converted into functions, which is very efficient. You can nest and loop all you want. On the other hand, template files are pure php. There is no boilerplate code whatsoever. Can be as simple as you want and as powerful as you want.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group