Surance Center

XOOPS模块开发快速入门中文翻译(六)

    由于这两天一直研究XOOPS的模块,所以找到了这篇很好的模块开发快速入门。
看了以后,就兴致勃勃的来开发模块了,可是开发的过程中遇到一些问题。
应该是我看的太快了,要学而时习之啊。因此翻译在这里。
==============
作者:Surance Yin
邮箱:Suranceyin@yahoo.com.cn
主页:http://www.fltek.com.cn
=================

第二章――使用Smarty 模板

这个教程讲了怎么在xoops模块中使用smarty模板和其他语句

首先,下载上一部分完成后的文件 part1. 并安装。

仔细看一下index.php,可以发现没有大量的html和php的代码混和。但是如果是很多php和html的话,代码就变得很难维护。用smarty就可以避免这个问题。用了Smarty,我们可以创建一个 html 模板, html是非常好维护的,当然,也可以用编辑器动态生成。 Xoops 使用Smarty的时候,有些特别。

创建一个xoop smarty 模板。打开 xoops_version.php. 增加以下代码:

<?php
// Tutorial Module
// Created by kaotik

$modversion['name'] = "Tutorial";
$modversion['version'] = 2.00;
$modversion['description'] = "This is a tutorial module to teach how to build a simple module";
$modversion['author'] = "KaotiK";
$modversion['credits'] = "KaotiK";
$modversion['help'] = "";
$modversion['license'] = "GPL see LICENSE";
$modversion['official'] = 0;
$modversion['image'] = "images/tutorial.png";
$modversion['dirname'] = "tutorial";

// Admin
$modversion['hasAdmin'] = 0;

// Menu
$modversion['hasMain'] = 1;

$modversion['sqlfile']['mysql'] = "sql/mysql.sql";
$modversion['tables'][0] = "tutorial_myform";

// Templates
$modversion['templates'][1]['file'] = 'tut_main.html';
$modversion['templates'][1]['description'] = '';

?

这是告诉xoops我们要创建一个叫做tut_main.html的模板. 现在在模块里面创建一个文件夹,名字为 templates. 再在这个文件夹中创建一个 html 文件,名为tut_main.html. (注意:所有xoops里面的模板文件都用 .html.保证这个文件里面没有被你的编辑器生成其他代码)。到模块管理界面,更新模块.可以发现 tut_main.html文件已经被xoops认可了. OK,我们进行下一步。

写一份模板文件

Smarty模板文件只是一些html和一些特殊的字符。在xoops里面,这些特殊字符 用<{}>包围。举例来说,为了使用xoops的url,我们可以在smarty模板中用<{$xoops_url}>

为了使这个快速入门更简单,我们用另外一种更简单的代码。重命名index.php 为 index_original.php. 创建一个新的index.php 文件,输入以下的代码:

<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
$xoopsOption['template_main'] = 'tut_main.html';
require(XOOPS_ROOT_PATH.'/header.php');

$xoopsTpl->assign('mytest', 'Hello World!');

require(XOOPS_ROOT_PATH.'/footer.php');
?>

这个代码有2行新的。第一行$xoopsOption 告诉xoops 这个页面要用哪个模板文件. 在这里是 tut_main.html.第二行定义了smarty使用的变量; 可以将php 变量转变为smarty变量.什么意思? 这表示在smarty中, 'mytest' 会被显示为 'Hello World!'. 现在打开/templates/tut_main.html输入以下代码: <{$mytest}>注意:如果你用的Dreamer一类所见即所得的软件,保证你是在code视图下写的在主菜单中点击“turial”,你会看到“hello world!”

很好!现在我们为页面加入了smarty。现在我们要用smarty实现第一章的所有功能。

在第一章,我们创建了一个4个字段的表,现在我们来在模板文件中创建表头。删除 <{$mytest}>,用以下代码替代

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><{$smarty.const.TT_NAME}></td>
<td><{$smarty.const.TT_ADDRESS}></td>
<td><{$smarty.const.TT_TELEPHONE}></td>
<td><{$smarty.const.TT_EMAIL}></td>
</tr>
</table>

不要忘记使用Code视图

现在如果在主菜单店家tutorial,会发现一个4列的表。注意到了吗,我使用的是$smarty.const 来获取/tutorial/language/english/main.php 里面定义的常量。还有一种办法来定义常量:$xoopsTpl->assign。但是我不推荐,这容易出问题。

数组,有什么好处?

打开index_original.php ,看一下用来列表的代码,有没有注意到,我定义了每一个列,用了 $name, $address,等变量,输出结果的时候用的是echo.用 smarty模板后,这个方法就不管用了。我们要用其他方法输出。将内容存在一个变量里面,然后用smarty输出。这里array就可以起作用了。我们要创建一个叫做$client的数组,用来存放所有的用户。打开index.php ,输入以下代码

<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
$xoopsOption['template_main'] = 'tut_main.html';
require(XOOPS_ROOT_PATH.'/header.php');

$clientdata=clientLoader();
$xoopsTpl->assign('client', $clientdata);

function clientLoader(){
global $xoopsDB;
$client=array();
$q=1;
$query = $xoopsDB->query(' SELECT * FROM ' . $xoopsDB->prefix('tutorial_myform'));
while($myrow = $xoopsDB->fetchArray($query) )
{
$client[$q]['name'] = $myrow['name'];
$client[$q]['address'] = $myrow['address'];
$client[$q]['telephone'] = $myrow['telephone'];
$client[$q]['email'] = $myrow['email'];
$q++;
}
return $client;
}

require(XOOPS_ROOT_PATH.'/footer.php');
?>


这里有2个新的东西:我用了一个函数。函数可以使代码看起来更简洁,我创建了一个函数clientLoader ,它去获取到所有的用户,并且返回一个数组。我来具体解释一下这个函数。


function clientLoader(){
global $xoopsDB;

第一行声明函数clientLoader. 第二行声明$xoopsDB 作为 global变量。如果你在页面中声明了一个$mytest,在函数中就无法调用了。所以要声明为global变量,从而在函数里面才可以用。

$client=array();
$q=1;

使用数组前声明一下数组是非常好的。第二行声明了一个辅助变量。后面会讲到。

$query = $xoopsDB->query(' SELECT * FROM ' . $xoopsDB->prefix('tutorial_myform'));
while($myrow = $xoopsDB->fetchArray($query) )
{
$client[$q]['name'] = $myrow['name'];
$client[$q]['address'] = $myrow['address'];
$client[$q]['telephone'] = $myrow['telephone'];
$client[$q]['email'] = $myrow['email'];
$q++;
}

第一行链接数据库。会每次一行的加载所有的客户信息到$client 。这也是为什么用 $q。首先 $q=1,第一行就是e $client[1]['name']='something', 等.$q每此循环加一,从而第二行就是$client[2].

return $client;
}

最后函数返回。$client.

 $clientdata=clientLoader();
$xoopsTpl->assign('client', $clientdata);

这里我们创建了一个变量 $clientdata  然后将他赋给smarty 中的变量 client. 重新安装模块,来看一下效果。.打开 tut_main.html输入:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><{$smarty.const.TT_NAME}></td>
<td><{$smarty.const.TT_ADDRESS}></td>
<td><{$smarty.const.TT_TELEPHONE}></td>
<td><{$smarty.const.TT_EMAIL}></td>
</tr>
<{foreach item=cli from=$client}>
<tr>
<td><{$cli.name}></td>
<td><{$cli.address}></td>
<td><{$cli.telephone}></td>
<td><{$cli.email}></td>
</tr>
<{/foreach}>
</table>

这里使用Foreach循环。当然这个实例很简单,不然我们可以使用CSS文件。在主菜单上进入该模块,我们可以看到第一章里面的用户列表。

表单和Smarty模板

我们来创建一个用来输入数据的表单。我们还要用第一章的代码。有2种方法实现,我们来用一个简单的:在xoops_version.php

<?php
// Tutorial Module
// Created by kaotik

$modversion['name'] = "Tutorial";
$modversion['version'] = 2.00;
$modversion['description'] = "This is a tutorial module to teach how to build a simple module";
$modversion['author'] = "KaotiK";
$modversion['credits'] = "KaotiK";
$modversion['help'] = "";
$modversion['license'] = "GPL see LICENSE";
$modversion['official'] = 0;
$modversion['image'] = "images/tutorial.png";
$modversion['dirname'] = "tutorial";

// Admin
$modversion['hasAdmin'] = 0;

// Menu
$modversion['hasMain'] = 1;

$modversion['sqlfile']['mysql'] = "sql/mysql.sql";
$modversion['tables'][0] = "tutorial_myform";

// Templates
$modversion['templates'][1]['file'] = 'tut_form.html';
$modversion['templates'][1]['description'] = '';

$modversion['templates'][2]['file'] = 'tut_main.html';
$modversion['templates'][2]['description'] = '';

?>

你一定注意到2个问题:我们的新模板是1,原来的main模板是2。原因是我要把tut_form.html放在tut_main.html里面,这种情况下,我们需要先声明这个模板。

现在进入templates文件夹,创建一个空白的 html 文件,叫做 tut_form.html. 然后进入系统的模块管理,更新该模块。打开tut_form.html ,加入以下代码:

<form name="tutorial_form" method="post" action="index.php">
<table width="400" border="0">
<tr>
<td align="right"><{$smarty.const.TT_NAME}></td>
<td><input type="text" name="name"></td>
</tr><tr>
<td align="right"><{$smarty.const.TT_ADDRESS}></td>
<td><input type="text" name="address"></td>
</tr><tr>
<td align="right"><{$smarty.const.TT_TELEPHONE}></td>
<td><input type="text" name="tel"></td>
</tr><tr>
<td align="right"><{$smarty.const.TT_EMAIL}></td>
<td><input type="text" name="email"></td>
</tr><tr>
<td><input type="submit" name="listall" value="List All"></td>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>

好,现在我们打开tut_main.html 在里面加一行。

<{include file="db:tut_form.html"}>

浏览一下这个模块的,可以看到一个表单。向tut_main.html  里面 输入以下代码

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><a href="index.php?addnew=1">Add new Client</a></td>
</tr>
</table>
<{if $addnew==1}>
<{include file="db:tut_form.html"}>
<{/if}>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><{$smarty.const.TT_NAME}></td>
<td><{$smarty.const.TT_ADDRESS}></td>
<td><{$smarty.const.TT_TELEPHONE}></td>
<td><{$smarty.const.TT_EMAIL}></td>
</tr>
<{foreach item=cli from=$client}>
<tr>
<td><{$cli.name}></td>
<td><{$cli.address}></td>
<td><{$cli.telephone}></td>
<td><{$cli.email}></td>
</tr>
<{/foreach}>
</table>

现在,我们有了一个叫做 Add new Client的超链接,用来增加一个用户。打开 index.php ,输入以下代码

<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
$xoopsOption['template_main'] = 'tut_main.html';
require(XOOPS_ROOT_PATH.'/header.php');

if (isset($_GET['addnew'])){
$xoopsTpl->assign('addnew', 1);
}

$clientdata=clientLoader();
$xoopsTpl->assign('client', $clientdata);

function clientLoader(){
global $xoopsDB;
$client=array();
$q=1;
$query = $xoopsDB->query(' SELECT * FROM ' . $xoopsDB->prefix('tutorial_myform'));
while($myrow = $xoopsDB->fetchArray($query) )
{
$client[$q]['name'] = $myrow['name'];
$client[$q]['address'] = $myrow['address'];
$client[$q]['telephone'] = $myrow['telephone'];
$client[$q]['email'] = $myrow['email'];
$q++;
}
return $client;
}

require(XOOPS_ROOT_PATH.'/footer.php');
?>

我用了if,这样可以看是否显示tut_form.html.

将index.php改为:

<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
$xoopsOption['template_main'] = 'tut_main.html';
require(XOOPS_ROOT_PATH.'/header.php');

if (isset($_GET['addnew'])){
$xoopsTpl->assign('addnew', 1);
}

if (isset($_POST['submit'])){
if (empty($_POST['name'])){
$xoopsTpl->assign('msg', "Please fill in a name");
} else {
$name=$_POST['name'];
$address=$_POST['address'];
$tel=$_POST['tel'];
$email=$_POST['email'];
$query = "Insert into ".$xoopsDB->prefix("tutorial_myform")." (name, address, telephone, email) values ('$name', '$address', '$tel', '$email' )";
$res=$xoopsDB->query($query);
if(!$res) {
$xoopsTpl->assign('msg', "error: $query");
} else {
$xoopsTpl->assign('msg', "Data was correctly inserted into DB!");
}
}
}

$clientdata=clientLoader();
$xoopsTpl->assign('client', $clientdata);

function clientLoader(){
global $xoopsDB;
$client=array();
$q=1;
$query = $xoopsDB->query(' SELECT * FROM ' . $xoopsDB->prefix('tutorial_myform'));
while($myrow = $xoopsDB->fetchArray($query) )
{
$client[$q]['name'] = $myrow['name'];
$client[$q]['address'] = $myrow['address'];
$client[$q]['telephone'] = $myrow['telephone'];
$client[$q]['email'] = $myrow['email'];
$q++;
}
return $client;
}

require(XOOPS_ROOT_PATH.'/footer.php');
?>

在第一章中已经解释过这些代码 ,只是现在把error放到一个smarty变量msg中.打开 tut_main.html 在第一行输入:

<{$msg}>
现在进入add new client链接,, 填写表单,并提交,我们就插入了一条数据!

还有一步,为了安全起见,我们要过滤一下用户输入的字符串。

<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
$xoopsOption['template_main'] = 'tut_main.html';
require(XOOPS_ROOT_PATH.'/header.php');

if (isset($_GET['addnew'])){
$xoopsTpl->assign('addnew', 1);
}

if (isset($_POST['submit'])){
if (empty($_POST['name'])){
echo 'please fill in a name';
} else {
$myts = myTextSanitizer::getInstance();
$name=$myts->addslashes($_POST['name']);
$address=$myts->addslashes($_POST['address']);
$tel=$myts->addslashes($_POST['tel']);
$email=$myts->addslashes($_POST['email']);

$query = "Insert into ".$xoopsDB->prefix("tutorial_myform")." (name, address, telephone, email) values ('$name', '$address', '$tel', '$email' )";
$res=$xoopsDB->query($query);
if(!$res) {
$xoopsTpl->assign('msg', "error: $query");
} else {
$xoopsTpl->assign('msg', "Data was correctly inserted into DB!");
}
}
}

$clientdata=clientLoader();
$xoopsTpl->assign('client', $clientdata);

function clientLoader(){
global $xoopsDB;
$client=array();
$q=1;
$query = $xoopsDB->query(' SELECT * FROM ' . $xoopsDB->prefix('tutorial_myform'));
while($myrow = $xoopsDB->fetchArray($query) )
{
$client[$q]['name'] = $myrow['name'];
$client[$q]['address'] = $myrow['address'];
$client[$q]['telephone'] = $myrow['telephone'];
$client[$q]['email'] = $myrow['email'];
$q++;
}
return $client;
}

require(XOOPS_ROOT_PATH.'/footer.php');
?>

现在,安全了
Part 3 - Building an AJAX module

这里有完整的实例下载


Tutorial module
tutorial

posted @ 2008-04-17 22:40  xxp  阅读(1068)  评论(1编辑  收藏  举报
Surance Center