三、Dwoo语法讲解
下面以实例的形式讲解下Dwoo的语法,先来看最常用的if语句。
1) if 语句
下面是一个模版的例子:
- <html>
- <head></head>
- <body>
- {if $auth == 0}
- Not logged in
- {else}
- Logged in as: Anonymous User
- {/if}
- </body>
- </html>
可以看到,Dwoo中的if语句其实跟普通的if语句没什么区别。接下来我们看下控制这个模版的php文件,如下:
- <?php
- include 'dwooAutoload.php';
- try {
- $dwoo = new Dwoo();
- $tpl = new Dwoo_Template_File('tmpl/auth.tpl');
- $data = new Dwoo_Data();
- $data->assign('auth', rand(0,1));
- $dwoo->output($tpl, $data);
- } catch (Exception $e) {
- echo "Error: " . $e->getMessage();
- }
- ?>
注意,这里我们使用了new Dwoo_Data();这个Dwoo_Data()方法的优势在于,它比较容易存放大量的数据,比用数组的方法去存储数据方便多了,而且它本身提供了很多不同的方法去获得,清理和删除模版变量。这个例子中,用随机数的方法产生了auth变量,结果可能为如下图:
当然,可以使用if elseif语句,比如模版中:
- <html>
- <head></head>
- <body>
- {if $auth == 1}
- Logged in as: Anonymous User
- {elseif $auth == 2}
- Logged in as: Administrator
- {else}
- Not logged in
- {/if}
- </body>
- </html>
2) LOOP循环语句
在Dwoo中,可以使用{loop}进行循环,动态产生数据,下面是例子:
- <html>
- <head></head>
- <body>
- <ul>
- {loop $items}
- <li>{escape($item)}</li>
- {/loop}
- </ul>
- </body>
- </html>
下面是产生数据的php文件:
- <?php
- include 'dwooAutoload.php';
- try {
- $dwoo = new Dwoo();
- $tpl = new Dwoo_Template_File('tmpl/list.tpl');
- $data = new Dwoo_Data();
- $items = array();
- $items[] = array('item' => 'red');
- $items[] = array('item' => 'yellow');
- $items[] = array('item' => 'blue');
- $items[] = array('item' => 'green');
- $data->assign('items', $items);
- $dwoo->output($tpl, $data);
- } catch (Exception $e) {
- echo "Error: " . $e->getMessage();
- }
- ?>
这里,我们生成了数组items,然后在模版文件中,通过{loop $items}即可循环输出内容。结果如下图:
注意,这里使用了{escape($item)}的方法输出每一行的内容,其中eascape是dwoo中使用的插件,是将所有内容在输出前使用HTML编码格式过滤,这可以防止XSS攻击,是个很好的实践。
而在Dwoo中,可以同样使用{foreach}而达到同样的效果,代码如下:
- <html>
- <head></head>
- <body>
- <ul>
- {foreach $items item}
- <li>{escape($item)}</li>
- {/foreach}
- </ul>
- </body>
- </html>
同样,foreach也可以使用如下的用法,即:
- <html>
- <head></head>
- <body>
- <ul>
- {foreach $items key value}
- <li>{upper($key)} is for {$value}</li>
- {/foreach}
- </ul>
- </body>
- </html>
而配合这个模版,PHP的控制页面中的关联数组的写法如下:
- $data = new Dwoo_Data();
- $items = array(
- 'a' => 'apple',
- 'b' => 'ball',
- 'c' => 'cat',
- 'd' => 'dog'
- );
- $data->assign('items', $items);
这样输出结果如下图:
我们既然学会了loop,下面来尝试下从数据库中取出数据集,并通过Dwoo显示出来,下面是模版文件的主要部分:
- <body>
- <table>
- <tr class="heading">
- <td>Author</td>
- <td>Title</td>
- </tr>
- {loop $records}
- <tr>
- <td>{$author}</td>
- <td>{$title}</td>
- </tr>
- {/loop}
- </table>
- </body>
而PHP文件代码如下,其中使用了PDO去访问数据库:
- <? php
- include 'dwooAutoload.php';
- // 连接数据库
- try {
- $dbh = new PDO('mysql:dbname=library;host=localhost', 'user', 'pass');
- } catch (PDOException $e) {
- echo "Error: Could not connect. " . $e->getMessage();
- }
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- try {
- $sql = "SELECT a.AuthorName AS author, t.TitleName AS title FROM author AS a, title AS t, author_title AS at WHERE a.AuthorID = at.AuthorID AND t.TitleID = at.TitleID ORDER BY author LIMIT 0,20";
- $sth = $dbh->query($sql);
- while ($row = $sth->fetchObject()) {
- $records[] = array('author' => $row->author, 'title' => $row->title);
- }
- //关闭数据库连接
- unset($dbh);
- $dwoo = new Dwoo();
- $tpl = new Dwoo_Template_File('tmpl/books.tpl');
- $data = new Dwoo_Data();
- $data->assign('records', $records);
- $dwoo->output($tpl, $data);
- } catch (PDOException $e) {
- echo "Error: Could not execute query \"$sql\". " . $e->getMessage();
- unset($dbh);
- } catch (Exception $e) {
- echo "Error: " . $e->getMessage();
- }
- ?>
PHP之友评论