• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[PHP] Page Class

Zisly

Intermediate OT User
Joined
Jun 9, 2008
Messages
7,338
Reaction score
120
I made this class for my page and I thought I could share it.
This is a pretty simple and easy to use, template/page class.

What it simply does is; it adds content after a tag in the template file.
Aff, I suck at explaining. Download the example instead ^^.

Example:
example.rar

Class:
PHP:
<?php
/*====================================================*\
|| ***************************************************||
|| * This file is part of Z-CMD 0.0.1
|| * -------------------------------------------------||
|| * Author website: http://zisly.com                 ||
|| * Extra credits: 
||   Absolute Mango @otland for the preg_replace help   
|| ***************************************************||
\*====================================================*/
defined( 'A_PAGE' ) or die( 'Restricted access' );

class page
{
    // Holds the page content
    private $page;
    
/* ===========================================================================================*\
|| __construct( )
|| $dir = path to theme
\* ===========================================================================================*/    
    public function __construct($dir)
    {
        $this->page = file_get_contents($dir.'index.html');
        // Fixes the dirs inside the template file
        $this->page = str_ireplace("{themedir?}", $dir, $this->page);
    }
/* ===========================================================================================*\
|| parse ( )
|| $file = file to parse
\* ===========================================================================================*/
    public function parse($file) 
    {
        ob_start();
        include($file);
        $buffer = ob_get_contents();
        ob_end_clean();
        return $buffer;
    }
/* ===========================================================================================*\
|| setdata ()
|| $tags = array of tags and their replacement
|| $bUnique = if the tag is unique
\* ===========================================================================================*/    
    public function setdata($tags, $bUnique = false) 
    {
        // See if it's an array
        if (is_array($tags))
        {
            // Start loop to start replacing tags
            foreach ($tags as $tag => $data)
            {    
                // Check if it's a files content the user wants to include
                $data = (file_exists($data)) ? $this->parse($data) : $data;
                
                // Checks if it's a unique element
                $this->page = ($bUnique) ? preg_replace('/\{'.$tag.'\}/i',$data,$this->page): preg_replace('/\{'.$tag.'\}/i',$data.'{'.$tag.'}',$this->page);
            }
        }
    }
/* =============================================================================================*\
|| output ()
|| Returns the page with all unused tags stripped
\* =============================================================================================*/    
    public function output()
    {
        // Clear all unused tags
        $page = preg_replace('/\{(.*?)\}/i','',$this->page);
        
        unset( $this );
        
        // Returns page
        return $page;
    }
}

?>
Feel free to give me rep++ ;D
OTLand rep:


Simple Reputation System rep:
 
Last edited:
You can have my template compiler:
PHP:
	public static function template($file, $return=false) {
		$template = self::$template;
		
		$tempfile = ROOT.'/tmp/temp__'.$template.'__'.$file.'.php';
		$realfile = ROOT.'/templates/'.$template.'/'.$file.'.html';
		if(!file_exists($tempfile) || (DEBUG && filemtime($realfile) >= filemtime($tempfile))) {
			$data = file_get_contents($realfile);
			// Comments
			$data = preg_replace('|<!-- (.*) -->}|mis', '', $data);
			$data = str_replace('{% end %}', '<?php } ?>', $data);
			$data = str_replace('\$', '?&£&?', $data);
			$data = preg_replace('|\$([a-zA-Z0-9_]*)|', '$GLOBALS["${1}"]', $data);
			$data = preg_replace('|{% if ([^%]+) %}|', '<?php if ( ${1} ) { ?>', $data);
			$data = preg_replace('|{% access (\w+) (\w+) (.+) %}|', '<?php if ( Artic::access("${1}","${2}", ${3}) ) { ?>', $data);
			$data = preg_replace('|{% access (\w+) (\w+) %}|', '<?php if ( Artic::access("${1}","${2}") ) { ?>', $data);
			$data = preg_replace('|{% elif (.+) %}|', '<?php } else if ( ${1} ) { ?>', $data);
			$data = preg_replace_callback('|{% include (\w+) %}|', create_function('$m','return Artic::template($m[1], true);'), $data);
			$data = str_replace('{% else %}', '<?php } else { ?>', $data);
			$data = preg_replace('|{% foreach (.+) %}|', '<?php foreach(${1}) { ?>', $data);
			$data = preg_replace('|{% while (.+) %}|', '<?php while(${1}) { ?>', $data);
			$data = preg_replace('|{% for (.*) %}|', '<?php for(${1}) { ?>', $data); // {% for $i=0 $i<100 $i++ %}
			
			$data = preg_replace('|{{ ([^}]*) }}|', '<?php echo ${1}; ?>', $data);
			$data = preg_replace('|{% ([^%]+) %}|', '<?php Display::${1}; ?>', $data);
			if(!DEBUG) {
				$data = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data);
				$search = array(
        			'/\>[^\S ]+/s', //strip whitespaces after tags, except space
        			'/\{[^\S ]+/s', //strip whitespaces after tags, except space
        			'/[^\S ]+\</s', //strip whitespaces before tags, except space
       			 '/(\s)+/s'  // shorten multiple whitespace sequences
       		);
    			$replace = array(
        			'>',
        			'{',
        			'<',
        			
        			'\\1'
        		);
  				$data = preg_replace($search, $replace, $data);
			}
			$data = str_replace('?&£&?', '$', $data);
			$data = str_replace('?><?php', '', $data);
			file_put_contents($tempfile, $data);
		}
		if($return)
			return file_get_contents($tempfile);
		else
			include($tempfile);
	}
(Needs some moding to fit your system)

I use it like this:
test.php:
PHP:
$myvar = "OTland";
Artic::template('mytemplate');

mytemplate.html:
PHP:
Hello, {{ $myvar }}!
 
You can have my template compiler:
PHP:
	public static function template($file, $return=false) {
		$template = self::$template;
		
		$tempfile = ROOT.'/tmp/temp__'.$template.'__'.$file.'.php';
		$realfile = ROOT.'/templates/'.$template.'/'.$file.'.html';
		if(!file_exists($tempfile) || (DEBUG && filemtime($realfile) >= filemtime($tempfile))) {
			$data = file_get_contents($realfile);
			// Comments
			$data = preg_replace('|<!-- (.*) -->}|mis', '', $data);
			$data = str_replace('{% end %}', '<?php } ?>', $data);
			$data = str_replace('\$', '?&£&?', $data);
			$data = preg_replace('|\$([a-zA-Z0-9_]*)|', '$GLOBALS["${1}"]', $data);
			$data = preg_replace('|{% if ([^%]+) %}|', '<?php if ( ${1} ) { ?>', $data);
			$data = preg_replace('|{% access (\w+) (\w+) (.+) %}|', '<?php if ( Artic::access("${1}","${2}", ${3}) ) { ?>', $data);
			$data = preg_replace('|{% access (\w+) (\w+) %}|', '<?php if ( Artic::access("${1}","${2}") ) { ?>', $data);
			$data = preg_replace('|{% elif (.+) %}|', '<?php } else if ( ${1} ) { ?>', $data);
			$data = preg_replace_callback('|{% include (\w+) %}|', create_function('$m','return Artic::template($m[1], true);'), $data);
			$data = str_replace('{% else %}', '<?php } else { ?>', $data);
			$data = preg_replace('|{% foreach (.+) %}|', '<?php foreach(${1}) { ?>', $data);
			$data = preg_replace('|{% while (.+) %}|', '<?php while(${1}) { ?>', $data);
			$data = preg_replace('|{% for (.*) %}|', '<?php for(${1}) { ?>', $data); // {% for $i=0 $i<100 $i++ %}
			
			$data = preg_replace('|{{ ([^}]*) }}|', '<?php echo ${1}; ?>', $data);
			$data = preg_replace('|{% ([^%]+) %}|', '<?php Display::${1}; ?>', $data);
			if(!DEBUG) {
				$data = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data);
				$search = array(
        			'/\>[^\S ]+/s', //strip whitespaces after tags, except space
        			'/\{[^\S ]+/s', //strip whitespaces after tags, except space
        			'/[^\S ]+\</s', //strip whitespaces before tags, except space
       			 '/(\s)+/s'  // shorten multiple whitespace sequences
       		);
    			$replace = array(
        			'>',
        			'{',
        			'<',
        			
        			'\\1'
        		);
  				$data = preg_replace($search, $replace, $data);
			}
			$data = str_replace('?&£&?', '$', $data);
			$data = str_replace('?><?php', '', $data);
			file_put_contents($tempfile, $data);
		}
		if($return)
			return file_get_contents($tempfile);
		else
			include($tempfile);
	}
(Needs some moding to fit your system)

I use it like this:
test.php:
PHP:
$myvar = "OTland";
Artic::template('mytemplate');

mytemplate.html:
PHP:
Hello, {{ $myvar }}!

Why not just use Smarty which comes with Cache as well?
 
You can have my template compiler:
PHP:
	public static function template($file, $return=false) {
		$template = self::$template;
		
		$tempfile = ROOT.'/tmp/temp__'.$template.'__'.$file.'.php';
		$realfile = ROOT.'/templates/'.$template.'/'.$file.'.html';
		if(!file_exists($tempfile) || (DEBUG && filemtime($realfile) >= filemtime($tempfile))) {
			$data = file_get_contents($realfile);
			// Comments
			$data = preg_replace('|<!-- (.*) -->}|mis', '', $data);
			$data = str_replace('{% end %}', '<?php } ?>', $data);
			$data = str_replace('\$', '?&£&?', $data);
			$data = preg_replace('|\$([a-zA-Z0-9_]*)|', '$GLOBALS["${1}"]', $data);
			$data = preg_replace('|{% if ([^%]+) %}|', '<?php if ( ${1} ) { ?>', $data);
			$data = preg_replace('|{% access (\w+) (\w+) (.+) %}|', '<?php if ( Artic::access("${1}","${2}", ${3}) ) { ?>', $data);
			$data = preg_replace('|{% access (\w+) (\w+) %}|', '<?php if ( Artic::access("${1}","${2}") ) { ?>', $data);
			$data = preg_replace('|{% elif (.+) %}|', '<?php } else if ( ${1} ) { ?>', $data);
			$data = preg_replace_callback('|{% include (\w+) %}|', create_function('$m','return Artic::template($m[1], true);'), $data);
			$data = str_replace('{% else %}', '<?php } else { ?>', $data);
			$data = preg_replace('|{% foreach (.+) %}|', '<?php foreach(${1}) { ?>', $data);
			$data = preg_replace('|{% while (.+) %}|', '<?php while(${1}) { ?>', $data);
			$data = preg_replace('|{% for (.*) %}|', '<?php for(${1}) { ?>', $data); // {% for $i=0 $i<100 $i++ %}
			
			$data = preg_replace('|{{ ([^}]*) }}|', '<?php echo ${1}; ?>', $data);
			$data = preg_replace('|{% ([^%]+) %}|', '<?php Display::${1}; ?>', $data);
			if(!DEBUG) {
				$data = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data);
				$search = array(
        			'/\>[^\S ]+/s', //strip whitespaces after tags, except space
        			'/\{[^\S ]+/s', //strip whitespaces after tags, except space
        			'/[^\S ]+\</s', //strip whitespaces before tags, except space
       			 '/(\s)+/s'  // shorten multiple whitespace sequences
       		);
    			$replace = array(
        			'>',
        			'{',
        			'<',
        			
        			'\\1'
        		);
  				$data = preg_replace($search, $replace, $data);
			}
			$data = str_replace('?&£&?', '$', $data);
			$data = str_replace('?><?php', '', $data);
			file_put_contents($tempfile, $data);
		}
		if($return)
			return file_get_contents($tempfile);
		else
			include($tempfile);
	}
(Needs some moding to fit your system)

I use it like this:
test.php:
PHP:
$myvar = "OTland";
Artic::template('mytemplate');

mytemplate.html:
PHP:
Hello, {{ $myvar }}!

Damn :eek:


Btw, I got my own cache system :p
 
ZCMD
PHP:
<?php
$title' = 'Example',

'logo' => '<a href="index.php">Example Logo</a>',

'content' => '<h1>Example Title</h1>

				Random text',

'top_menu' => '<ul>

              <li><a href="#">Menu 1</a></li>

			 <li><a href="#">Menu 2</a></li>

			  {menu_item}

			  </ul>',

'footer' => '<center><p>Copyright &copy 2010 bla bla.</p></center'

));





// Lets add some more content and some menu items into the custom tags

// which we added before

$page->setdata(array(

'content' => '<br/><b>more</b> content',

'menu_item' => ' <li><a href="#">Menu 3</a></li>'

));



echo $page->output();

?>

VS

VAPus-PHP / Artic PHP output is handled in a registered shutdown event) (Might be released later)
pages/test.php
PHP:
<?php

require_once('artic/global.php');
$title = 'Example';
$logo = '<a href="index.php">Example Logo</a>';


$content = 'Random text';

$top_menu = array('Menu 1'=>'#', 'Menu 2'=>'#')

$footer = 'Copyright &copy 2010 bla bla';





// Lets add some more content and some menu items into the custom tags

// which we added before



$content .= '<br/><b>more</b> content';

$top_menu['Menu 3'] = '#';



Artic::template('mytemplate')

?>
mytemplate.html:
Code:
<h1>{{ $title }}</h1>{{ $logo }}

{% foreach $top_menu as $text => $link %}
<a href="{{ $link }}">{{ $text }}</a>
{% end %}

{{ $content }}
<center><p>{{ $copyright }}</p></center>
 
haha, I got pwnd :(
But remember, I'm a newbie in PHP :p
 
Back
Top