[Astrospaces-commits] SF.net SVN: astrospaces: [22] trunk/functions/template.php
Brought to you by:
p3net
From: <cal...@us...> - 2007-07-29 20:49:31
|
Revision: 22 http://astrospaces.svn.sourceforge.net/astrospaces/?rev=22&view=rev Author: caleb870 Date: 2007-07-29 13:49:34 -0700 (Sun, 29 Jul 2007) Log Message: ----------- Added items remotely C:\Program Files\xampp\htdocs\code\functions\template.php Added Paths: ----------- trunk/functions/template.php Added: trunk/functions/template.php =================================================================== --- trunk/functions/template.php (rev 0) +++ trunk/functions/template.php 2007-07-29 20:49:34 UTC (rev 22) @@ -0,0 +1,270 @@ +<?php +class template { + + var $template; + var $result; + var $slices; + var $var; + var $lang; + var $lang_prefix = 'lang:'; + + var $start_symbol = '['; + var $end_symbol = ']'; + + var $slice_start = '<!-- START %s -->'; // The %s is required + var $slice_end = '<!-- END %s -->'; // The %s is required + + /* + [FUNCTIONS] + + [ LOADING ] + load_from_file - Load template from a file's contents + $file - The file + load - Load template from a variable in PHP + $template - Variable with template + + [ VARIABLES ] + define_var - Defines a variable + $var_name - Name of variable, overwrites previous + $value - Value of variable + $parent (optional) - Parent of variable. Meant for situations when using slices. + add_to_var - Add to the existing value of a variable + $var_name - Name of variable + $add_what - Add what to the variable + $parent (optional) - Parent of variable. Meant for situations when using slices. + + [ SLICES & SLICE PARSING ] + parse_slice_to_var - Parse the result of a slice to a variable + $slice_name - Name of slice to be parsed + $root_var_name - Name of variable to parse the slice to. + $parent (optional) - Parent of variable. Meant for situations when using slices. + parse_slice - Parse a slice. Returns the result to the caller. + $slice_name - Name of slice to be parsed. + + [ ROOT PARSING ] + parse - Parses the root with echoing the result, yet returning the result to the caller. + (No parameters needed) + pparse - Parses the root and echoes the result. + + [ MISCELLANEOUS ] + flush_template - Drops the current data of template + flush_slice - Drops the current data of the slice + $slice_name - Name of slice to drop + reset_template - Resets the entire template and drops all information. + + [ TERMINOLOGY ] + Slice - A Removable or a part of a template that can be repetitively parsed. + + Root - The core template. Slices must be parsed to a variable in the root + in order to be seen, otherwise they are never printed. + + [ HOW IT WORKS ] + When you load a file, it loads all the slices in the file and removes them + from the root template. When you define a variable, it is only added to + a list of variables to be parsed. They can still be added to before it + is parsed. Once it is parsed it echoes the result and resets it so that + it may be parsed again. + + */ + + function load_from_file ( $file ) + { + @$content = file( $file ); + if ($content === false) + { + return false; + } else { + $this->load(implode(null,$content)); + return true; + } + } + + function load( $template ) + { + $this->slice_start = preg_quote($this->slice_start); + $this->slice_end = preg_quote($this->slice_end); + + $this->start_symbol = preg_quote($this->start_symbol); + $this->end_symbol = preg_quote($this->end_symbol); + + $this->template = $template; + $this->result = $template; + + $this->compile_slices(); // KEEP THIS HERE! + } + + /* Under development + function load_lang_file ( $filename ) + { + @$array = include($filename); + if (!$array = false) + { + $this->lang = array_merge($this->lang, $array); + } else { + echo '<p>Language file could not be loaded</p>'; + } + } + + function langValue ( $var_name ) + { + if ($this->lang[$var_name] != NULL) + { + return $this->lang[$var_name]; + } else { + return false; + } + } + + function langPrefix($name) + { + return $this->lang_prefix . $name; + } + */ + + function slice_start ( $name ) { return sprintf( $this->slice_start, $name); } + + function slice_end ( $name ) { return sprintf( $this->slice_end, $name); } + + function compile_slices ($parent = null) + { + if (!empty($parent)) + { + // Searchs for slices in a specified slice + $regex = '/'. $this->slice_start($slice_name) .'(?:.*)'.$this->slice_start('(.*)').'(?:.*)'.$this->slice_end($slice_name).'/smiU'; + preg_match_all( $regex, $this->template, $sub_slices); + } else { + $regex = '/'.$this->slice_end('(.*)').'/smiU'; + preg_match_all( $regex, $this->template, $sub_slices); + } + foreach ($sub_slices[1] as $slice) + { + if ($this->count_slices($slice) > 0) + { + $this->compile_slices($slice); + } else { + $this->assemble_slice($slice); + } + } + + if (!empty($parent)) + { + $this->assemble_slice($parent); + } + + } + + function assemble_slice ( $slice_name ) + { + $regex = '/'.$this->slice_start($slice_name).'(.*)\n\s*'.$this->slice_end($slice_name).'\n?/si'; + preg_match($regex, $this->template, $resultset); + + if (count($resultset) > 0) + { + $this->slices[$slice_name]['template'] = $resultset[1]; + $this->slices[$slice_name]['result'] = $resultset[1]; + $this->slices[$slice_name]['name'] = $slice_name; + $newTemplate = preg_replace($regex,'',$this->template); + $this->template = $newTemplate; + $this->result = $newTemplate; + } + } + + function count_slices ( $parent ) + { + $regex = '/'.$this->slice_start($parent).'(?:.*)('.$this->slice_start('(.*)').')(?:.*)'.$this->slice_end($parent).'/smi'; + preg_match( $regex, $this->template, $sub_slices ); + $count = count($sub_slices); + return $count; + } + + function define_var ( $var_name, $value, $parent = 'root') + { + $this->slices[$parent]['vars'][$var_name]['name'] = $var_name; + $this->slices[$parent]['vars'][$var_name]['value'] = $value; + } + + function add_to_var ( $var_name, $add_what, $parent = 'root' ) + { + $this->slices[$parent]['vars'][$var_name]['value'] .= $add_what; + } + + function parse_slice_to_var ( $slice_name, $root_var_name, $parent = 'root' ) + { + $content = $this->parse_slice( $slice_name ); + $this->define_var($root_var_name, $content, $parent); + return $parsed_result; + } + + function parse_slice ( $slice_name ) + { + if ($this->slices[$slice_name]['vars'] != Null) + { + foreach($this->slices[$slice_name]['vars'] as $variable) + { + $replacement_regex = '/' . $this->start_symbol . preg_quote($variable['name']).$this->end_symbol.'\n?/si'; + $result = preg_replace( $replacement_regex, $variable['value'], $this->slices["$slice_name"]['result'] ); + $this->slices[$slice_name]['result'] = $result; + } + } + + $replacement_regex = '/'.$this->start_symbol.'(.*)'.$this->end_symbol.'\n?/si'; + $this->slices[$slice_name]['result'] = preg_replace( $replacement_regex, '', $this->slices[$slice_name]['result'] ); + + $parsed_result = $this->slices[$slice_name]['result']; + + $this->flush_slice( $slice_name ); + return $parsed_result; + } + + function parse() + { + if ($this->slices['root']['vars'] != null) + { + foreach($this->slices['root']['vars'] as $variable) + { + $replacement_regex = '/'.$this->start_symbol.$variable['name'].$this->end_symbol.'\n?/si'; + $result = preg_replace($replacement_regex,$variable['value'],$this->result); + $this->result = $result; + } + } + + $replacement_regex = '/'.$this->start_symbol.'(.*)'.$this->end_symbol.'\n?/si'; + $this->result = preg_replace( $replacement_regex, '', $this->result ); + + $parsed_result = $this->result; + + $this->flush_template(); + + return $parsed_result; + } + + function pparse ($die_on_finish = false) + { + echo $this->parse(); + + if ($die_on_finish == true) + { + die(); + } + } + + function flush_template () + { + $this->result = $this->template; + $this->slices['root']['vars'] = null; + } + + function flush_slice ( $slice_name ) + { + $this->slices[$slice_name]['result'] = $this->slices[$slice_name]['template']; + $this->slices[$slice_name]['vars'] = null; + } + + function reset_template () + { + $this->template = null; + $this->result = null; + $this->slices = null; + } +} +?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |