[Astrospaces-commits] SF.net SVN: astrospaces: [99] trunk
Brought to you by:
p3net
From: <cal...@us...> - 2007-08-07 17:22:03
|
Revision: 99 http://astrospaces.svn.sourceforge.net/astrospaces/?rev=99&view=rev Author: caleb870 Date: 2007-08-07 10:22:04 -0700 (Tue, 07 Aug 2007) Log Message: ----------- Completed the template system with language files and caching. Next thing is to allow the user to tell the system what to cache. I also included an example of how the template system works in /develop/template-example. Modified Paths: -------------- trunk/functions/template.php trunk/globals.php Added Paths: ----------- trunk/develop/template-example/ trunk/develop/template-example/footer.tpl trunk/develop/template-example/header.tpl trunk/develop/template-example/sub.tpl trunk/develop/template-example/test.cache trunk/develop/template-example/test.tpl trunk/develop/template-example/tester.php Added: trunk/develop/template-example/footer.tpl =================================================================== --- trunk/develop/template-example/footer.tpl (rev 0) +++ trunk/develop/template-example/footer.tpl 2007-08-07 17:22:04 UTC (rev 99) @@ -0,0 +1,14 @@ +<div style="width: 100%; background-color: #EEEEEE; border: 1px solid #CCCCCC"> +Copyright George Won Kong. +<p>In pulvinar leo quis ipsum tempor pellentesque. Sed viverra tellus porttitor quam. +Cras quis dui et velit consectetuer placerat. Nunc feugiat pulvinar quam. Nam +sollicitudin. Donec erat metus, aliquam sed, malesuada sit amet, tristique +laoreet, augue. Nunc eu sem. Sed vel enim. Quisque eu mauris. Suspendisse risus +neque, sodales non, adipiscing lacinia, elementum vitae, lacus. Vestibulum +tortor neque, congue ac, lobortis eget, aliquam ut, tellus. Vestibulum varius +blandit purus. Morbi porta pede fringilla arcu. Cras tristique, eros sit amet +porttitor nonummy, arcu nibh euismod quam, pharetra semper nibh leo vel elit. +Phasellus dignissim leo sit amet turpis.</p> +[address] +</div> +<!-- INCLUDE 'sub.tpl' --> \ No newline at end of file Added: trunk/develop/template-example/header.tpl =================================================================== --- trunk/develop/template-example/header.tpl (rev 0) +++ trunk/develop/template-example/header.tpl 2007-08-07 17:22:04 UTC (rev 99) @@ -0,0 +1,15 @@ +<div style="width: 100%; background-color: #EEEEEE; border: 1px solid #CCCCCC"> +<h1>[LANG:BLOG_AUTHOR]</h1> +</div> +<p> +Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi dictum accumsan ante. +Vestibulum hendrerit vulputate sapien. Donec ac enim. In pulvinar leo quis ipsum tempor +pellentesque. Sed viverra tellus porttitor quam. Cras quis dui et velit consectetuer +placerat. Nunc feugiat pulvinar quam. Nam sollicitudin. Donec erat metus, aliquam sed, +malesuada sit amet, tristique laoreet, augue. Nunc eu sem. Sed vel enim. Quisque eu +mauris. Suspendisse risus neque, sodales non, adipiscing lacinia, elementum vitae, lacus. +Vestibulum tortor neque, congue ac, lobortis eget, aliquam ut, tellus. Vestibulum varius +blandit purus. Morbi porta pede fringilla arcu. Cras tristique, eros sit amet porttitor +nonummy, arcu nibh euismod quam, pharetra semper nibh leo vel elit. Phasellus dignissim +leo sit amet turpis. +</p> \ No newline at end of file Added: trunk/develop/template-example/sub.tpl =================================================================== --- trunk/develop/template-example/sub.tpl (rev 0) +++ trunk/develop/template-example/sub.tpl 2007-08-07 17:22:04 UTC (rev 99) @@ -0,0 +1 @@ +<p><b>All rights reserved to nobody</b></p> \ No newline at end of file Added: trunk/develop/template-example/test.cache =================================================================== --- trunk/develop/template-example/test.cache (rev 0) +++ trunk/develop/template-example/test.cache 2007-08-07 17:22:04 UTC (rev 99) @@ -0,0 +1,101 @@ +O:8:"template":21:{s:6:"slices";a:5:{s:4:"root";a:3:{s:8:"template";s:2085:"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> +<html lang="en"> +<head> + <title>You must be logged in to access this page</title> +</head> +<body> +<div style="width: 100%; background-color: #EEEEEE; border: 1px solid #CCCCCC"> +<h1>Author</h1> +</div> +<p> +Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi dictum accumsan ante. +Vestibulum hendrerit vulputate sapien. Donec ac enim. In pulvinar leo quis ipsum tempor +pellentesque. Sed viverra tellus porttitor quam. Cras quis dui et velit consectetuer +placerat. Nunc feugiat pulvinar quam. Nam sollicitudin. Donec erat metus, aliquam sed, +malesuada sit amet, tristique laoreet, augue. Nunc eu sem. Sed vel enim. Quisque eu +mauris. Suspendisse risus neque, sodales non, adipiscing lacinia, elementum vitae, lacus. +Vestibulum tortor neque, congue ac, lobortis eget, aliquam ut, tellus. Vestibulum varius +blandit purus. Morbi porta pede fringilla arcu. Cras tristique, eros sit amet porttitor +nonummy, arcu nibh euismod quam, pharetra semper nibh leo vel elit. Phasellus dignissim +leo sit amet turpis. +</p> +<!-- Template Error: Including ./leftnav.tpl failed! --> + <h1>Author Test page</h1> +[SLICE:one] +<pre> +[pre] +</pre> +<div style="width: 100%; background-color: #EEEEEE; border: 1px solid #CCCCCC"> +Copyright George Won Kong. +<p>In pulvinar leo quis ipsum tempor pellentesque. Sed viverra tellus porttitor quam. +Cras quis dui et velit consectetuer placerat. Nunc feugiat pulvinar quam. Nam +sollicitudin. Donec erat metus, aliquam sed, malesuada sit amet, tristique +laoreet, augue. Nunc eu sem. Sed vel enim. Quisque eu mauris. Suspendisse risus +neque, sodales non, adipiscing lacinia, elementum vitae, lacus. Vestibulum +tortor neque, congue ac, lobortis eget, aliquam ut, tellus. Vestibulum varius +blandit purus. Morbi porta pede fringilla arcu. Cras tristique, eros sit amet +porttitor nonummy, arcu nibh euismod quam, pharetra semper nibh leo vel elit. +Phasellus dignissim leo sit amet turpis.</p> +[address] +</div> +<p><b>All rights reserved to nobody</b></p> +</body> +</html>";s:6:"result";s:2085:"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> +<html lang="en"> +<head> + <title>You must be logged in to access this page</title> +</head> +<body> +<div style="width: 100%; background-color: #EEEEEE; border: 1px solid #CCCCCC"> +<h1>Author</h1> +</div> +<p> +Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi dictum accumsan ante. +Vestibulum hendrerit vulputate sapien. Donec ac enim. In pulvinar leo quis ipsum tempor +pellentesque. Sed viverra tellus porttitor quam. Cras quis dui et velit consectetuer +placerat. Nunc feugiat pulvinar quam. Nam sollicitudin. Donec erat metus, aliquam sed, +malesuada sit amet, tristique laoreet, augue. Nunc eu sem. Sed vel enim. Quisque eu +mauris. Suspendisse risus neque, sodales non, adipiscing lacinia, elementum vitae, lacus. +Vestibulum tortor neque, congue ac, lobortis eget, aliquam ut, tellus. Vestibulum varius +blandit purus. Morbi porta pede fringilla arcu. Cras tristique, eros sit amet porttitor +nonummy, arcu nibh euismod quam, pharetra semper nibh leo vel elit. Phasellus dignissim +leo sit amet turpis. +</p> +<!-- Template Error: Including ./leftnav.tpl failed! --> + <h1>Author Test page</h1> +[SLICE:one] +<pre> +[pre] +</pre> +<div style="width: 100%; background-color: #EEEEEE; border: 1px solid #CCCCCC"> +Copyright George Won Kong. +<p>In pulvinar leo quis ipsum tempor pellentesque. Sed viverra tellus porttitor quam. +Cras quis dui et velit consectetuer placerat. Nunc feugiat pulvinar quam. Nam +sollicitudin. Donec erat metus, aliquam sed, malesuada sit amet, tristique +laoreet, augue. Nunc eu sem. Sed vel enim. Quisque eu mauris. Suspendisse risus +neque, sodales non, adipiscing lacinia, elementum vitae, lacus. Vestibulum +tortor neque, congue ac, lobortis eget, aliquam ut, tellus. Vestibulum varius +blandit purus. Morbi porta pede fringilla arcu. Cras tristique, eros sit amet +porttitor nonummy, arcu nibh euismod quam, pharetra semper nibh leo vel elit. +Phasellus dignissim leo sit amet turpis.</p> +[address] +</div> +<p><b>All rights reserved to nobody</b></p> +</body> +</html>";s:4:"name";s:4:"root";}s:3:"two";a:5:{s:8:"template";s:69:" + <b style="color: [color]">[two.bold] testing my [two.morez]</b>";s:6:"result";s:69:" + <b style="color: [color]">[two.bold] testing my [two.morez]</b>";s:4:"name";s:3:"two";s:6:"parent";s:3:"one";s:4:"vars";N;}s:4:"blah";a:4:{s:8:"template";s:29:" + <p>Content!</p>";s:6:"result";s:29:" + <p>Content!</p>";s:4:"name";s:4:"blah";s:6:"parent";s:5:"three";}s:5:"three";a:4:{s:8:"template";s:47:" + <b>[two.bolder]</b> + [SLICE:blah]";s:6:"result";s:47:" + <b>[two.bolder]</b> + [SLICE:blah]";s:4:"name";s:5:"three";s:6:"parent";s:3:"one";}s:3:"one";a:5:{s:8:"template";s:137:" + <div style="background-color: [color]; margin: 3px">[content]</div> + [SLICE:two] +<p color="green">[var]</p> + [SLICE:three]";s:6:"result";s:137:" + <div style="background-color: [color]; margin: 3px">[content]</div> + [SLICE:two] +<p color="green">[var]</p> + [SLICE:three]";s:4:"name";s:3:"one";s:6:"parent";s:4:"root";s:4:"vars";N;}}s:4:"lang";a:15:{s:11:"BLOG_AUTHOR";s:6:"Author";s:9:"BLOG_TIME";s:9:"Posted at";s:12:"BLOG_COMMENT";s:23:"This post has 1 comment";s:13:"BLOG_COMMENTS";s:25:"This post has %s comments";s:18:"BLOG_LEAVE_COMMENT";s:40:"Click %s to leave a comment on this post";s:9:"BLOG_POST";s:13:"New blog post";s:22:"BLOG_COMMENT_SUBMITTED";s:31:"Your comment has been submitted";s:19:"BLOG_POST_SUBMITTED";s:28:"Your post has been submitted";s:13:"GALLERY_VIEWS";s:8:"%s views";s:13:"GALLERY_OWNER";s:11:"Uploaded by";s:15:"GALLERY_COMMENT";s:24:"This image has 1 comment";s:16:"GALLERY_COMMENTS";s:26:"This image has %s comments";s:22:"GALLERY_LEAVE_COMMEMNT";s:41:"Click %s to leave a comment on this image";s:11:"GLOBAL_HERE";s:4:"here";s:22:"ERROR_MUST_BE_LOGGEDIN";s:41:"You must be logged in to access this page";}s:11:"lang_prefix";s:5:"LANG:";s:15:"lang_prefix_reg";s:5:"LANG:";s:16:"slice_prefix_reg";s:6:"SLICE:";s:12:"slice_prefix";s:6:"SLICE:";s:16:"start_symbol_reg";s:2:"\[";s:14:"end_symbol_reg";s:2:"\]";s:12:"start_symbol";s:1:"[";s:10:"end_symbol";s:1:"]";s:11:"include_reg";s:30:"\<\!--\s*INCLUDE\s*'%s'\s*--\>";s:11:"slice_start";s:24:"\<\!--\s*START %s\s*--\>";s:9:"slice_end";s:22:"\<\!--\s*END %s\s*--\>";s:11:"slice_array";a:4:{i:0;s:3:"one";i:1;s:3:"two";i:2;s:5:"three";i:3;s:4:"blah";}s:10:"tpl_folder";s:2:"./";s:8:"tpl_file";s:8:"test.tpl";s:10:"cache_file";s:10:"test.cache";s:10:"cache_life";i:5;s:9:"cache_age";i:1186507160;s:7:"recache";b:1;s:12:"cache_exists";b:1;} \ No newline at end of file Added: trunk/develop/template-example/test.tpl =================================================================== --- trunk/develop/template-example/test.tpl (rev 0) +++ trunk/develop/template-example/test.tpl 2007-08-07 17:22:04 UTC (rev 99) @@ -0,0 +1,28 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> +<html lang="en"> +<head> + <title>[LANG:ERROR_MUST_BE_LOGGEDIN]</title> +</head> +<body> +<!-- INCLUDE 'header.tpl' --> +<!-- INCLUDE 'leftnav.tpl' --> + <h1>[LANG:BLOG_AUTHOR] Test page</h1> +<!-- START one --> + <div style="background-color: [color]; margin: 3px">[content]</div> + <!-- START two --> + <b style="color: [color]">[two.bold] testing my [two.morez]</b> + <!-- END two --> + <p color="green">[var]</p> + <!-- START three --> + <b>[two.bolder]</b> + <!-- START blah --> + <p>Content!</p> + <!-- END blah --> + <!-- END three --> +<!-- END one --> +<pre> +[pre] +</pre> +<!-- INCLUDE 'footer.tpl' --> +</body> +</html> \ No newline at end of file Added: trunk/develop/template-example/tester.php =================================================================== --- trunk/develop/template-example/tester.php (rev 0) +++ trunk/develop/template-example/tester.php 2007-08-07 17:22:04 UTC (rev 99) @@ -0,0 +1,19 @@ +<?php +$time = microtime(); +include '../../functions/template.php'; +$tpl = new template('test.tpl','test.cache',5); +$tpl->load_lang_file('../../lang/en/lang_main.php'); +$tpl->compile_slices(); +//print_r($tpl); + +$tpl->set(array('address' => '123 Sample Street NW')); +$tpl->set_multi(array(array('two.bold' => 'Just', 'two.morez' => ' script <br />'),array('two.bold' => 'Im', 'two.morez' => ' pie.', 'color' => ' red')),'two'); +//echo "<a href=\"http://www.deltalabs.net/othercrap/musicsample.zip\">CLICK ME</a>"; +$tpl->set_lang_var('var','GALLERY_COMMENTS','one','5'); +$tpl->parse_slice_to_parent('one'); +//print_r($tpl->slices); +$tpl->pparse(); +$tpl->finish(); +//print_r($tpl->slices); +echo microtime() - $time; +?> \ No newline at end of file Modified: trunk/functions/template.php =================================================================== --- trunk/functions/template.php 2007-08-07 17:20:03 UTC (rev 98) +++ trunk/functions/template.php 2007-08-07 17:22:04 UTC (rev 99) @@ -20,32 +20,38 @@ *********************************************************/ class template { - var $template; - var $result; var $slices; - var $var; var $lang; - var $lang_prefix = 'lang:'; - var $lang_prefix_reg = 'lang:'; - var $slice_prefix_reg = 'slice:'; - var $slice_prefix = 'slice:'; + var $lang_prefix = 'LANG:'; + var $lang_prefix_reg = 'LANG:'; + var $slice_prefix_reg = 'SLICE:'; + var $slice_prefix = 'SLICE:'; var $start_symbol_reg = '\['; var $end_symbol_reg = '\]'; var $start_symbol = '['; var $end_symbol = ']'; - var $slice_start = '\<\!-- START %s --\>'; // The %s is required - var $slice_end = '\<\!-- END %s --\>'; // The %s is required + var $include_reg = '\<\!--\s*INCLUDE\s*\'%s\'\s*--\>'; + var $slice_start = '\<\!--\s*START %s\s*--\>'; + var $slice_end = '\<\!--\s*END %s\s*--\>'; var $slice_array; // System-only variable - //var $line_break = '\r\n'; // Only SINGLE quotes + var $tpl_folder; + var $tpl_file; + var $cache_file; + var $cache_life; + var $cache_age; + var $recache = true; + + var $cache_exists = false; + /* [FUNCTIONS] [ LOADING ] load_from_file - Load template from a file's contents - $file - The file + $file - The file. load - Load template from a variable in PHP $template - Variable with template @@ -105,6 +111,59 @@ */ + function template($file, $cache_to, $cache_life = 600) + { + $this->tpl_folder = dirname($file).'/'; + $this->tpl_file = $file; + $this->cache_file = $cache_to; + $this->cache_life = $cache_life; + + if (isset($cache_to)) + { + $cache_age = @filemtime($cache_to); + $this->cache_age = $cache_age; + // ($cache_age != false AND time() - $cache_life < $cache_age) or ($cache_age != false and $cache_life === 0) + if ($cache_age === false) + { + $this->cache_exists = false; + return $this->load_from_file($file); + } + elseif (time() - $cache_life < $cache_age or $cache_life === 0) + { + $content = file( $cache_to ); + if ($content === false) + { + return false; + } + $this->cache_exists = true; + $cache = unserialize(implode(null,$content)); + $this->slices = $cache->slices; + $this->lang = $cache->lang; + $this->lang_prefix = $cache->lang_prefix; + $this->lang_prefix_reg = $cache->lang_prefix_reg; + $this->slice_prefix_reg = $cache->slice_prefix_reg; + $this->slice_prefix = $cache->slice_prefix; + + $this->start_symbol_reg = $cache->start_symbol_reg; + $this->end_symbol_reg = $cache->end_symbol_reg; + $this->start_symbol = $cache->start_symbol; + $this->end_symbol = $cache->end_symbol; + + $this->slice_start = $cache->slice_start; + $this->slice_end = $cache->slice_end; + return true; + } + elseif ($cache_age != false) + { + $this->cache_exists = false; + return $this->load_from_file($file); + } + } + + $this->cache_exists = true; + return $this->load_from_file($file); + } + function load_from_file ( $file ) { @$content = file( $file ); @@ -119,44 +178,75 @@ function load( $template ) { - $template = $this->parse_lang_vars($template); $this->slices['root']['template'] = $template; $this->slices['root']['result'] = $template; $this->slices['root']['name'] = 'root'; + $this->include_files(); $regex = '/'.$this->slice_start('(.*)').'/smiU'; preg_match_all($regex, $template, $slice_array); $this->slice_array = $slice_array[1]; - - $this->compile_slices(); // KEEP THIS HERE! } - function load_lang_file ( $filename ) + function include_files() { - @$array = include($filename); - if ($array != false) + preg_match_all('/'.sprintf($this->include_reg, '(.*)').'/smU', + $this->slices['root']['template'], + $files); + if (count($files) === 0) return; + + foreach ($files[1] as $file) { - $this->lang = array_merge($this->lang, $array); - } else { - $error->general('Language file could not be loaded', $filename); + @$content = file( $this->tpl_folder.$file ); + if ($content === false) + { + $content = '<!-- Template Error: Including '.$this->tpl_folder.$file.' failed! -->'; + } else { + $content = implode(null,$content); + } + + $regex = '/'.sprintf($this->include_reg, str_replace('/','\/',preg_quote($file))).'/sm'; + $this->slices['root']['template'] = preg_replace($regex, $content, $this->slices['root']['template']); } + + if (preg_match('/'.sprintf($this->include_reg, '(.*)').'/sm', $this->slices['root']['template'], $files)) + { + $this->include_files(); + } } - function langData ( $array ) + function load_lang_file ( $filename ) { - $this->lang = array_merge($this->lang, $array); + if ($this->cache_exists === false) + { + @$array = include($filename); + if ($array != false) + { + $this->lang = array_merge($this->lang, $array); + } else { + $error->general('Language file could not be loaded', $filename); + } + } } - function parse_lang_vars( $input ) + function parse_lang_vars() { - if (empty($this->lang)) return $input; + if (empty($this->lang)) return false; foreach ($this->lang as $name => $value) { - $input = str_replace($this->start_symbol.$this->lang_prefix.$name.$this->end_symbol, $value, $input); + if (strstr($value, '%s') === false) + { + $this->slices['root']['template'] = str_replace($this->start_symbol.$this->lang_prefix.$name.$this->end_symbol, $value, $this->slices['root']['template']); + } } - return $input; + return true; } + function set_lang_var ( $var_name, $lang_name, $parent, $value, $value2 = null, $value3 = null, $value4 = null ) + { + $this->define_var($var_name, sprintf($this->lang[$lang_name], $value, $value2, $value3, $value4),$parent); + } + function slice_start ( $name ) { return sprintf( $this->slice_start, $name); } // System-only function function slice_end ( $name ) { return sprintf( $this->slice_end, $name); } // System-only function @@ -195,6 +285,7 @@ function compile_slices () // System-only function { + $this->parse_lang_vars(); $regex = '/'.$this->slice_end('(.*)').'/smiU'; preg_match_all( $regex, $this->slices['root']['template'], $sub_slices); foreach ($sub_slices[1] as $slice) @@ -364,5 +455,65 @@ $this->slices['root']['result'] = null; $this->slices = null; } + + function dir($location, $dir) // done and tested + { + $array = explode('/',$dir); + if ($array === '.') return true; + $count = count($array); + + for ($i = 0; $i < $count; $i++) + { + for ($ii = 0; $ii < $i; $ii++) + { + if ($ii === 0) + { + $path = $array[$ii]; + } else { + $path .= '/'.$array[$ii]; + } + } + if (!file_exists($location.$path)) + { + $missing = $i; + break 1; + } + } + + if ($missing != Null) + { + for ($i = $missing; $i <= $count; $i++) + { + for ($ii = 0; $ii < $i; $ii++) + { + if ($ii === 0) + { + $path = $array[$ii]; + } else { + $path .= '/'.$array[$ii]; + } + } + + if (!mkdir($location.$path)) return false; + } + } + return true; + } + + function finish() + { + if ($this->cache_exists === false) + { + $resource = fopen($this->cache_file, 'w'); + $this->cache_exists = true; + return fwrite($resource, serialize($this)); + } + return true; + } + + function clear_cache() + { + unlink($this->cache_file); + } } ?> \ No newline at end of file Modified: trunk/globals.php =================================================================== --- trunk/globals.php 2007-08-07 17:20:03 UTC (rev 98) +++ trunk/globals.php 2007-08-07 17:22:04 UTC (rev 99) @@ -19,7 +19,7 @@ @id: $Id$ *********************************************************/ /* Include our larger functions */ -require('./config.php'); //We can't include using a constant defined in the file we're including +require('config.php'); require(AS_LOC_DIRECT.'functions/template.php'); $template =& new template(); require(AS_LOC_DIRECT.'functions/user.php'); @@ -74,6 +74,7 @@ $error =& new error; $message =& new message(); -$db =& new ADOConnection(AS_DB_TYPE); +global $db; +$db =& new ADONewConnection(AS_DB_TYPE); $db->Connect(AS_DB_HOST, AS_DB_USER, AS_DB_PASS, AS_DB_SCHEMA); ?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |