Jump to content

templating system, inserting php code into the page...


Zephyris

Recommended Posts

On the index page, it doesn't replace #menu_usager# by the user/guest menu at all.

 

There must be something wrong with my replace function, what am I doing wrong?

 

template system

<?

// Template & Layout
class Template {
// Variables
public $default;
public $header;
public $user;
public $content;
public $footer;
   
   function load($page) {
      $this->header 	= file_get_contents($this->default ."hdp.html");
      $this->content 	= file_get_contents($this->default ."$page.html");
      $this->footer 	= file_get_contents($this->default ."bdp.html");
      
      // Load User Menu depending on session.
      if($_SESSION['user']=="guest"){
      		$this->user	= file_get_contents($this->default . "menu_guest.html");
      } else {
      		$this->user	= file_get_contents($this->default . "menu_logged.html");
      }
   }
   
   
   function replace($var, $content,$from) {
      $this->$from = str_replace("#$var#", $content, $this->$from);
   }
   
   
   function publish() {
     		eval("?>". $this->header . $this->content . $this->footer ."<?");
   }
}
?>

 

index page

<?
session_start();

if($_SESSION['user']==""){
	$_SESSION['user']="guest";
}


include "includes/class.php";

$tmp = new Template;
$tmp->default="styles/origine/";
$tmp->load("p_index");
$tmp->replace("menu_usager",$tmp->user,$tmp->header);
$tmp->publish();

?>

 

Yes, well look at your method:

 

function replace($var, $content,$from) {
      $this->$from = str_replace("#$var#", $content, $this->$from);
   }

 

Notice that your parameter is $from, but you are passing $this->$from, which should instead be just $from.

Yeah I've tried that too.

 

But it doesn't seem to understand that it needs to get #menu_usager# from the header file. as in $tmp->replace("menu_usager",$tmp->user,$tmp->header);

 

$tmp->header is where the header source is located so it should work.

 

tried

function replace($var, $content,$from) {      $from = str_replace("#$var#", $content, $from);   }

and

function replace($var, $content,$from) {      $this->$from = str_replace("#$var#", $content, $from);   }

 

and other forms, I cannot get this to work however

   function replace($var, $content) {
      $this->template = str_replace("#$var#", $content, $this->template);
   }

this is the original code which works.

Basically, PHP is a templating system for HTML. Adding another layer of abstraction will only get things more complicated. With just a little caution, you can use standart PHP code as a template. Anyway.

 

What I don't get in there is the use of eval. Isn't your templating system a pseudo-code based one, where each is replaced with an actual value? As I'm not quite sure if you understand templating systems, I wrote a very quick and basic one just to give you the idea.

 

First I made 2 files called "header.tpl" and "content.tpl" in a folder called "tpl".

 

header.tpl

<h1>{welcome_message}</h1>

 

content.tpl

<p>{some_text}</p>

 

The simple templating class

<?php
class Template_Engine {
private $path = 'tpl/';
private $output = '';

public function load ($tpl) {
	if (is_array($tpl)) {
		foreach ($tpl as $tpl_file) {
			$this->getContents($tpl_file);
		}
	} else {
		$this->getContents($tpl);
	}
}

public function replace ($search, $replace) {
	$this->output = str_replace($search, $replace, $this->output);
}

private function getContents ($file) {
	$file = $this->path . $file . '.tpl';
	if (file_exists($file)) {
		$this->output .= file_get_contents($file);
	}
}

public function output () {
	return $this->output;
}
}
?>

 

How to use it

<?php
$template = new Template_Engine;

$template->load(array('header', 'content'));
$template->replace(array('{welcome_message}', '{some_text}'), array('Welcome to PHP Freaks', 'PHP Freaks is a great community!'));

echo $template->output();
?>

You're still making it more complicated than it has to be.

 

Just use straight up PHP. It is the fastest and easiest solution.

 

Here is a simple class I made a while ago:

<?php

class template
{	
private $vars = array();

private $path;

public function __construct($path)
{
	$this->path = $path;
}

public function view($file)
{
	if (file_exists($this->path . '/' . $file . '.php')) {
		extract($this->vars);

		require $this->path . '/' . $file . '.php';
	} else {
		trigger_error('The template <strong>' . $this->path . '/' . $file . '.php</strong> does not exist!', E_USER_ERROR);
	}
}

public function output($file)
{
	if (file_exists($this->path . '/' . $file . '.php')) {
		extract($this->vars);

		ob_start();

		require $this->path . '/' . $file . '.php';

		$contents = ob_get_contents();

		ob_end_clean();

		return $contents;
	} else {
		trigger_error('The template <strong>' . $this->path . '/' . $file . '.php</strong> does not exist!' E_USER_ERROR);
	}
}

public function assign($key,$val=null)
{
	if (is_array($key)) {
		foreach($key as $k=>$v)
		{
			if (substr($k,-2) == '[]') {
				$k = rtrim($k,'[]');

				if (isset($this->vars[$k])) {
					$temp = $this->vars;

					$temp[$k][] = $v;
				} else {
					$temp[$k][] = $v;
				}
			} else {
				$temp[$k] = $v;
			}

			$this->vars = array_merge($this->vars,$temp);
		}
	} else {
		if (substr($key,-2) == '[]') {
			$key = rtrim($key,'[]');

			if (isset($this->vars[$key])) {
				$temp = $this->vars;

				$temp[$key][] = $val;
			} else {
				$temp[$key][] = $val;
			}
		} else {
			$temp[$key] = $val;
		}						

		$this->vars = array_merge($this->vars,$temp);
	}
}
}

 

EDIT: And usage:

 

$tpl = new template('templates');

$tpl->assign(array(
'tpl_var' => 'some variable',
'tpl_var2' => 'another variable'
));

$tpl->view('test');


// template
echo $tpl_var;
echo $tpl_var2;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.