[Cs-content-commits] SF.net SVN: cs-content:[352] releases/1.0
PHP Templating & Includes System
Brought to you by:
crazedsanity
|
From: <cra...@us...> - 2009-02-04 16:42:56
|
Revision: 352
http://cs-content.svn.sourceforge.net/cs-content/?rev=352&view=rev
Author: crazedsanity
Date: 2009-02-04 16:42:52 +0000 (Wed, 04 Feb 2009)
Log Message:
-----------
*** RELEASE 1.0-ALPHA5 ***
SUMMARY OF CHANGES:::
* better handling of block rows and template vars.
* a few more methods for testing & stripping undefined template vars.
* fixed problem with mainTemplate getting an invalid prefix when an absolute
path is defined (#236)
SVN COMMAND:::
merge --depth=infinity -r349:HEAD https://cs-content.svn.sourceforge.net/svnroot/cs-content/trunk/1.0
Modified Paths:
--------------
releases/1.0/VERSION
releases/1.0/cs_genericPage.class.php
releases/1.0/tests/testOfCSContent.php
Added Paths:
-----------
releases/1.0/tests/files/gptest_all-together.txt
releases/1.0/tests/files/templates/
releases/1.0/tests/files/templates/content.shared.tmpl
releases/1.0/tests/files/templates/footer.shared.tmpl
releases/1.0/tests/files/templates/infobar.shared.tmpl
releases/1.0/tests/files/templates/main.shared.tmpl
releases/1.0/tests/files/templates/menubar.shared.tmpl
releases/1.0/tests/files/templates/title.shared.tmpl
Removed Paths:
-------------
releases/1.0/tests/files/templates/content.shared.tmpl
releases/1.0/tests/files/templates/footer.shared.tmpl
releases/1.0/tests/files/templates/infobar.shared.tmpl
releases/1.0/tests/files/templates/main.shared.tmpl
releases/1.0/tests/files/templates/menubar.shared.tmpl
releases/1.0/tests/files/templates/title.shared.tmpl
Property Changed:
----------------
releases/1.0/tests/files/sampleConfig.xml
Modified: releases/1.0/VERSION
===================================================================
--- releases/1.0/VERSION 2009-02-04 16:41:11 UTC (rev 351)
+++ releases/1.0/VERSION 2009-02-04 16:42:52 UTC (rev 352)
@@ -1,5 +1,5 @@
## Stores the current version of the cs-content system, and it's source. Please do NOT modify this file.
-VERSION: 1.0-ALPHA4
+VERSION: 1.0-ALPHA5
PROJECT: cs-content
$HeadURL$
\ No newline at end of file
Modified: releases/1.0/cs_genericPage.class.php
===================================================================
--- releases/1.0/cs_genericPage.class.php 2009-02-04 16:41:11 UTC (rev 351)
+++ releases/1.0/cs_genericPage.class.php 2009-02-04 16:42:52 UTC (rev 352)
@@ -11,9 +11,10 @@
require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php");
class cs_genericPage extends cs_contentAbstract {
- var $templateObj; //template object to parse the pages
- var $templateVars = array(); //our copy of the global templateVars
- var $mainTemplate; //the default layout of the site
+ public $templateObj; //template object to parse the pages
+ public $templateVars = array(); //our copy of the global templateVars
+ public $mainTemplate; //the default layout of the site
+ public $unhandledVars=array();
private $tmplDir;
private $libDir;
@@ -60,9 +61,15 @@
*/
protected function initialize_locals($mainTemplateFile) {
- //NOTE: this **requires** that the global variable "SITE_ROOT" is already set.
- $this->siteRoot = preg_replace('/\/public_html/', '', $_SERVER['DOCUMENT_ROOT']);
- $this->tmplDir = $this->siteRoot .'/templates';
+
+ if(strlen(dirname($mainTemplateFile)) && dirname($mainTemplateFile) !== '/') {
+ $this->tmplDir = dirname($mainTemplateFile);
+ }
+ else {
+ //NOTE: this **requires** that the global variable "SITE_ROOT" is already set.
+ $this->siteRoot = preg_replace('/\/public_html/', '', $_SERVER['DOCUMENT_ROOT']);
+ }
+ $this->siteRoot = preg_replace('/\/templates$/', '', $this->tmplDir);
$this->libDir = $this->siteRoot .'/lib';
//if there have been some global template vars (or files) set, read 'em in here.
@@ -81,7 +88,7 @@
//build a new instance of the template library (from PHPLib)
$this->templateObj=new Template($this->tmplDir,"keep"); //initialize a new template parser
- if(preg_match('/^\//', $mainTemplateFile)) {
+ if(!preg_match('/^\//', $mainTemplateFile)) {
$mainTemplateFile = $this->tmplDir ."/". $mainTemplateFile;
}
$this->mainTemplate=$mainTemplateFile; //load the default layout
@@ -199,7 +206,7 @@
$name = $handle;
$str = $this->templateVars[$parent];
- $reg = "/<!-- BEGIN $handle -->.+<!-- END $handle -->/sU";
+ $reg = "/<!-- BEGIN $handle -->(.+){0,}<!-- END $handle -->/sU";
preg_match_all($reg, $str, $m);
if(!is_string($m[0][0])) {
#exit("set_block_row(): couldn't find '$handle' in var '$parent'");
@@ -273,6 +280,7 @@
if(!$this->templateVars[$str2] && $stripUndefVars) {
//TODO: set an internal pointer or something to use here, so they can see what was missed.
$this->templateObj->varvals[out] = str_replace($str, '', $this->templateObj->varvals[out]);
+ $this->unhandledVars[$str2]++;
}
}
$this->templateObj->parse("out", "out");
@@ -524,10 +532,10 @@
//cast $retArr as an array, so it's clean.
$retArr = array();
- //NOTE: the value 31 isn't just a randomly chosen length; it's the minimum
- // number of characters to have a block row. EG: "<!-- BEGIN x -->o<!-- END x -->"
+ //NOTE: the value 30 isn't just a randomly chosen length; it's the minimum
+ // number of characters to have a block row. EG: "<!-- BEGIN x --><!-- END x -->"
$templateContents = $this->templateVars[$templateVar];
- if(strlen($templateContents) >= 31) {
+ if(strlen($templateContents) >= 30) {
//looks good to me. Run the regex...
$flags = PREG_PATTERN_ORDER;
$reg = "/<!-- BEGIN (\S{1,}) -->/";
@@ -615,6 +623,48 @@
return($this->allowInvalidUrls);
}//end allow_invalid_urls()
//---------------------------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function return_printed_page($stripUndefVars=1) {
+ ob_start();
+ $this->print_page($stripUndefVars);
+ $retval = ob_get_contents();
+ ob_end_clean();
+ return($retval);
+ }//end return_printed_page()
+ //-------------------------------------------------------------------------
+
+
+
+ //-------------------------------------------------------------------------
+ public function strip_undef_template_vars($section='content') {
+ $numLoops = 0;
+ if(isset($this->templateVars[$section])) {
+ $templateContents = $this->templateVars[$section];
+ while(preg_match_all('/\{.\S+?\}/', $templateContents, $tags) && $numLoops < 50) {
+ $tags = $tags[0];
+
+ //TODO: figure out why this works when running it twice.
+ foreach($tags as $key=>$str) {
+ $str2 = str_replace("{", "", $str);
+ $str2 = str_replace("}", "", $str2);
+ if(!$this->templateVars[$str2]) {
+ //TODO: set an internal pointer or something to use here, so they can see what was missed.
+ $templateContents = str_replace($str, '', $templateContents);
+ }
+ }
+ $this->templateObj->parse("out", "out");
+ $numLoops++;
+ }
+ }
+ else {
+ throw new exception(__METHOD__ .": section (". $section .") does not exist");
+ }
+ return($templateContents);
+ //-------------------------------------------------------------------------
+ }
}//end cs_genericPage{}
?>
Copied: releases/1.0/tests/files/gptest_all-together.txt (from rev 351, trunk/1.0/tests/files/gptest_all-together.txt)
===================================================================
--- releases/1.0/tests/files/gptest_all-together.txt (rev 0)
+++ releases/1.0/tests/files/gptest_all-together.txt 2009-02-04 16:42:52 UTC (rev 352)
@@ -0,0 +1,19 @@
+<html>
+ <head>
+ <title>This is the title.</title>
+ </head>
+<body>
+ <table>This is the infobar.</table>
+ --- the menubar (DATE: 2009-01-01)
+---+++ CONTENT STARTS HERE +++---
+ <!-- BEGIN blockRow1 -->This is a TEST<!-- comment --><!-- END blockRow1 -->
+ <!-- BEGIN blockRow2 -->This
+ is second blockRow<!-- END blockRow2 -->
+<!-- BEGIN blockRow3 --> Some data In here...
+ <!-- END blockRow3 -->
+
+ <!-- BEGIN blockRow4 --><!-- END blockRow4 -->
+---+++ CONTENT ENDS HERE +++---
+ --- the footer.
+</body>
+</html>
\ No newline at end of file
Property changes on: releases/1.0/tests/files/sampleConfig.xml
___________________________________________________________________
Added: svn:eol-style
+ native
Deleted: releases/1.0/tests/files/templates/content.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/content.shared.tmpl 2009-02-04 16:41:11 UTC (rev 351)
+++ releases/1.0/tests/files/templates/content.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -1,8 +0,0 @@
-
- <!-- BEGIN blockRow1 -->This is a TEST<!-- comment --><!-- END blockRow1 -->
- <!-- BEGIN blockRow2 -->This
- is second blockRow<!-- END blockRow2 -->
-<!-- BEGIN blockRow{blockRowTestVal} --> Some data In here...
- <!-- END blockRow3 -->
-
- <!-- BEGIN blockRow4 --><!-- END blockRow4 -->{invisibleTemplateVar}
Copied: releases/1.0/tests/files/templates/content.shared.tmpl (from rev 351, trunk/1.0/tests/files/templates/content.shared.tmpl)
===================================================================
--- releases/1.0/tests/files/templates/content.shared.tmpl (rev 0)
+++ releases/1.0/tests/files/templates/content.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -0,0 +1,8 @@
+
+ <!-- BEGIN blockRow1 -->This is a TEST<!-- comment --><!-- END blockRow1 -->
+ <!-- BEGIN blockRow2 -->This
+ is second blockRow<!-- END blockRow2 -->
+<!-- BEGIN blockRow{blockRowTestVal} --> Some data In here...
+ <!-- END blockRow3 -->
+
+ <!-- BEGIN blockRow4 --><!-- END blockRow4 -->{invisibleTemplateVar}
Deleted: releases/1.0/tests/files/templates/footer.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/footer.shared.tmpl 2009-02-04 16:41:11 UTC (rev 351)
+++ releases/1.0/tests/files/templates/footer.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -1 +0,0 @@
---- the footer.
\ No newline at end of file
Copied: releases/1.0/tests/files/templates/footer.shared.tmpl (from rev 351, trunk/1.0/tests/files/templates/footer.shared.tmpl)
===================================================================
--- releases/1.0/tests/files/templates/footer.shared.tmpl (rev 0)
+++ releases/1.0/tests/files/templates/footer.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -0,0 +1 @@
+--- the footer.
\ No newline at end of file
Deleted: releases/1.0/tests/files/templates/infobar.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/infobar.shared.tmpl 2009-02-04 16:41:11 UTC (rev 351)
+++ releases/1.0/tests/files/templates/infobar.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -1 +0,0 @@
-<table>This is the infobar.</table>
\ No newline at end of file
Copied: releases/1.0/tests/files/templates/infobar.shared.tmpl (from rev 351, trunk/1.0/tests/files/templates/infobar.shared.tmpl)
===================================================================
--- releases/1.0/tests/files/templates/infobar.shared.tmpl (rev 0)
+++ releases/1.0/tests/files/templates/infobar.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -0,0 +1 @@
+<table>This is the infobar.</table>
\ No newline at end of file
Deleted: releases/1.0/tests/files/templates/main.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/main.shared.tmpl 2009-02-04 16:41:11 UTC (rev 351)
+++ releases/1.0/tests/files/templates/main.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -1,11 +0,0 @@
-<html>
- <head>
- <title>{title}</title>
- </head>
-<body>
- {infobar}
- {menubar}
----+++ CONTENT STARTS HERE +++---{content}---+++ CONTENT ENDS HERE +++---
- {footer}
-</body>
-</html>
\ No newline at end of file
Copied: releases/1.0/tests/files/templates/main.shared.tmpl (from rev 351, trunk/1.0/tests/files/templates/main.shared.tmpl)
===================================================================
--- releases/1.0/tests/files/templates/main.shared.tmpl (rev 0)
+++ releases/1.0/tests/files/templates/main.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -0,0 +1,11 @@
+<html>
+ <head>
+ <title>{title}</title>
+ </head>
+<body>
+ {infobar}
+ {menubar}
+---+++ CONTENT STARTS HERE +++---{content}---+++ CONTENT ENDS HERE +++---
+ {footer}
+</body>
+</html>
\ No newline at end of file
Deleted: releases/1.0/tests/files/templates/menubar.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/menubar.shared.tmpl 2009-02-04 16:41:11 UTC (rev 351)
+++ releases/1.0/tests/files/templates/menubar.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -1 +0,0 @@
---- the menubar (DATE: {date})
\ No newline at end of file
Copied: releases/1.0/tests/files/templates/menubar.shared.tmpl (from rev 351, trunk/1.0/tests/files/templates/menubar.shared.tmpl)
===================================================================
--- releases/1.0/tests/files/templates/menubar.shared.tmpl (rev 0)
+++ releases/1.0/tests/files/templates/menubar.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -0,0 +1 @@
+--- the menubar (DATE: {date})
\ No newline at end of file
Deleted: releases/1.0/tests/files/templates/title.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/title.shared.tmpl 2009-02-04 16:41:11 UTC (rev 351)
+++ releases/1.0/tests/files/templates/title.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -1 +0,0 @@
-This is the title.
\ No newline at end of file
Copied: releases/1.0/tests/files/templates/title.shared.tmpl (from rev 351, trunk/1.0/tests/files/templates/title.shared.tmpl)
===================================================================
--- releases/1.0/tests/files/templates/title.shared.tmpl (rev 0)
+++ releases/1.0/tests/files/templates/title.shared.tmpl 2009-02-04 16:42:52 UTC (rev 352)
@@ -0,0 +1 @@
+This is the title.
\ No newline at end of file
Modified: releases/1.0/tests/testOfCSContent.php
===================================================================
--- releases/1.0/tests/testOfCSContent.php 2009-02-04 16:41:11 UTC (rev 351)
+++ releases/1.0/tests/testOfCSContent.php 2009-02-04 16:42:52 UTC (rev 352)
@@ -22,8 +22,8 @@
require_once(dirname(__FILE__) .'/../cs_globalFunctions.class.php');
require_once(dirname(__FILE__) .'/../cs_siteConfig.class.php');
- $this->gf = new cs_globalFunctions;
- $this->gf->debugPrintOpt=1;
+ $this->gfObj = new cs_globalFunctions;
+ $this->gfObj->debugPrintOpt=1;
}//end __construct()
//-------------------------------------------------------------------------
@@ -245,6 +245,63 @@
+ //-------------------------------------------------------------------------
+ function test_genericPage() {
+ $filesDir = dirname(__FILE__) .'/files';
+
+ $page = new cs_genericPage(false, $filesDir .'/templates/main.shared.tmpl', false);
+ $fs = new cs_fileSystem($filesDir .'/templates');
+
+ $lsData = $fs->ls();
+
+ foreach($lsData as $index=>$value) {
+ $filenameBits = explode('.', $index);
+ $page->add_template_var($filenameBits[0], $page->file_to_string($index));
+ }
+
+ $page->add_template_var('blockRowTestVal', 3);
+ $page->add_template_var('date', '2009-01-01');
+
+ $checkThis = $page->return_printed_page();
+
+
+ $this->assertEqual($checkThis, file_get_contents($filesDir .'/gptest_all-together.txt'));
+
+ //now let's rip all the template rows out & add them back in.
+ $rowDefs = $page->get_block_row_defs('content');
+ $rippedRows = $page->rip_all_block_rows('content');
+
+ $this->assertEqual($rowDefs['ordered'], array_keys($rippedRows));
+ $remainingRows = $page->rip_all_block_rows('content');
+ $this->assertEqual(array(), $remainingRows, "ERROR: some block rows exist after ripping: ".
+ $this->gfObj->string_from_array(array_keys($remainingRows), 'null', ','));
+
+
+ foreach($rippedRows as $name=>$data) {
+ $page->add_template_var($name, $data);
+ }
+ $checkThis2 = $page->return_printed_page();
+
+ $this->assertEqual($checkThis, $checkThis2);
+
+ $checkThis = $page->return_printed_page(0);
+ $this->assertTrue(preg_match('/\{.\S+?\}/', $checkThis));
+
+ //clone the page object so we can change stuff & not affect the original.
+ $page2 = clone $page;
+ unset($page2->templateVars);
+ $this->assertNotEqual($page->templateVars, $page2->templateVars);
+ $page2 = clone $page;
+
+ $this->assertNotEqual($page2->templateVars['content'], $page2->strip_undef_template_vars('content'));
+ $this->assertNotEqual($page2->templateVars['content'], $page2->strip_undef_template_vars('content'));
+ $page2->templateVars['content'] = $page2->strip_undef_template_vars('content');
+ $this->assertEqual($page->return_printed_page(1), $page2->return_printed_page(1));
+ }//end test_genericPage
+ //-------------------------------------------------------------------------
+
+
+
}//end TestOfCSContent
//=============================================================================
?>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|