phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP N-tier: Building Presentation GUI

Tag: n-tier, multi-tier, application architecture, application design, presentation, GUI   Category: PHP Application
post: 15 Mar 2008 read: 146


PHP N-Tier Strategy Step By Step Tutorial - Part 7: We have grabbed data from server at presentation logic. And we tie data to html at previous post. It will give us more flexible if we you template system. So, we can change any layout without change all. for this job, we use smarty template.

Exactly, what is smarty template? I rewrite from their readme: Smarty is a template engine for PHP. Many other template engines for PHP provide basic variable substitution and dynamic block functionality. Smarty takes a step further to be a "smart" template engine, adding features such as configuration files, template functions, and variable modifiers, and making all of this functionality as easy as possible to use for both programmers and template designers.

First, please download smarty template at smarty.php.net. Extract compressed file to www/test/ntier. You will get folder named like "smarty-2.6.xx". Rename, for simply, become: "smarty".

Then, create folders named "templates" and "templates_c" within www/test/ntier.

Open again your client_book.php, rewrite with this code:


<?
// call library
require_once ('./lib/nusoap.php');
require_once ('./smarty/libs/smarty.class.php');

// retrieve data from server
// use webservice by nusoap
$client = new soapclient('http://localhost:8048/test/ntier/dataacces.php');
$response = $client->call('books');

if($client->fault)
{
  echo "FAULT: <p>Code: (".$client->faultcode.")</p>";
  echo "String: ".$client->faultstring;
  die();
}
else
{
  // start using smarty  
  $smarty = new Smarty;
  
  // assign parameters
  $smarty->assign("title","Books Collection");
  $smarty->assign("books",$response[0]);
  
  //load template
  $smarty->display('template.tpl');
}
?>

Now, you can see. Your code be more clean. No html tag over there. All html tag will be place at template.

Create a file named "template.tpl" within www/test/ntier/templates. Then enter following code:


<html>
<head>
<title>{$title}</title>
</head>
<body>
  <table border="1">
    <tr>
      <th>Id</th>
      <th>Title</th>        
      <th>Author</th>
    </tr>
{foreach key=key item=item from=$books}
    <tr>
  {foreach key=key item=item from=$item}
    <td>{$item}</td>  
  {/foreach}  
    </tr>
{/foreach}
  </table>
</body>
</html>

About smarty code, we will talk later.

Congrat! you have understand how to build 5-tier use php. You can expand this tutorial as you need.




| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
CAPTCHA - part 3 : "Are you human or ....?" (Build Your CAPTCHA)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting