[Cs-content-commits] SF.net SVN: cs-content:[350] trunk/1.0
PHP Templating & Includes System
Brought to you by:
crazedsanity
|
From: <cra...@us...> - 2009-02-04 16:34:34
|
Revision: 350
http://cs-content.svn.sourceforge.net/cs-content/?rev=350&view=rev
Author: crazedsanity
Date: 2009-02-04 16:34:29 +0000 (Wed, 04 Feb 2009)
Log Message:
-----------
Unit testing for template/block row handling & minor feature additions.
/cs_genericPage.class.php:
* MAIN:::
-- changed "var" definitions into public definitions
-- added "unhandledVars=array()" definition
* initialize_locals():
-- creates the tmplDir from the dirname of the "mainTemplateFile", if it
is determinable (i.e. if a path to the template was given).
-- siteRoot is taken from tmplDir after dropping the ending "/templates"
part off the path. NOTE::: siteRoot should eventually be able to be set
independently of the tmplDir... this should eventually use constants as
default values.
-- tmplDir is only appended to the mainTemplateFile var when it does NOT
begin with a slash (old logic was inverted).
* set_block_row():
-- allows blank block rows to be removed instead of requiring at least
one character between definitions (get_block_row_defs() would see the
row, but it would not get removed).
* print_page():
-- updates internal "unhandledVars" array when stripping undefined vars
(mostly for debugging purposes)
* get_block_row_defs():
-- changed minimum length of block row from 31 to 30 to match changes in
set_block_row() method.
* return_printed_page() [NEW]:
-- uses output buffering when calling print_page() to return the page
without printing it.
* strip_undef_template_vars() [NEW]:
-- strip undefined vars from a part of the page & return that parsed
section (does not affect given section directly).
/tests/testOfCSContent.php:
* __construct():
-- create internal cs_globalFunctions{} object, turn debug printing on.
* test_genericPage() [NEW]:
-- runs some basic tests to make sure that template parsing & block row
handling all work exactly as they are expected to.
/tests/files/:
* added some files to support unit tests for cs_genericPage{}.
Modified Paths:
--------------
trunk/1.0/cs_genericPage.class.php
trunk/1.0/tests/testOfCSContent.php
Added Paths:
-----------
trunk/1.0/tests/files/gptest_all-together.txt
trunk/1.0/tests/files/templates/
trunk/1.0/tests/files/templates/content.shared.tmpl
trunk/1.0/tests/files/templates/footer.shared.tmpl
trunk/1.0/tests/files/templates/infobar.shared.tmpl
trunk/1.0/tests/files/templates/main.shared.tmpl
trunk/1.0/tests/files/templates/menubar.shared.tmpl
trunk/1.0/tests/files/templates/title.shared.tmpl
Property Changed:
----------------
trunk/1.0/tests/files/sampleConfig.xml
Modified: trunk/1.0/cs_genericPage.class.php
===================================================================
--- trunk/1.0/cs_genericPage.class.php 2009-02-03 19:28:36 UTC (rev 349)
+++ trunk/1.0/cs_genericPage.class.php 2009-02-04 16:34:29 UTC (rev 350)
@@ -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{}
?>
Added: trunk/1.0/tests/files/gptest_all-together.txt
===================================================================
--- trunk/1.0/tests/files/gptest_all-together.txt (rev 0)
+++ trunk/1.0/tests/files/gptest_all-together.txt 2009-02-04 16:34:29 UTC (rev 350)
@@ -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: trunk/1.0/tests/files/gptest_all-together.txt
___________________________________________________________________
Added: svn:eol-style
+ native
Property changes on: trunk/1.0/tests/files/sampleConfig.xml
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/1.0/tests/files/templates/content.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/content.shared.tmpl (rev 0)
+++ trunk/1.0/tests/files/templates/content.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350)
@@ -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}
Property changes on: trunk/1.0/tests/files/templates/content.shared.tmpl
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/1.0/tests/files/templates/footer.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/footer.shared.tmpl (rev 0)
+++ trunk/1.0/tests/files/templates/footer.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350)
@@ -0,0 +1 @@
+--- the footer.
\ No newline at end of file
Property changes on: trunk/1.0/tests/files/templates/footer.shared.tmpl
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/1.0/tests/files/templates/infobar.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/infobar.shared.tmpl (rev 0)
+++ trunk/1.0/tests/files/templates/infobar.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350)
@@ -0,0 +1 @@
+<table>This is the infobar.</table>
\ No newline at end of file
Property changes on: trunk/1.0/tests/files/templates/infobar.shared.tmpl
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/1.0/tests/files/templates/main.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/main.shared.tmpl (rev 0)
+++ trunk/1.0/tests/files/templates/main.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350)
@@ -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
Property changes on: trunk/1.0/tests/files/templates/main.shared.tmpl
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/1.0/tests/files/templates/menubar.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/menubar.shared.tmpl (rev 0)
+++ trunk/1.0/tests/files/templates/menubar.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350)
@@ -0,0 +1 @@
+--- the menubar (DATE: {date})
\ No newline at end of file
Property changes on: trunk/1.0/tests/files/templates/menubar.shared.tmpl
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/1.0/tests/files/templates/title.shared.tmpl
===================================================================
--- trunk/1.0/tests/files/templates/title.shared.tmpl (rev 0)
+++ trunk/1.0/tests/files/templates/title.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350)
@@ -0,0 +1 @@
+This is the title.
\ No newline at end of file
Property changes on: trunk/1.0/tests/files/templates/title.shared.tmpl
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/1.0/tests/testOfCSContent.php
===================================================================
--- trunk/1.0/tests/testOfCSContent.php 2009-02-03 19:28:36 UTC (rev 349)
+++ trunk/1.0/tests/testOfCSContent.php 2009-02-04 16:34:29 UTC (rev 350)
@@ -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.
|