Jump to content

oop templating system


wrathican

Recommended Posts

Hiya people

 

i recently came into the posession of a oop templating class and im trying to modify it a little bit.

the modifications i have made make the script timeout.

 

i know exactly what is going on in the script and how to execute/access the methods and properties inside the class.

 

the original script had one set of 'repeaters' so that it could display one set of records from a database.

 

im trying to modify the script so that it can have more than one set of 'repeaters'

 

could someone please offer their advice on how to resolve this issue?

 

Thanks

 

templating class:

<?php
/*#########################################*/
/*                                         */
/*          PAGE MANAGEMENT CLASS          */
/*                                         */
/* Files that require this class will also */
/*   need to require the database class.   */
/*                                         */
/*#########################################*/

function bbdecode(&$bbcode) {
$bbcode = str_replace(array(
	"\r\n",
	"\r",
	'[b]',
	'[/b]',
	'[i]',
	'[/i]',
	'[img',
	'[/img]',
	'[u]',
	'[/u]',
	'[/url]',
	'[ul]',
	'[/ul]',
	'[*]',
	'',
	'[h1]',
	'[h2]',
	'[h3]',
	'[h4]',
	'[h5]',
	'[h6]',
	'[/h1]',
	'[/h2]',
	'[/h3]',
	'[/h4]',
	'[/h5]',
	'[/h6]',
	"\n\n",
), array(
	"\n",
	"\n",
	'<strong>',
	'</strong>',
	'<em>',
	'</em>',
	'<img src="',
	'" alt="image" />',
	'<u>',
	'</u>',
	'</a>',
	'<ul>',
	'</ul>',
	'<li>',
	'</li>',
	'<h1>',
	'<h2>',
	'<h3>',
	'<h4>',
	'<h5>',
	'<h6>',
	'</h1>',
	'</h2>',
	'</h3>',
	'</h4>',
	'</h5>',
	'</h6>',
	"</p>\n<p>",
), $bbcode);

$bbcode = preg_replace(array(
	'!\[url=http://([^\s]*)]!',
	'!\[url=([^s]*)]!',
), array(
	'<a href="http://$1">',
	'<a href="index.php?page=$1">',
), $bbcode);
}

function lines_to_array($some_text) {
$some_text = str_replace("\r\n", "\n", $some_text);
$some_text = str_replace("\r", "\n", $some_text);
# changes all txt file formats to *nix
$output = explode("\n", $some_text);
# each line in one element of the array
return $output;
}

function templatify_line($a_line, $an_array) {
while (preg_match('!\{\{([A-Z0-9a-z_]*)\}\}!', $a_line, $matches)) {
	# Matches the {{whatever}} stuff in the template files and throws whatever into $matches[1]
	# replace the {{whatever}} with what's in $anArray['whatever']
	$search = '{{'.$matches[1].'}}';
	$rep = $an_array[$matches[1]];
	$a_line = str_replace($search, $rep, $a_line);
}
return($a_line);
}

function word_trim($string, $count, $dots = FALSE){
$words = explode(' ', $string);
if (count($words) > $count){
	array_splice($words, $count);
	$string = implode(' ', $words);
	if (is_string($dots)){
		$string .= $dots;
	}else {
		$string .= '…';
	}
}
return $string;
}

function br_remove($text) {
$text = str_replace("<br />", "", $text);
return $text;
}

class Page {
var $html;       # HTML code for final output
var $Database;   # db connection as defined in class Database
var $headers;    # array for non-repeating section of template
var $repeaters;  # array for repeating section of template
var $source;     # location of template html file

function Page() {
	$this->html = '<h1>Unknown page</h1><p>You must have got here by mistake</p>';
	# default html if called for an unknown set of inputs
	$this->Database = new Database();
}

function parse($src, $head = array(), $rep = array()) {
	$this->source = $src;
	$this->headers = $head;
	$this->repeaters = $rep;
	$this->template();
}

function template() {
	$output = '';

	for ($i = 0; $i < count($this->repeaters); $i++) {
		$a = 0;
		if($i = 0) {
			# put lines to array being the the source
			$unprocessed_htm = file_get_contents($this->source);
			$lines = lines_to_array($unprocessed_htm);
			# the HTML from the template file is chopped into lines.
			unset($unprocessed_htm);
		}else{
			# lines to array is $this->html;
			$lines = lines_to_array($this->html);
			unset($this->html);
		}

		# delimiters for the repeating item area in the template
		$y = $a + 1;

		$repeater_start = array_search('<!--ITEM ' . $y . '-->', $lines);
		$repeater_end = array_search('<!--END OF ITEM ' . $y . '-->', $lines);

		# outside the repeating area, templatifyLine is passed the headers array
		for ($g = 0; $g < $repeater_start; $g++) {
			$nextline = templatify_line($lines[$g], $this->headers);
			$output .= $nextline . "\n";
		}

		# inside the repeating area, templatifyLine is passed elements of the repeaters array which are themselves arrays.
		foreach($this->repeaters[$a] as $an_item) {
			for ($h = $repeater_start + 1; $h < $repeater_end; $h++) {
				$nextline = templatify_line($lines[$h], $an_item);
				$output .= $nextline . "\n";
			}
		}

		# back outside the repeating area, templatifyLine is passed the headers array again
		$c = count($lines);
		for ($k = $repeater_end + 1; $k < $c; $k++) {
			$nextline = templatify_line($lines[$k], $this->headers);
			$output .= $nextline . "\n";
		}

		$a++;

		$this->html = $output;
	}

	$this->error();
}

function error () {
	if(($_SESSION['messagestate'] == '1') || ($_SESSION['messagestate'] == '2')) {

		$html_snippet = '<div class="';
		#1 == bad
		#2 == good
		if($_SESSION['messagestate'] == '2') {
			$html_snippet .= 'success"><p><img src="images/icons/tick.png" alt="Success" /> ';
		}elseif ($_SESSION['messagestate'] == '1'){
			$html_snippet .= 'failure"><p><img src="images/icons/cross.png" alt="Failure" /> ';
		}
		foreach($_SESSION['message'] as $key => $value){
			$html_snippet .= $value.'<br />';
		};
		$html_snippet .= '</p></div>';
		$this->html = str_replace('<!--errors-->', $html_snippet, $this->html);
	}		
}
}
?>

 

how i am calling the class:

<?php
# simple demonstration CMS system
# start session! Durrr
session_start();

#message array
$message_array = array();


require_once('class/page.php');
require_once('class/database.php');

$p = new Page();
$d = new Database();

# put the page and proc get variables into local variables
if (isset($_GET['p']) && $_GET['p'] != '') {
$page = $_GET['p'];
} else {
$page = 'index';
}
if (isset($_GET['proc']) && $_GET['proc'] != '') {
$proc = $_GET['proc'];
} else {
$proc = '';
}
# new Page object for output
$current_login = 'ca5095';
# check the login cookie; if it's not there pull in the login page
if (isset($_COOKIE['85c9d20051'])) {
if ($_COOKIE['85c9d20051'] == md5(serialize($valid). $d->seed)) {
	$current_login = $valid;
}
} elseif ($proc != 'login' && $current_login == 'ca5095') {
$p->parse('templates/admin_login.html');
die($p->html);
}
if ($proc == '') {
# normal pages
switch ($page) {
	case 'index' :
		$all_pages = $d->db_select('*', 'pages');

		$p->parse('templates/admin_template.html', array(), $all_pages);
	break; // index

	case 'images' :
		$images = $d->db_select('*', 'images', '1', 'ORDER BY id ASC');
		//echo'<pre>';print_r($images);die('</pre>');
		$p->parse('templates/images.html', array(), $images);
	break; //images

	case 'edit' :
		$pages = $d->db_select('*', 'pages', "`url` = '{$_GET['page']}'");
		if ($pages == array()) $pages[0] = array('url' => '0', 'heading' => '', 'body' => '');
		if ($pages[0]['url'] == '0') {
			$pages[0]['field'] = '<input name="url" type="text" id="url" />';
		} else {
			$pages[0]['field'] = $pages[0]['url'];
		}
		$pages[0]['body'] = html_entity_decode(br_remove($pages[0]['body']));
		$page_list = $d->db_select('*', 'pages'); 
		$p->parse('templates/edit.html', $pages[0], $page_list);
	break; // edit

	case 'test':
		$array1 = array();

		$array1[] = $d->db_select('*', 'pages');

		$array1[] = $d->db_select('*', 'images');

		$array1[] = $d->db_select('*', 'files');

		$p->parse('templates/test.html', array(), $array1);
	break;

}
echo $p->html;

#unset all session variables
unset($_SESSION['messagestate']);
unset($_SESSION['message']);


}

?>

 

templated page:

<!--ITEM 1-->
{{url}}<br>
<!--END OF ITEM 1--><br>
<br>
<br>
<br>
<!--ITEM 2-->
{{image_url}}<br>
<!--END OF ITEM 2--><br>
<br>
<br>
<br>
<!--ITEM 3-->
{{url}}<br>
<!--END OF ITEM 3-->
...

 

if you need anything else just ask.

Link to comment
https://forums.phpfreaks.com/topic/125923-oop-templating-system/
Share on other sites

im still new to OOP so im making this to practice

 

well, the good news is i managed to fix the timeout but the templete class isn't replacing the bits i need

this is the new class

 

the large comment inside template (); is the original codefor that function.

<?php
/*#########################################*/
/*                                         */
/*          PAGE MANAGEMENT CLASS          */
/*                                         */
/* Files that require this class will also */
/*   need to require the database class.   */
/*                                         */
/*#########################################*/

function bbdecode(&$bbcode) {
$bbcode = str_replace(array(
	"\r\n",
	"\r",
	'[b]',
	'[/b]',
	'[i]',
	'[/i]',
	'[img',
	'[/img]',
	'[u]',
	'[/u]',
	'[/url]',
	'[ul]',
	'[/ul]',
	'[*]',
	'',
	'[h1]',
	'[h2]',
	'[h3]',
	'[h4]',
	'[h5]',
	'[h6]',
	'[/h1]',
	'[/h2]',
	'[/h3]',
	'[/h4]',
	'[/h5]',
	'[/h6]',
	"\n\n",
), array(
	"\n",
	"\n",
	'<strong>',
	'</strong>',
	'<em>',
	'</em>',
	'<img src="',
	'" alt="image" />',
	'<u>',
	'</u>',
	'</a>',
	'<ul>',
	'</ul>',
	'<li>',
	'</li>',
	'<h1>',
	'<h2>',
	'<h3>',
	'<h4>',
	'<h5>',
	'<h6>',
	'</h1>',
	'</h2>',
	'</h3>',
	'</h4>',
	'</h5>',
	'</h6>',
	"</p>\n<p>",
), $bbcode);

$bbcode = preg_replace(array(
	'!\[url=http://([^\s]*)]!',
	'!\[url=([^s]*)]!',
), array(
	'<a href="http://$1">',
	'<a href="index.php?page=$1">',
), $bbcode);
}

function lines_to_array($some_text) {
$some_text = str_replace("\r\n", "\n", $some_text);
$some_text = str_replace("\r", "\n", $some_text);
# changes all txt file formats to *nix
$output = explode("\n", $some_text);
# each line in one element of the array
return $output;
}

function templatify_line($a_line, $an_array) {
while (preg_match('!\{\{([A-Z0-9a-z_]*)\}\}!', $a_line, $matches)) {
	# Matches the {{whatever}} stuff in the template files and throws whatever into $matches[1]
	# replace the {{whatever}} with what's in $anArray['whatever']
	$search = '{{'.$matches[1].'}}';
	$rep = $an_array[$matches[1]];
	$a_line = str_replace($search, $rep, $a_line);
}
return($a_line);
}

function word_trim($string, $count, $dots = FALSE){
$words = explode(' ', $string);
if (count($words) > $count){
	array_splice($words, $count);
	$string = implode(' ', $words);
	if (is_string($dots)){
		$string .= $dots;
	}else {
		$string .= '…';
	}
}
return $string;
}

function br_remove($text) {
$text = str_replace("<br />", "", $text);
return $text;
}

class Page {
var $html;       # HTML code for final output
var $Database;   # db connection as defined in class Database
var $headers;    # array for non-repeating section of template
var $repeaters;  # array for repeating section of template
var $source;     # location of template html file

function Page() {
	$this->html = '<h1>Unknown page</h1><p>You must have got here by mistake</p>';
	# default html if called for an unknown set of inputs
	$this->Database = new Database();
}

function parse($src, $head = array(), $rep = array()) {
	$this->source = $src;
	$this->headers = $head;
	$this->repeaters = $rep;
	$this->template();
}

function template() {
	/*$output = '';
	$unprocessed_htm = file_get_contents($this->source);
	$lines = lines_to_array($unprocessed_htm);
	# the HTML from the template file is chopped into lines.
	unset($unprocessed_htm);*/



	#echo count($this->repeaters);
	#echo'<pre>';print_r($this->repeaters[0]);die('</pre>');

	for($r = 0; $r < count($this->repeaters);$r++) {
		#echo'<pre>';print_r($this->repeaters[$r]);echo('</pre>');


		if($r == 0) {
			# if this is the first instance, get source from file
			$output = '';
			$unprocessed_htm = file_get_contents($this->source);
			$lines = lines_to_array($unprocessed_htm);
			# the HTML from the template file is chopped into lines.
			unset($unprocessed_htm);
		}else{
			# if more than first instance use exsisting html property as source.
			$output = '';
			$lines = lines_to_array($this->html);
			# the HTML from the template file is chopped into lines.
			$this->html = '';
		}

		# run through the source to convert repeating areas
		$y = $r + 1;
		# where repeating area start and finishes
		$repeater_start = array_search('<!--ITEM ' . $y . '-->', $lines);
		$repeater_end = array_search('<!--END OF ITEM ' . $y . '-->', $lines);

		# outside the repeating area, templatifyLine is passed the headers array
		for ($i = 0; $i < $repeater_start; $i++) {
			$nextline = templatify_line($lines[$i], $this->headers);
			$output .= $nextline . "\n";
		}

		# inside the repeating area, templatifyLine is passed elements of the repeaters array which are themselves arrays.
		foreach($this->repeaters[$r] as $an_array) {
			foreach($an_array as $an_itm) {
				for ($i = $repeater_start + 1; $i < $repeater_end; $i++) {
					$nextline = templatify_line($lines[$i], $an_item);
					$output .= $nextline . "\n";
					#echo $nextline;
				}
			}
		}

		# back outside the repeating area, templatifyLine is passed the headers array again
		$c = count($lines);
		for ($i = $repeater_end + 1; $i < $c; $i++) {
			$nextline = templatify_line($lines[$i], $this->headers);
			$output .= $nextline . "\n";
		}

		$this->html = $output;

	}
	/*

	# delimiters for the repeating item area in the template
	$repeater_start = array_search('<!--ONE ITEM-->', $lines);
	$repeater_end = array_search('<!--END OF ONE ITEM-->', $lines);

	# outside the repeating area, templatifyLine is passed the headers array
	for ($i = 0; $i < $repeater_start; $i++) {
		$nextline = templatify_line($lines[$i], $this->headers);
		$output .= $nextline . "\n";
	}

	# inside the repeating area, templatifyLine is passed elements of the repeaters array which are themselves arrays.
	foreach($this->repeaters as $an_item) {
		for ($i = $repeater_start + 1; $i < $repeater_end; $i++) {
			$nextline = templatify_line($lines[$i], $an_item);
			$output .= $nextline . "\n";
		}
	}

	# back outside the repeating area, templatifyLine is passed the headers array again
	$c = count($lines);
	for ($i = $repeater_end + 1; $i < $c; $i++) {
		$nextline = templatify_line($lines[$i], $this->headers);
		$output .= $nextline . "\n";
	}

	$this->html = $output;*/
	$this->error();
}

function error () {
	if(($_SESSION['messagestate'] == '1') || ($_SESSION['messagestate'] == '2')) {

		$html_snippet = '<div class="';
		#1 == bad
		#2 == good
		if($_SESSION['messagestate'] == '2') {
			$html_snippet .= 'success"><p><img src="images/icons/tick.png" alt="Success" /> ';
		}elseif ($_SESSION['messagestate'] == '1'){
			$html_snippet .= 'failure"><p><img src="images/icons/cross.png" alt="Failure" /> ';
		}
		foreach($_SESSION['message'] as $key => $value){
			$html_snippet .= $value.'<br />';
		};
		$html_snippet .= '</p></div>';
		$this->html = str_replace('<!--errors-->', $html_snippet, $this->html);
	}		
}
}
?>

everything else stays the same

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.