[Cs-content-commits] SF.net SVN: cs-content:[459] trunk/1.0
PHP Templating & Includes System
Brought to you by:
crazedsanity
From: <cra...@us...> - 2009-09-15 20:28:26
|
Revision: 459 http://cs-content.svn.sourceforge.net/cs-content/?rev=459&view=rev Author: crazedsanity Date: 2009-09-15 20:28:18 +0000 (Tue, 15 Sep 2009) Log Message: ----------- Fix issue #301 + #237. /cs_genericPage.class.php: * MAIN::: -- remove dependency on template.inc (#237) -- remove templateObj var * initialize_locals(): -- don't allow dirname if it equals "." -- fix path using cs_fileSystem::resolve_path_with_dots() so there are no "../" things in the path (for unit testing) -- don't create instance of Template{}. * print_page(): -- do ALL parsing of template vars internally instead of using methods from Template (i.e. Template::parse() and Template::pparse()). -- if the template var "main" is set, use that instead of pulling the contents of the mainTemplate (maintains old functionality). -- prints output directly. * file_to_string(): -- explodes the filename on "templates" so there's no redundant adding of the templates directory... (There are probably situations where this logic is broken, i.e. if there is "templates/../templates" in the filename) -- use the path given by template_file_exists() as the path to the file. -- NOTE::: if the file can't be loaded, it should really throw an exception instead of calling set_message_wrapper(), since that would break if the file that couldn't be loaded was the main template or the message template. * template_file_exists(): -- returns boolean false instead of numeric 0 by default. /required/ [DELETED] * removed unneeded folder (and "template.inc" from PHPLib). /tests/testOfCSContent.php: * test_genericPage(): -- add tests on template_file_exists() and file_to_string(). -- drop bogus check on templateObj->varvals['out'], since Template{} is no longer being used. -- added notes about where things fail on r455 (or less) of cs_genericPage. -- use full path to the main template. -- lots of tests with block rows to ensure issue #237 is fixed. /tests/files/(et all) -- tests with blockrows embedded within other blockrows with unused template vars sprinkled around. -- added the required "system" folder with the 404 and message_box templates. Modified Paths: -------------- trunk/1.0/cs_genericPage.class.php trunk/1.0/tests/files/gptest_all-together.txt trunk/1.0/tests/files/templates/content.shared.tmpl trunk/1.0/tests/testOfCSContent.php Added Paths: ----------- trunk/1.0/tests/files/gptest_blockrows.txt trunk/1.0/tests/files/gptest_blockrows2.txt trunk/1.0/tests/files/templates/system/ trunk/1.0/tests/files/templates/system/404.shared.tmpl trunk/1.0/tests/files/templates/system/message_box.tmpl Removed Paths: ------------- trunk/1.0/required/ Modified: trunk/1.0/cs_genericPage.class.php =================================================================== --- trunk/1.0/cs_genericPage.class.php 2009-09-14 15:21:16 UTC (rev 458) +++ trunk/1.0/cs_genericPage.class.php 2009-09-15 20:28:18 UTC (rev 459) @@ -7,10 +7,8 @@ * $LastChangedBy$ * $LastChangedRevision$ */ -require_once(dirname(__FILE__) ."/required/template.inc"); class cs_genericPage extends cs_contentAbstract { - public $templateObj; //template object to parse the pages public $templateVars = array(); //our copy of the global templateVars public $templateFiles = array(); //our list of template files... public $templateRows = array(); //array of block rows & their contents. @@ -63,7 +61,7 @@ $mainTemplateFile = preg_replace('/^\//', '', $mainTemplateFile); } - if(isset($mainTemplateFile) && strlen($mainTemplateFile) && is_dir(dirname($mainTemplateFile))) { + if(isset($mainTemplateFile) && strlen($mainTemplateFile) && is_dir(dirname($mainTemplateFile)) && dirname($mainTemplateFile) != '.') { $this->siteRoot = dirname($mainTemplateFile); if(preg_match('/\//', $this->siteRoot) && preg_match('/templates/', $this->siteRoot)) { $this->siteRoot .= "/.."; @@ -78,6 +76,8 @@ else { throw new exception(__METHOD__ .": cannot locate siteRoot from main template file (". $mainTemplateFile .")"); } + $fs = new cs_fileSystem(dirname(__FILE__)); + $this->siteRoot = $fs->resolve_path_with_dots($this->siteRoot); $this->tmplDir = $this->siteRoot .'/templates'; $this->libDir = $this->siteRoot .'/lib'; @@ -97,9 +97,6 @@ } } unset($GLOBALS['templateVars'], $GLOBALS['templateFiles']); - - //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)) { $mainTemplateFile = $this->tmplDir ."/". $mainTemplateFile; @@ -270,21 +267,33 @@ //Show any available messages. $this->process_set_message(); - //Load the default page layout. - $this->templateObj->set_file("main", $this->mainTemplate); - - //load the placeholder names and thier values - $this->templateObj->set_var($this->templateVars); - $this->templateObj->parse("out","main"); //parse the sub-files into the main page + if(isset($this->templateVars['main'])) { + //this is done to simulate old behaviour (the "main" templateVar could overwrite the entire main template). + $out = $this->templateVars['main']; + } + else { + $out = $this->file_to_string($this->mainTemplate); + } + if(!strlen($out)) { + $this->gfObj->debug_print($out); + $this->gfObj->debug_print($this->mainTemplate); + $this->gfObj->debug_print("MANUAL FILE CONTENTS::: ". htmlentities(file_get_contents($this->tmplDir .'/'. $this->mainTemplate))); + exit(__METHOD__ .": mainTemplate (". $this->mainTemplate .") was empty...?"); + } + $numLoops = 0; + $tags = array(); + while(preg_match_all('/\{.\S+?\}/', $out, $tags) && $numLoops < 10) { + $out = $this->gfObj->mini_parser($out, $this->templateVars, '{', '}'); + $numLoops++; + } + if($stripUndefVars) { - $this->templateObj->varvals['out'] = $this->strip_undef_template_vars( - $this->templateObj->varvals['out'], - $this->unhandledVars - ); + $out = $this->strip_undef_template_vars($out, $this->unhandledVars); } - $this->templateObj->pparse("out","out"); //parse the main page + print($out); + }//end of print_page() //--------------------------------------------------------------------------------------------- @@ -330,9 +339,20 @@ * content & returns it. */ public function file_to_string($templateFileName) { + + if(preg_match('/templates/', $templateFileName)) { + $bits = explode('templates', $templateFileName); + if(count($bits) == 2) { + $templateFileName = $bits[1]; + } + else { + throw new exception(__METHOD__ .": full path to template file given but could not break the path into bits::: ". $templateFileName); + } + } $templateFileName = preg_replace('/\/\//', '\/', $templateFileName); - if($this->template_file_exists($templateFileName)) { - $retval = file_get_contents($this->tmplDir .'/'. $templateFileName); + $fullPathToFile = $this->template_file_exists($templateFileName); + if($fullPathToFile !== false && strlen($fullPathToFile)) { + $retval = file_get_contents($fullPathToFile); } else { $this->set_message_wrapper(array( "title" => 'Template File Error', @@ -351,7 +371,7 @@ * Checks to see if the given filename exists within the template directory. */ public function template_file_exists($file) { - $retval = 0; + $retval = false; //If the string doesn't start with a /, add one if (strncmp("/",$file,1)) { //strncmp returns 0 if they match, so we're putting a / on if they don't Modified: trunk/1.0/tests/files/gptest_all-together.txt =================================================================== --- trunk/1.0/tests/files/gptest_all-together.txt 2009-09-14 15:21:16 UTC (rev 458) +++ trunk/1.0/tests/files/gptest_all-together.txt 2009-09-15 20:28:18 UTC (rev 459) @@ -13,6 +13,10 @@ <!-- END blockRow3 --> <!-- BEGIN blockRow4 --><!-- END blockRow4 --> + + <!-- BEGIN blockRow5 --><!-- BEGIN embeddedBlockRow1 --><!-- END embeddedBlockRow1 --> + <!-- BEGIN embeddedBlockRow2 --><!-- END embeddedBlockRow2 --> + <!-- END blockRow5 --> ---+++ CONTENT ENDS HERE +++--- --- the footer. </body> Copied: trunk/1.0/tests/files/gptest_blockrows.txt (from rev 458, trunk/1.0/tests/files/gptest_all-together.txt) =================================================================== --- trunk/1.0/tests/files/gptest_blockrows.txt (rev 0) +++ trunk/1.0/tests/files/gptest_blockrows.txt 2009-09-15 20:28:18 UTC (rev 459) @@ -0,0 +1,20 @@ +<html> + <head> + <title>This is the title.</title> + </head> +<body> + <table>This is the infobar.</table> + --- the menubar (DATE: ) +---+++ CONTENT STARTS HERE +++--- + + +<!-- BEGIN blockRow3 --> Some data In here... + <!-- END blockRow3 --> + + + + +---+++ CONTENT ENDS HERE +++--- + --- the footer. +</body> +</html> \ No newline at end of file Copied: trunk/1.0/tests/files/gptest_blockrows2.txt (from rev 458, trunk/1.0/tests/files/gptest_all-together.txt) =================================================================== --- trunk/1.0/tests/files/gptest_blockrows2.txt (rev 0) +++ trunk/1.0/tests/files/gptest_blockrows2.txt 2009-09-15 20:28:18 UTC (rev 459) @@ -0,0 +1,19 @@ +<html> + <head> + <title>This is the title.</title> + </head> +<body> + <table>This is the infobar.</table> + --- the menubar (DATE: ) +---+++ CONTENT STARTS HERE +++--- + + + + + + + +---+++ CONTENT ENDS HERE +++--- + --- the footer. +</body> +</html> \ No newline at end of file Modified: trunk/1.0/tests/files/templates/content.shared.tmpl =================================================================== --- trunk/1.0/tests/files/templates/content.shared.tmpl 2009-09-14 15:21:16 UTC (rev 458) +++ trunk/1.0/tests/files/templates/content.shared.tmpl 2009-09-15 20:28:18 UTC (rev 459) @@ -6,3 +6,7 @@ <!-- END blockRow3 --> <!-- BEGIN blockRow4 -->{anotherInvisibleVar}<!-- END blockRow4 -->{invisibleTemplateVar} + + <!-- BEGIN blockRow5 -->{anotherInvisibleVar}<!-- BEGIN embeddedBlockRow1 -->{testEmbedded}<!-- END embeddedBlockRow1 --> + <!-- BEGIN embeddedBlockRow2 -->{testEmbedded2}<!-- END embeddedBlockRow2 --> + <!-- END blockRow5 --> Copied: trunk/1.0/tests/files/templates/system/404.shared.tmpl (from rev 458, trunk/1.0/sample_files/templates/system/404.shared.tmpl) =================================================================== --- trunk/1.0/tests/files/templates/system/404.shared.tmpl (rev 0) +++ trunk/1.0/tests/files/templates/system/404.shared.tmpl 2009-09-15 20:28:18 UTC (rev 459) @@ -0,0 +1,33 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + +<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US"> +<!-- + * Created on Jun 3, 2007 + * +--> + <head> + <title>Page Not Found</title> + <link rel="stylesheet" href="{APPURL}/css/site.css" type="text/css"> + </head> + <body> + + <table border=0 cellpadding=0 cellspacing=0 width="90%" align="center"> +<TR> + <td align="center"><!-- This row should span across the page. --><hr>{datetime}<hr></td> +</TR> + + <tr> + + <td> + + + {content} + + + + </td> + </tr> +</table> + </body> +</html> Copied: trunk/1.0/tests/files/templates/system/message_box.tmpl (from rev 458, trunk/1.0/sample_files/templates/system/message_box.tmpl) =================================================================== --- trunk/1.0/tests/files/templates/system/message_box.tmpl (rev 0) +++ trunk/1.0/tests/files/templates/system/message_box.tmpl 2009-09-15 20:28:18 UTC (rev 459) @@ -0,0 +1,29 @@ +<!-- *** MESSAGE_BOX START (/system/message_box.tmpl) *** --> +<table border="0" cellspacing="0" cellpadding="0" align="center"> + <tr> + <td class="{messageType}" width="5" align="left" valign="top"><img src="{APPURL}/images/frame/crn-white-tl.gif" width="5" height="5" alt=""></td> + <td class="{messageType}"><img src="{APPURL}/images/clear.gif" width="20" height="2" alt=""></td> + <td class="{messageType}" width="5" align="right" valign="top"><img src="{APPURL}/images/frame/crn-white-tr.gif" width="5" height="5" alt=""></td> + </tr> + <tr> + <td class="{messageType}" width="5"><img src="{APPURL}/images/clear.gif" width="5" height="20" alt=""></td> + <td> + <table class="{messageType}" width="100%" border="0" cellspacing="0" cellpadding="5"> + <tr> + <td valign="top"> + <p style="border-style: solid; border-width: 0px 0px 2px 0px" class="title1">{title}</p> +<p style="margin: 5px 0px 5px 0px">{message}</p> + {redirect} </td> + </tr> + </table></td> + <td class="{messageType}" width="5"><img src="{APPURL}/images/clear.gif" width="5" height="20" alt=""></td> + </tr> + <tr> + <td class="{messageType}" width="5" align="left" valign="bottom"><img src="{APPURL}/images/frame/crn-white-bl.gif" width="5" height="5" alt=""></td> + <td class="{messageType}"><img src="{APPURL}/images/clear.gif" width="20" height="2" alt=""></td> + <td class="{messageType}" width="5" align="right" valign="bottom"><img src="{APPURL}/images/frame/crn-white-br.gif" width="5" height="5" alt=""></td> + </tr> + </table> +<br> +<!-- *** MESSAGE_BOX END (/system/message_box.tmpl) *** --> + Modified: trunk/1.0/tests/testOfCSContent.php =================================================================== --- trunk/1.0/tests/testOfCSContent.php 2009-09-14 15:21:16 UTC (rev 458) +++ trunk/1.0/tests/testOfCSContent.php 2009-09-15 20:28:18 UTC (rev 459) @@ -102,8 +102,18 @@ function test_genericPage() { $filesDir = dirname(__FILE__) .'/files'; - $mainTemplate = $filesDir .'/templates/main.shared.tmpl'; - $page = new cs_genericPage(false, $mainTemplate); + $mainTemplateFullUrl = $filesDir .'/templates/main.shared.tmpl'; + $mainTemplate = 'main.shared.tmpl'; + + $page = new cs_genericPage(false, $mainTemplateFullUrl); + + //NOTE::: this test FAILS with cs_genericPage.class.php@455 (or any revision less than 455) + $this->assertEqual($mainTemplateFullUrl, $page->template_file_exists($mainTemplate)); + + $this->assertEqual($page->file_to_string($mainTemplate), file_get_contents($mainTemplateFullUrl)); + + + $fs = new cs_fileSystem($filesDir .'/templates'); $lsData = $fs->ls(); @@ -118,9 +128,7 @@ $checkThis = $page->return_printed_page(); - $this->assertEqual($checkThis, file_get_contents($filesDir .'/gptest_all-together.txt')); - $this->assertEqual($checkThis, $page->templateObj->varvals['out']); //now let's rip all the template rows out & add them back in. $rowDefs = $page->get_block_row_defs('content'); @@ -137,11 +145,10 @@ } $checkThis2 = $page->return_printed_page(); + //NOTE::: this test FAILS with cs_genericPage.class.php@455 (or any revision less than 455) $this->assertEqual($checkThis, $checkThis2); - $this->assertEqual($checkThis2, $page->templateObj->varvals['out']); $checkThis = $page->return_printed_page(0); - $this->assertTrue(preg_match('/\{.\S+?\}/', $page->templateObj->varvals['out'])); $this->assertTrue(preg_match('/\{.\S+?\}/', $checkThis)); //clone the page object so we can change stuff & not affect the original. @@ -169,9 +176,9 @@ } - //make sure stripping undefined vars works properly. + //make sure stripping undefined vars works properly (see issue #237) { - $page = new cs_genericPage(false, $mainTemplate); + $page = new cs_genericPage(false, $mainTemplateFullUrl); $this->assertEqual($fs->read($mainTemplate), $page->return_printed_page(0)); $this->assertNotEqual($fs->read($mainTemplate), $page->return_printed_page(1)); @@ -192,6 +199,49 @@ $this->gfObj->debug_print($page->unhandledVars); } } + + //Test if ripping out all the block rows works as intended (also related to issue #237) + { + $page = new cs_genericPage(false, $mainTemplateFullUrl); + $page->add_template_var('blockRowTestVal', 3); + + $fs = new cs_fileSystem($filesDir .'/templates'); + $lsData = $fs->ls(); + foreach($lsData as $index=>$value) { + $filenameBits = explode('.', $index); + $page->add_template_file($filenameBits[0], $index); + } + + $blockRows = $page->rip_all_block_rows('content'); + + //make sure printing the page multiple times doesn't change its output. + $this->assertEqual($page->return_printed_page(), $page->return_printed_page()); + $this->assertEqual($page->return_printed_page(), $page->return_printed_page()); + $this->assertEqual($page->return_printed_page(), $page->return_printed_page()); + + /* + * NOTE::: if this seems confusing, well... it is. Basically, the template var "{blockRowTestVal}" doesn't get + * parsed into the value of 3 until the call to print_page(), so therefore the block row "blockRow3" doesn't have + * a valid BEGIN statement until AFTER the page is built... ripping out that blockrow would have to be done after + * everything is all complete (i.e. by assigning the value of return_printed_page() to a template var) + */ + if(!$this->assertEqual(6, count($blockRows))) { + $this->gfObj->debug_print($blockRows); + } + + $this->assertEqual(file_get_contents($filesDir .'/gptest_blockrows.txt'), $page->return_printed_page()); + + $rasterizedData = $page->return_printed_page(); + $page->add_template_var('main', $page->return_printed_page()); + $this->assertEqual($rasterizedData, $page->return_printed_page()); + + $blockRows = $page->rip_all_block_rows('main'); + $this->assertEqual(1, count($blockRows)); + $this->assertTrue(isset($blockRows['blockRow3'])); + + $this->assertEqual(file_get_contents($filesDir .'/gptest_blockrows2.txt'), $page->return_printed_page()); + $this->assertNotEqual(file_get_contents($filesDir .'/gptest_blockrows2.txt'), file_get_contents($filesDir .'/gptest_blockrows.txt')); + } }//end test_genericPage //------------------------------------------------------------------------- This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |