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 03-17-2008, 09:24 AM   #1 (permalink)
The Contributor
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
TerrorRonin is on a distinguished road
Asterix Template / Theme Parsing and Management

I'm curious to how you all work your theme / template engines. Do you use SQL, Flat File, something else? How do you load it within your own framework / cms? Do you just use a single class?

I'm currently developing my framework, so i'm trying to figure out a good method to building my theme engine.

I'm considering doing something similar to wordpress, in the idea of using a /themes/ folder with sub-folders being like /default/, /red-panda/, whatever.

But once I get to that point, I'm curious how to make it as customizable as possible. I don't want to mix and match my html as php (or refrain from it as much as possible), yet I want the user to have full control of display of their content.

If you have any example code, that would be awesome :)
TerrorRonin is offline  
Reply With Quote
Old 03-17-2008, 09:53 AM   #2 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Well you gotta narrow down what customization you want the user to have the ability to do. The following is an old class I dug up in my folders it supports variables such as $dog.id $dog.food and <{include=footer.tpl}> it's rather inefficient and handeling it though.

PHP Code:
<?php
class mrf_template
{
    private 
$mrf;
    private 
$key = array();
    public 
$template;
    public 
$cache;
    
    
        public function 
__construct($mrfClass)
        {
            
//accept master class
            
$this->mrf $mrfClass;
            
//set template
            
$this->template $this->mrf->db->GetOne($this->mrf->sql['default_template']);
            
$this->cache $this->mrf->root "Cache/";
            
//see if the template does not exists within the file cache
            
if(!$this->exists("Templates/".$this->template) && !empty($this->template))
            {
                
//the template file path
                
$this->temp_root $this->mrf->root "Templates/" $this->template "/";
                
//assign function replacement values
                
$this->assign_function('title','__fetch("title")',true);
                
$this->assign_function('keywords','__fetch("keywords")',true);
                
$this->assign_function('description','__fetch("description")',true);
                
$this->assign_function('content','getPage()');
                
//assign ariable replacement values
                
$this->assign('url',$this->mrf->info('url'));
                
$this->assign('temp_url',$this->mrf->info('url') . "Templates/" $this->template "/");
                
//now parse the file
                
$parsed $this->parse("main.tpl","templates/$this->template");
                
//now cache it
                
$this->cache("main.php",$parsed,"templates/$this->template");
                unset(
$parsed);
            }
        }
        
        public function 
exists($what)
        {
            return (
is_dir($this->cache $what)) ? true : (file_exists($this->cache $what)) ? true false;
        }
        
        public function 
assign($key,$value)
        {
            
$this->key['<{'.$key.'}>'] = $value;
        }
        
        public function 
assign_function($key,$func,$verbose=false)
        {
            
$echo = ($verbose) ? 'echo ' '';
            
$this->key['<{'.$key.'}>'] = '<?php ' $echo $func '; ?>';
        }
        
        public function 
cache($file,$content,$what)
        {
            
$what ucfirst($what);
            
            
//ok now make sure it's a folder
            
$cache_dir $this->cache $what;
            if(!
is_dir($cache_dir))
            {
                
mkdir($cache_dir,0777);
            }
            
            
//if it wasn't it is now..continue forth
            
file_put_contents($cache_dir "/" $file,$content);
        }
        
        public function 
parse($file,$what)
        {
            
$what ucfirst($what);
            
$oFile file_get_contents($this->mrf->root $what '/' $file);
            
            
//ok first take all the include files
            
preg_match_all('$\\<{inc=(.*?)}\\>$',$oFile,$matches);
            
$i 0;
            foreach(
next($matches) as $filez)
            {
                
$noFile file_get_contents($this->mrf->root $what '/' $filez);
                
$noFile $this->parse_variables($noFile);
                
$oFile str_replace($matches[0][$i],$noFile,$oFile);
                ++
$i;
            }
            
//now the file itself
            
$oFile $this->parse_variables($oFile);
            return 
$oFile;
        }
        
        public function 
parse_variables($oFile)
        {
                
//now split up all the variables and functions
                
preg_match_all('/<\\{(.*?)\\}>/',$oFile,$matches);
                
$matches reset($matches);
                foreach(
$matches as $key)
                {
                    
$expf explode('.',$key);
                    
$exp = (is_array($expf)) ? reset($expf) : $key;
                    if(!
is_array($this->key[$exp]))
                    {
                        
$oFile str_replace($key,$this->key[$exp],$oFile);
                        
//get rid of replace variables
                        
unset($matches[$exp]);
                    }
                    else
                    {
                        
$oFile str_replace($key,$this->key[$exp][$expf[1]]);
                        unset(
$matches[$exp]);
                    }    
                }
                
                return 
$oFile;    
        }
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 03-17-2008, 10:37 AM   #3 (permalink)
The Contributor
 
Join Date: Dec 2007
Posts: 31
Thanks: 0
TerrorRonin is on a distinguished road
Default

Well, I wan't the ability for the user to display how exactly the content of the site is displayed...

An example being, my old theme engine was something similar to:
include header.php

html stuff here
{$content}

include footer.php

And then it had a few variables it would parse for, and replace, ie {$title}, {$content}, {$copyright}, etc..

But the content would display based on page, and would show a static <h1>Page Title</h1>... etc... so they had to code their html and css to match my engine.. which wasn't completely a bad investment, but it lacked a lot of customization, as there was only so much they could do.

So now I was to give them complete control over everything.. ie

I want my news to look like this:
<h1><font color="#ffff00">News: </font> {$post_title}</h1>
{$post_content}

etc..

And then it would be able to change on any type of page in any environment..

This engine will be used for AT LEAST the following type of sites:
High School Homepage
Clan Gaming Site
Stock Market Education
African Safari Information Page

So obviously, all of those pages are completely different in how many pages, whether they're pages such as "Members List", or just an informative amount of text, etc..

So I need to allow for quite a bit of customization.

Any more help would be awesome !
TerrorRonin is offline  
Reply With Quote
Old 03-18-2008, 04:49 PM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

It seriously sounds like you're in the market for SMARTY. :) I think you should check out their class and kinda modify it to suit your needs. (no need to reinvent the wheel is my vision)
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 03-18-2008, 07:17 PM   #5 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

While smarty is certainly nice, what if you don't need all of smarty's power? like the plugin capability and such. In this case simply rolling your own templating system would do the trick.
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 03-18-2008, 07:31 PM   #6 (permalink)
The Contributor
 
flyingbuddha's Avatar
 
Join Date: Jan 2008
Location: Birmingham, UK
Posts: 60
Thanks: 10
flyingbuddha is on a distinguished road
Default

Here's how I do it (basic, stripped version):

/interface/functions.php
PHP Code:
// template swap
function parse(&$content,$values=array()){
    if(
sizeof($values)){
        foreach(
$values as $find=>$replace){
            
$content str_replace($find,$replace,$content);
        }
        return 
$content;
    }

/modules/index.php
PHP Code:
$tpl file_get_contents(dirname(__FILE__).'/tpl/eg.tpl');
echo 
parse($tpl,array(
    
'{title}' => '[SET TITLE]',
    
'{content}' => '',
)); 
/modules/tpl/eg.tpl
Code:
<h1>{title}</h1>
{content}
__________________
Pro. Geek
http://www.mikeholloway.co.uk
flyingbuddha is offline  
Reply With Quote
Old 03-18-2008, 07:47 PM   #7 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

PHP Code:
  foreach($values as $find=>$replace){
            
$content str_replace($find,$replace,$content);
        } 
a better method would be..

PHP Code:
$keys array_keys($values);
$vals array_values($values);
$content str_replace($keys,$vals,$content); 
I'm fairly certain it would be faster then running str_replace x ammounts of times in a loop.
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
The Following User Says Thank You to TlcAndres For This Useful Post:
flyingbuddha (03-19-2008)
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 01:04 AM.

 
     

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