- 浏览: 2943977 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (2529)
- finance (1459)
- technology (218)
- life (343)
- play (150)
- technology-component (0)
- idea (6)
- house (74)
- health (75)
- work (32)
- joke (23)
- blog (1)
- amazing (13)
- important (22)
- study (13)
- Alternative (0)
- funny (8)
- stock_technology (12)
- business (16)
- car (21)
- decorate (4)
- basketball (2)
- English (16)
- banker (1)
- TheBest (1)
- sample (2)
- love (13)
- management (4)
最新评论
-
zhongmin2012:
BSM确实需要实践,标准ITIL服务流程支持,要做好,需要花费 ...
BSM实施之前做什么 -
shw340518:
提示楼主,有时间逻辑bug:是你妈二十那年写的 那会儿连你爹都 ...
80后辣妈给未来儿子的信~我的儿,你也给我记住了~~~ -
guoapeng:
有相关的文档吗?
it项目管理表格(包含146个DOC文档模板) -
solomon:
看到的都是 这种 CTRL+C 和 CTRL+V 的文章, ...
Designing a website with InfoGlue components -
wendal:
恩, 不错. 有参考价值
Designing a website with InfoGlue components
http://www.hbcms.com/main/smarty/index.html
This is an example of a full working PHP application using Smarty. The purpose of this example is to demonstrate how Smarty ties in with an application and how to separate your presentation. The example given is a fairly simple yet complete mini-framework for building Smarty driven applications quickly and easily. Once you understand the concepts of presentation separation, you should be able to apply them to any type of programming pattern. With that said, you may use the following code at your own discretion, and at your own risk.
You can download the source of this Sample Application here.
This is not a guide on how to setup Apache, PEAR or MySQL. Be sure you know these things or have handy references to them. If you are using alternatives, you will need to make appropriate adjustments in the code.
We will be building a guestbook where users can sign it or view it. There is no administration interface. We will be covering a few programming topics that involve Smarty such as form processing and database data retrieval and display.
This example extends the guestbook application setup given in the installation guide for Smarty, so we'll build on top of that. Here are the files we'll start with for our app:
/web/www.example.com/docs/ /web/www.example.com/docs/guestbook/ /web/www.example.com/docs/guestbook/index.php /web/www.example.com/smarty/guestbook/ /web/www.example.com/smarty/guestbook/templates/ /web/www.example.com/smarty/guestbook/templates_c/ /web/www.example.com/smarty/guestbook/configs/ /web/www.example.com/smarty/guestbook/cache/ /web/www.example.com/smarty/guestbook/libs/ /web/www.example.com/smarty/guestbook/libs/guestbook_setup.php /web/www.example.com/smarty/guestbook/libs/guestbook.lib.php /web/www.example.com/smarty/guestbook/libs/sql.lib.php |
Lets go over each one:
/web/www.example.com/docs/ |
The /docs/ directory is our web server document root.
/web/www.example.com/docs/guestbook/ |
/guestbook/ is the subdirectory where our application is accessed by the browser.
/web/www.example.com/docs/guestbook/index.php |
index.php will be the entry point of our application. The web browser will be accessing this script directly via http://www.example.com/guestbook/index.php.
/web/www.example.com/smarty/guestbook/ |
This is the directory we will keep all files for our guestbook app that do not need to be under doc root. Whether you choose to keep files under doc root is up to you, but for this example we follow the practice of putting only files directly accessed by the browser there. You could also use Apache .htaccess or other web server means to stop direct access to application files under doc root.
/web/www.example.com/smarty/guestbook/templates/ |
This where we will put our Smarty template files.
/web/www.example.com/smarty/guestbook/templates_c/ |
This is where Smarty places its compiled template files. If you installed this correctly, the web server user running PHP has write access here. For most intents and purposes you can just ignore this directory.
/web/www.example.com/smarty/guestbook/configs/ |
This is where we keep config files for our application. Config files are a place to store information that you want accessible from either the templates or the application. These are not PHP scripts, they are text files parsed by the Smarty config file parser.
/web/www.example.com/smarty/guestbook/cache/ |
This is where Smarty puts its cache files. This directory is only used if Smarty caching features are enabled. If you installed this correctly, the web server user running PHP has write access here. Much the same as the compile directory, it can be ignored.
/web/www.example.com/smarty/guestbook/libs/ |
/libs/ is the directory we'll keep our main application files.
/web/www.example.com/smarty/guestbook/libs/guestbook_setup.php |
guestbook_setup.php is where we'll keep some basic initialization information for our application.
/web/www.example.com/smarty/guestbook/libs/guestbook.lib.php |
guestbook.lib.php is where we'll keep the bulk of our application logic.
/web/www.example.com/smarty/guestbook/libs/sql.lib.php |
sql.lib.php is where we keep our database access logic.
We'll start with index.php, the entry point of our application. This is the file directly accessed by the web browser.
<?php |
The index.php file acts as the application controller. It handles all incoming browser requests and directs what actions to take. It will define our application directories, include the setup script, and direct an action depending on the action value from the $_REQUEST super-global. We will have three basic actions: add when a user wants to add an entry to the guestbook, submit when a user submits an entry, and view when the user displays the guestbook. The default action is view.
<?php |
guestbook_setup.php is where we do some basic application configuration, such as our database and template configs. We will be using the PEAR::DB library available from PEAR. Be sure DB.php is in your include_path, or supply an absolute path to it. We will be using MySQL as our database, enter the appropriate dsn information for your database setup.
NOTE: If you get a runtime error similar to Call to undefined function: query(), it is likely that your $dsn information is incorrect. Check it twice, test your db connection.
We will be needing a basic database setup. The following is a script that you can dump directly into MySQL with mysql < guestbook.sql. Be sure you change the GRANT line with your database/user information.
CREATE DATABASE GUESTBOOK; USE GUESTBOOK; CREATE TABLE GUESTBOOK ( id int(11) NOT NULL auto_increment, Name varchar(255) NOT NULL default '', EntryDate datetime NOT NULL default '0000-00-00 00:00:00', Comment text NOT NULL, PRIMARY KEY (id), KEY EntryDate (EntryDate) ) TYPE=MyISAM; GRANT ALL ON GUESTBOOK.* to guestbook@localhost identified by 'foobar'; |
<?php |
sql.lib.php is our database wrapper class around PEAR::DB. It will help keep the database access syntax in our application to a minimum. You can just copy and paste the above code, don't worry too much about understanding it unless you feel inclined. Here is a crash course on the usage:
$guestbook->sql->query("select * from GUESTBOOK", SQL_ALL); print_r($guestbook->sql->record); |
Array ( [0] => Array ( [0] => 1 [1] => Monte [2] => 2005-03-12 17:23:32 [3] => test entry 1 ) [1] => Array ( [0] => 2 [1] => Monte [2] => 2005-03-12 17:23:33 [3] => test entry 2 ) [2] => Array ( [0] => 3 [1] => Monte [2] => 2005-03-12 17:23:35 [3] => test entry 3 ) ) |
All of the guestbook entries are shown. SQL_ALL will get all of the query records.
$guestbook->sql->query("select * from GUESTBOOK"); while($guestbook->sql->next()) { print_r($guestbook->sql->record); } |
Array ( [0] => 1 [1] => Monte [2] => 2005-03-12 17:23:32 [3] => test entry 1 ) Array ( [0] => 2 [1] => Monte [2] => 2005-03-12 17:23:33 [3] => test entry 2 ) Array ( [0] => 3 [1] => Monte [2] => 2005-03-12 17:23:35 [3] => test entry 3 ) |
This loops over the records one by one. If no second parameter is supplied to query(), then the resulting records are looped over with next().
$guestbook->sql->query("select * from GUESTBOOK", SQL_INIT); print_r($guestbook->sql->record); |
Array ( [0] => 1 [1] => Monte [2] => 2005-03-12 17:23:32 [3] => test entry 1 ) |
This outputs only one record (the first one). SQL_INIT will get one record only.
$guestbook->sql->query("select * from GUESTBOOK", SQL_INIT, SQL_ASSOC); print_r($guestbook->sql->record); |
Array ( [id] => 1 [Name] => Monte [EntryDate] => 2005-03-12 17:23:32 [Comment] => test entry 1 ) |
Passing a third parameter of SQL_ASSOC to query() will return the results as an associative array: fieldname => value.
$guestbook->sql->query("select * from GUESTBOOK"); while($guestbook->sql->next(SQL_ASSOC)) { print_r($guestbook->sql->record); } |
Array ( [id] => 1 [Name] => Monte [EntryDate] => 2005-03-12 17:23:32 [Comment] => test entry 1 ) Array ( [id] => 2 [Name] => Monte [EntryDate] => 2005-03-12 17:23:33 [Comment] => test entry 2 ) Array ( [id] => 3 [Name] => Monte [EntryDate] => 2005-03-12 17:23:35 [Comment] => test entry 3 ) |
Passing SQL_ASSOC as a parameter to next() will also return results as an associative array.
<?php |
guestbook.lib.php is our application class. It contains the main logic for our entire application. Lets look at each class method.
/** * class constructor */ function Guestbook() { // instantiate the sql object $this->sql =& new GuestBook_SQL; // instantiate the template object $this->tpl =& new Guestbook_Smarty; } |
This is the class constructor. It is executed each time we instantiate the guestbook object. It instantiates the SQL and Smarty objects as properties. We can then access them from within our object methods.
/** * display the guestbook entry form * * @param array $formvars the form variables */ function displayForm($formvars = array()) { // assign the form vars $this->tpl->assign('post',$formvars); // assign error message $this->tpl->assign('error', $this->error); $this->tpl->display('guestbook_form.tpl'); } |
The displayForm() method is used for displaying the guestbook entry form. It assigns the form variables and the form validation error message to the template, then displays the form.
/** * fix up form data if necessary * * @param array $formvars the form variables */ function mungeFormData(&$formvars) { // trim off excess whitespace $formvars['Name'] = trim($formvars['Name']); $formvars['Comment'] = trim($formvars['Comment']); } |
The mungeFormData() method trims off whitespace from the form input. This is called prior to form validation. Notice the form data is passed into the method by reference so the changes will affect the original array.
/** * test if form information is valid * * @param array $formvars the form variables */ function isValidForm($formvars) { // reset error message $this->error = null; // test if "Name" is empty if(strlen($formvars['Name']) == 0) { $this->error = 'name_empty'; return false; } // test if "Comment" is empty if(strlen($formvars['Comment']) == 0) { $this->error = 'comment_empty'; return false; } // form passed validation return true; } |
The method isValidForm() validates the form input. This is a simple test to see if the Name or Comment was empty. If so, the appropriate error code is assigned to the error property. (These error codes are used by the template later on.)
/** * add a new guestbook entry * * @param array $formvars the form variables */ function addEntry($formvars) { $_query = sprintf( "insert into GUESTBOOK values(0,'%s',NOW(),'%s')", mysql_escape_string($formvars['Name']), mysql_escape_string($formvars['Comment']) ); return $this->sql->query($_query); } |
The addEntry method enters a new guestbook entry into the database. Notice the values are escaped to avoid SQL syntax errors or injection attacks.
/** * get the guestbook entries */ function getEntries() { $this->sql->query( "select * from GUESTBOOK order by EntryDate", SQL_ALL, SQL_ASSOC ); return $this->sql->record; } |
The method getEntries() gets all the guestbook entries from the database in field => value format (SQL_ASSOC).
/** * display the guestbook * * @param array $data the guestbook data */ function displayBook($data = array()) { $this->tpl->assign('data', $data); $this->tpl->display('guestbook.tpl'); } |
The method displayBook() displays the guestbook entries. The $data array is expected to be an array of the guestbook entries. This is assigned to the template and then the template is displayed.
We have two templates for our guestbook, one for viewing and one for adding a new entry.
{* Smarty *} <table border="0" width="300"> <tr> <th colspan="2" bgcolor="#d1d1d1">Guestbook Entries (<a href="{$SCRIPT_NAME}?action=add">add</a>)</th> </tr> {foreach from=$data item="entry"} <tr bgcolor="{cycle values="#dedede,#eeeeee" advance=false}"> <td>{$entry.Name|escape}</td> <td align="right">{$entry.EntryDate|date_format:"%e %b, %Y %H:%M:%S"}</td> </tr> <tr> <td colspan="2" bgcolor="{cycle values="#dedede,#eeeeee"}">{$entry.Comment|escape}</td> </tr> {foreachelse} <tr> <td colspan="2">No records</td> </tr> {/foreach} </table> |
guestbook.tpl is the template for viewing the guestbook. It loops over the guestbook data (which was assigned from displayBook()) in a foreach loop and displays the Name, Date and Comment from each entry. The date is formatted with the date_format modifier. The Name and Comment are HTML-escaped using the escape modifier to avoid any HTML tag clashes or scripting attacks. The {cycle} function is used to cycle through background colors every two table rows.
{* Smarty *} <form action="{$SCRIPT_NAME}?action=submit" method="post"> <table border="1"> {if $error ne ""} <tr> <td bgcolor="yellow" colspan="2"> {if $error eq "name_empty"}You must supply a name. {elseif $error eq "comment_empty"} You must supply a comment. {/if} </td> </tr> {/if} <tr> <td>Name:</td> <td><input type="text" name="Name" value="{$post.Name|escape}" size="40"></td> </tr> <tr> <td valign="top">Comment:</td> <td><textarea name="Comment" cols="40" rows="10">{$post.Comment|escape}</textarea></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Submit"></td> </tr> </table> </form> |
guestbook_form.tpl is the template for adding an entry to the guestbook. If the form is being redisplayed due to a validation error, the form values are repopulated and the appropriate error message is displayed. The form values are HTML-escaped so there are no HTML tag or quote character clashes (very important!)
With this sample application we have accomplished several key aspects of a Smarty driven application.
* All presentation elements are contained in the template. We don't assign HTML tags or other presentation elements from outside the template. The only thing we assign is the page content, in this case the guestbook entries.
* Error messages are also maintained from the template. We don't assign error messages themselves, but error codes which are used to determine which error message to display. An alternative way to maintain error messages are from within Smarty config files, where you can have error_code = Error Message in the config file, then displayed with {$smarty.config.$error_code}
* PHP objects are used extensively to show their usefulness for easily passing information around (such as sql/template objects and error codes) avoiding procedural functions and clunky parameter passing.
Hopefully this gives you an idea how to setup your applications to work with Smarty in a way that cleanly separates the application from its presentation.
发表评论
-
New Enterprise Security Solutions
2011-09-13 15:46 0<!-- [if !mso]> <styl ... -
ES Announces Enterprise Security Solutions
2011-09-13 15:40 0<!-- [if !mso]> <styl ... -
linux下如何将文件打包、压缩并分割成制定大小?
2010-09-15 18:52 3310将大文件或目录打包、 ... -
rhel4 yum安装, 使用
2010-09-07 16:37 0第一种方法: yum源来自chinalinuxpub.com ... -
Windows: 远程自动安装程序
2010-08-26 15:48 1079问题的提出 作为 ... -
Oracle体系结构
2010-08-07 09:53 1010Oracle体系结构 Oracle Server包括Oracl ... -
ocp sesson 3
2010-07-31 14:39 0show parameter undo 只有 默认情况下服务 ... -
ocp session 2
2010-07-25 17:00 0/home/oracle/raInventory/orains ... -
ocp session 1
2010-07-24 13:02 0ocp first lesson D:\oracle_cou ... -
Python的xmlrpc调试
2010-07-19 23:55 2104Python的xmlrpc 调 试 ----------- ... -
mdadm使用详解及RAID 5简单分析
2010-07-11 16:19 1383http://blog.csdn.net/chinalinux ... -
Linux的lvm的基本配置步骤
2010-07-11 14:53 12791.增加硬件 增加的ide硬盘前缀为hd,scs ... -
OCP study material
2010-07-11 13:52 0\\192.168.1.105watch -n 1 'stat ... -
apache+python+mod_python+django 编译安装指南
2010-06-24 17:25 14691、本文将知道你在 linux 下使用源码包安装 ... -
在ubuntu下配置apache运行python脚本
2010-06-22 16:11 2266常用的简单命令 sudo apt ... -
Python 2.5 Quick Reference
2010-06-21 11:18 1459... -
shell 面试题汇集
2010-06-10 19:50 1041利用 top 取某个进程的 CPU 的脚本 : ... -
shell程序面试题
2010-06-10 19:48 28721.要求分析Apache访问日志,找出里面数量在前面100位的 ... -
EMC技术支持工程师笔试部分试题回忆
2010-06-07 15:16 1642要查看更多EMC公司笔经相关信息,请访问EMC公司校园招聘CL ... -
linux shell 条件语句
2010-06-03 23:29 1775...
相关推荐
Grav Guestbook插件 用于的Guestbook插件增加了添加可以接收访客消息的Guestbook页面的功能。 安装 Guestbook插件易于通过GPM安装。 $ bin/gpm install guestbook 或从GitHub克隆并放入user/plugins/guestbook...
《JavaWeb留言本项目——深度解析guestbook.zip》 在JavaWeb开发中,"guestbook.zip"是一个典型的实例,它涵盖了用户操作的核心功能以及Servlet技术的运用。这个项目旨在实现一个基本的在线留言本,提供了用户对...
GuestBook示例程序源码 ASP.NET Mvc + NHibernate + Unity Application Block 示例程序 今天无意间看到了Unity Application Block (以下简称Unity),感觉很不错,用起来挺方便的说。于是一时兴起写了这个留言本...
对于 "inf_guestbook_v4.12" 这个文件,这可能是 Guestbook 模块的一个特定版本,或者是针对该模块的一个漏洞利用工具或者补丁。如果这是一个漏洞利用工具,那么它可能包含了可以测试或演示 Guestbook 模块中特定...
本程序由ASP.NET与ASP写成 文章管理系统使用剑飞4.0管理系统 本程序正常运行需要服务器ASPX、ASP 文章管理后台地址:/...修改诊断系统读取数据库配置路径:guestbook/connections/cong.asp里的/guestbook/#1.mdb
在互联网的早期,GuestBook(访客簿)是一种常见的网站互动形式,它允许访问者留下他们的留言和联系方式。本文将深入探讨一个名为"Ex-guestbook.rar"的PHP GuestBook应用程序,帮助读者理解其工作原理、核心功能以及...
GuestBook是互联网上一种常见的交互功能,允许访客在网站上留下他们的评论、建议或者反馈,从而促进用户与网站之间的互动。 1. **PHP编程语言**:PHP是一种广泛使用的开源脚本语言,特别适合于Web开发。在这个...
Java Guestbook 源码分析与学习指南 Java Guestbook 是一个基于 Java 语言开发的简单留言板应用,它提供了一个用户交互的界面,允许访客留下他们的信息并查看其他人的留言。这个源码实例对于初学者来说是一个很好的...
欢迎来到您的CDK TypeScript项目! 这是使用CDK进行TypeScript开发的空白项目。 cdk.json文件告诉CDK Toolkit如何执行您的应用程序。 有用的命令 npm run build编译打字稿到js npm run watch监视更改并编译 ...
《EXT GuestBook:基于EXT UI引擎的留言板系统详解》 EXT GuestBook是一个专为EXT初学者设计的墙式留言板系统,其核心特点是采用EXT作为用户界面(UI)引擎,为用户提供了一个直观、交互性强的留言体验。EXT是一个...
《HyperBook Guestbook 汉化版》是一款适用于IT行业的开源留言本系统,它已经被精心地翻译成中文,便于国内用户使用。这个系统基于Web技术,可以为网站提供一个互动的平台,让访客能够留下他们的反馈、建议或交流...
《guestbook1》资源集合是作者三年来在IT行业,特别是软件项目实施过程中积累的宝贵经验的结晶。这个集合主要涵盖了C#、ASP.NET、SQL以及DBA(数据库管理员)等相关领域的知识,其中包含了源码文件,对于学习和理解...
在本文档中,我们将深入探讨"guestbook程序代码",这是一个基于Axeon平台的Servlet与JSP技术实现的Web应用示例。Servlet与JSP(JavaServer Pages)是Java Web开发中的核心技术,它们结合了服务器端的Java编程能力和...
【标题】:“guestbook留言板程序” 【描述】:“这是一个基于JSP技术编写的留言板程序,旨在为用户提供一个在线交流的平台。通过这个程序,用户可以发表留言、查看他人留言,从而实现互动沟通。” 【标签】:...
【标题】"appengine-guestbook-python-part1-helloworld.zip_GuestBook" 指的是一款基于Google App Engine平台开发的Python GuestBook应用的第一部分,主要用于实现一个简单的在线留言簿功能。Google App Engine是一...
在这个名为“asp_留言簿guestbook”的项目中,我们很显然是在处理一个基于ASP技术构建的在线留言簿应用。 留言簿是一个常见的Web应用程序,用户可以在此留下他们的信息、意见或者反馈。ASP可以处理这些输入,并将...
标题 "archive_一个不错的JAVA guestbook.zip.zip" 暗示了这是一个包含Java编程语言实现的Guestbook(访客簿)项目的压缩文件。这个项目可能是用于教学、实践或展示目的,它提供了一个基本的交互式应用程序,允许...
标题 "一个不错的JAVA guestbook" 暗示我们正在探讨一个用Java编程语言实现的简易留言簿应用。这个应用可能包含用户交互、数据存储以及服务器端处理等功能。描述中的 "一个不错的JAVA guestbook" 重复了标题信息,...
《Guestbook1.zip:探索与理解》 "guestbook1.zip"这个压缩包文件,从其名字来看,可能是一个包含了“客册”或“留言簿”数据的集合。在信息技术领域,这样的文件往往用于存储用户在网站、论坛或特定应用程序中的...
【标题】:“guestbook简易留言板下载” 在网页设计与开发中,一个简单的“guestbook”(留言板)系统是初学者和专业人士都会遇到的基础项目。它主要用于访客在网站上留下他们的评论、建议或反馈,无需注册即可直接...