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

PHP 5 e-commerce Development- P14 pot

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 (303.85 KB, 5 trang )

Planning our Framework
[ 48 ]
This is particularly useful when working in the model or controller with a single
row of results from a query. We may wish to convert them to template tags; this
function facilitates that, and allows us to prex the tags, which helps eliminate
naming conicts.
/**
* Convert an array of data into some tags
* @param array the data
* @param string a prefix which is added to field name to create
* the tag name
* @return void
*/
public function dataToTags( $data, $prefix )
{
foreach( $data as $key => $content )
{
$this->page->addTag( $key.$prefix, $content);
}
}
We set the title of a page directly to the page object. However, this needs to be
inserted directly into the template, as this function takes the title value and inserts
it between the title tags within the template itself.
/**
* Take the title we set in the page object, and insert them
* into the view
*/
public function parseTitle()
{
$newContent = str_replace('<title>', '<title>'
. $this->page->getTitle(), $this->page->getContent() );


$this->page->setContent( $newContent );
}
Finally, when we have nished assigning template variables, new templates need
to be inserted into a template and so on. We need to call the various replace and
parse functions. This method consolidates these calls, so we simply call it from our
framework. Our completed view is now ready to be sent to the user's browser.
/**
* Parse the page object into some output
* @return void
*/
public function parseOutput()
{
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
[ 49 ]
$this->replaceBits();
$this->replaceTags();
$this->parseTitle();
}
}
?>
Let's take a look at the code for page.class.php.
Our page object makes it easier to encapsulate the data and templates that we have
compiled during the framework's execution to create the appropriate nalized view
for the customer. It stores information such as the title of the page, the template
variables and their corresponding data values, the contents of various templates,
and any template bits we wish to insert into the others.
<?php
/**

* Page object for our template manager
*
* @author Michael Peacock
* @version 1.0
*/
class page {
// page elements

// page title
private $title = '';
// template tags
private $tags = array();
// tags which should be processed after the page has been parsed
// reason: what if there are template tags within the database
// content, we must parse the page, then parse it again for post
// parse tags
private $postParseTags = array();
// template bits
private $bits = array();
// the page content
private $content = "";

/**
* Create our page object
*/
function __construct() { }

/**
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
[ 50 ]
* Get the page title from the page
* @return String
*/
public function getTitle()
{
return $this->title;
}

/**
* Set the page title
* @param String $title the page title
* @return void
*/
public function setTitle( $title )
{
$this->title = $title;
}

/**
* Set the page content
* @param String $content the page content
* @return void
*/
public function setContent( $content )
{
$this->content = $content;
}


/**
* Add a template tag, and its replacement value/data to the page
* @param String $key the key to store within the tags array
* @param String $data the replacement data (may also be an
* array)
* @return void
*/
public function addTag( $key, $data )
{
$this->tags[$key] = $data;
}

/**
* Get tags associated with the page
* @return void
*/
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
[ 51 ]
public function getTags()
{
return $this->tags;
}

/**
* Add post parse tags: as per adding tags
* @param String $key the key to store within the array
* @param String $data the replacement data
* @return void

*/
public function addPPTag( $key, $data )
{
$this->postParseTags[$key] = $data;
}

/**
* Get tags to be parsed after the first batch have been parsed
* @return array
*/
public function getPPTags()
{
return $this->postParseTags;
}

/**
* Add a template bit to the page, doesnt actually add
* the content just yet
* @param String the tag where the template is added
* @param String the template file name
* @return void
*/
public function addTemplateBit( $tag, $bit )
{
$this->bits[ $tag ] = $bit;
}

/**
* Get the template bits to be entered into the page
* @return array the array of template tags and template

* file names
*/
public function getBits()
{
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
[ 52 ]
return $this->bits;
}

/**
* Gets a chunk of page content
* @param String the tag wrapping the block
* ( <! START tag > block <! END tag > )
* @return String the block of content
*/
public function getBlock( $tag )
{
preg_match ('#<! START '. $tag . ' >(.+?)
<! END '. $tag . ' >#si', $this->content, $tor);
$tor = str_replace ('<! START '. $tag . ' >', "", $tor[0]);
$tor = str_replace ('<! END ' . $tag . ' >', "", $tor);
return $tor;
}

public function getContent()
{
return $this->content;
}


}
?>
Extending the template management object
We could extend our template management object and page object to make things
easier or more powerful should we wish. Some examples include:
Making it possible to password protect pages
Restricting access to pages based on permissions
Making it easy to add CSS and JavaScript les into the page
Easily adding some onLoad JavaScript to the page's <body> tag
E-mail sending
Most sites require the need to send e-mails. This is even more so the case with
e-commerce sites, as they will need to send e-mails to conrm purchases, conrm
dispatches, and to inform customers when products they were interested in are
back in stock.




This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010
953 Quincy Drive, , Brick, , 08724

×