1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5 e-commerce Development- P14 pot

5 152 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Planning Our Framework
Tác giả Michael Peacock
Trường học Unknown
Chuyên ngành E-commerce Development
Thể loại Thesis
Năm xuất bản 2010
Thành phố Brick
Định dạng
Số trang 5
Dung lượng 303,85 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

We may wish to convert them to template tags; this function facilitates that, and allows us to prefix the tags, which helps eliminate naming conflicts.. However, this needs to be inse

Trang 1

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 prefix the tags, which helps eliminate

naming conflicts.

/**

* 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 finished 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()

{

Trang 2

$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 finalized 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() { }

/**

Trang 3

* 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

*/

Trang 4

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()

{

Trang 5

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 files 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 confirm purchases, confirm

dispatches, and to inform customers when products they were interested in are

back in stock.

Ngày đăng: 07/07/2014, 10:20