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 04-02-2008, 09:34 AM   #1 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default Pure PHP template class.

Pure PHP template class.
Nowdays with ZendFramework, Cake or any other MVC framework you have their integrated pure PHP template class - really makes life easier.
I'm not trying to raise the old-flame-war debate : Templating engines vs. Pure PHP templates.
For my point of view there are few advantages for Pure PHP:
  • Faster execution
  • No need for learning template engine's syntax
  • All the functions you need are there in PHP or you just extend the class and write your own

With this engine you can simply use and make your templates with the power of PHP behind you.

Usage - simple example:
PHP Code:
<?php
include 'x3View.php';

// Include the template file
$tmpl = new x3View'template/index.php');

// Assign some data
$tmpl->title "Simple example page";
$tmpl->data  "This is a really simple example!";

// Display the template;
$tmpl->display();
?>
The template:
PHP Code:
<?= $this->title?><br />
<?= $this->data?>
Displays:
Code:
Simple example page <br />
This is a really simple example!
Advanced usage:
PHP Code:
<?php
include 'x3View.php';

// The sub page 
$sub = new x3View'template/sub.php' );
$sub->title "Wow I'm going to be parsed";
$sub_parsed $sub->parse();

// Main page
$index = new x3View'template/index.php' );
$index->sub_page $sub_parsed;
$index->title "Wow we included something!";

$index->display();
?>
Now the templates:
sub.php
PHP Code:
I am a sub page. <br />
<?= $this->title ?>
index.php
PHP Code:
Hi! I am an index page! <br />
<?= $this->title?> <br />
Below is a parsed sub page: <br />
<?= $this->sub_page?>
At last - the output:

Code:
Hi! I am an index page! <br />
Wow we included something! <br />
Below is a parsed sub page: <br />
I am a sub page! <br />
Wow I'm going to be parsed
Usage is simple - simply use it like a Singleton :)
And at last - the class.
PHP Code:
<?php
  
  
class x3View {
      protected 
$file;  
    
      public function 
__construct$file ) {
        
$this->file $file;
      }
      
      
      
/*
       *  Output methods
       * ------------------------------
       */ 
      
public function display() {
        require 
$this->file;
      }
      
      public function 
parse() {
        
        
// Start
          
ob_start();
          require( 
$this->file );
          
$tmpl ob_get_contents();
          
ob_end_clean();  
          return 
$tmpl;
      }
    
  }
?>
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
Old 04-02-2008, 05:45 PM   #2 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Biggest con is it isn't separating logic from design. Designers will have to know basic PHP in order to create a template that uses PHP in it.

It is quick and easy though like you said.
__________________
Eric
wGEric 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


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