Tải bản đầy đủ (.pdf) (5 trang)

PHP 5 e-commerce Development- P13 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (241.14 KB, 5 trang )

Chapter 2
[ 43 ]
Our constructor includes the page class, and creates a new page object that is stored
within the page variable within the template object. This allows the template object
to manipulate the page as required by the framework.
private $page;

/**
* Include our page class, and build a page object to manage
* the content and structure of the page
*/
public function __construct()
{
include( FRAMEWORK_PATH . '/registry/objects/page.class.php');
$this->page = new Page();
}
In some cases, we may wish to insert one template into another. For instance, we
may wish to display a shopping basket summary on every page. However, if there is
nothing in the basket, we may wish to insert a different template, or just some text.
This method takes a template and places it into another template when it nds the
appropriate template tag.
/**
* Add a template bit from a view to our page
* @param String $tag the tag where we insert the template
* e.g. {hello}
* @param String $bit the template bit (path to file,
* or just the filename)
* @return void
*/
public function addTemplateBit( $tag, $bit )
{


if( strpos( $bit, 'views/' ) === false )
{
$bit = 'views/'
. PHPEcommerceFrameworkRegistry::getSetting('view')
. '/templates/' . $bit;
}
$this->page->addTemplateBit( $tag, $bit );
}
This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010
953 Quincy Drive, , Brick, , 08724
Download at Wow! eBook
Planning our Framework
[ 44 ]
To make it easier to add multiple templates into another template, we can add the
templates and the template tags to an array, which is then iterated through when the
templates are parsed.
/**
* Take the template bits from the view and insert them into
* our page content
* Updates the pages content
* @return void
*/
private function replaceBits()
{
$bits = $this->page->getBits();
// loop through template bits e.g.
foreach( $bits as $tag => $template )
{
$templateContent = file_get_contents( $template );
$newContent = str_replace( '{' . $tag . '}',

$templateContent, $this->page->getContent() );
$this->page->setContent( $newContent );
}
}
As we will also want to insert dynamically-generated data into our templates,
we need a function to do that—this replaces all of the template tags with the values
we wish to be associated with them. It also checks to see if a template variable is
data, or if it is a cached query (or cached data) and if so, replaces the tag with
results from a query.
/**
* Replace tags in our page with content
* @return void
*/
private function replaceTags()
{
// get the tags in the page
$tags = $this->page->getTags();
// go through them all
foreach( $tags as $tag => $data )
{
// if the tag is an array, then we need to do more than a
// simple find and replace!
if( is_array( $data ) )
{
if( $data[0] == 'SQL' )
This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010
953 Quincy Drive, , Brick, , 08724
Chapter 2
[ 45 ]
{

// it is a cached query replace tags from the database
$this->replaceDBTags( $tag, $data[1] );
}
elseif( $data[0] == 'DATA' )
{
// it is some cached data replace tags from cached data
$this->replaceDataTags( $tag, $data[1] );
}
}
else
{
// replace the content
$newContent = str_replace( '{' . $tag . '}', $data,
$this->page->getContent() );
// update the pages content
$this->page->setContent( $newContent );
}
}
}
If we have cached the results of a database query and assigned them to a template
variable, we need to replace the relevant template variables with the results of
the query. The following function does this, repeating the contents of <! START
template_tag > and <! END template_tag > for however many records
there are for that query, and then replacing tags within that block of code, with the
results of the query.
/**
* Replace content on the page with data from the database
* @param String $tag the tag defining the area of content
* @param int $cacheId the queries ID in the query cache
* @return void

*/
private function replaceDBTags( $tag, $cacheId )
{
$block = '';
$blockOld = $this->page->getBlock( $tag );
// foreach record relating to the query
while ($tags = PHPEcommerceFrameworkRegistry::getObject('db')->
resultsFromCache( $cacheId ) )
{
$blockNew = $blockOld;
// create a new block of content with the results replaced
// into it
This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010
953 Quincy Drive, , Brick, , 08724
Planning our Framework
[ 46 ]
foreach ($tags as $ntag => $data)
{
$blockNew = str_replace("{" . $ntag . "}", $data, $blockNew);
}
$block .= $blockNew;
}
$pageContent = $this->page->getContent();
// remove the seperator in the template, cleaner HTML
$newContent = str_replace( '<! START ' . $tag . ' >'
. $blockOld . '<! END ' . $tag . ' >', $block,
$pageContent );
// update the page content
$this->page->setContent( $newContent );
}

Here we do the same as replaceDBTags, only with data we have cached (as opposed
to a query).
/**
* Replace content on the page with data from the cache
* @param String $tag the tag defining the area of content
* @param int $cacheId the data's ID in the data cache
* @return void
*/
private function replaceDataTags( $tag, $cacheId )
{
$block = $this->page->getBlock( $tag );
$blockOld = $block;
while ($tags = PHPEcommerceFrameworkRegistry::getObject('db')-
>dataFromCache( $cacheId ) )
{
foreach ($tags as $tag => $data)
{
$blockNew = $blockOld;
$blockNew = str_replace("{" . $tag . "}", $data, $blockNew);
}
$block .= $blockNew;
}
$pageContent = $this->page->getContent();
$newContent = str_replace( $blockOld, $block, $pageContent );
$this->page->setContent( $newContent );
}
This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010
953 Quincy Drive, , Brick, , 08724
Chapter 2
[ 47 ]

This returns the page object, so we can directly call public methods from within it, if
we should need to from outside the scope of the template object.
/**
* Get the page object
* @return Object
*/
public function getPage()
{
return $this->page;
}
Our page is comprised of a number of templates. So generally, this is the rst thing
we need to do for a new view: we build the view from a number of templates. The
templates must be passed, in the correct order, to the method (that is, header, main
content, and footer) for them to be appropriately displayed.
/**
* Set the content of the page based on a number of templates
* pass template file locations as individual arguments
* @return void
*/
public function buildFromTemplates()
{
$bits = func_get_args();
$content = "";
foreach( $bits as $bit )
{
if( strpos( $bit, 'views/' ) === false )
{
$bit = 'views/'
. PHPEcommerceFrameworkRegistry::getSetting('view')
. '/templates/' . $bit;

}
if( file_exists( $bit ) == true )
{
$content .= file_get_contents( $bit );
}

}
$this->page->setContent( $content );
}
This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010
953 Quincy Drive, , Brick, , 08724

×