PHP头条
热点:

三、Dwoo语法讲解

下面以实例的形式讲解下Dwoo的语法,先来看最常用的if语句。

1) if 语句

下面是一个模版的例子:

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. {if $auth == 0}  
  5. Not logged in  
  6. {else}  
  7. Logged in as: Anonymous User   
  8. {/if}  
  9. </body> 
  10. </html> 

可以看到,Dwoo中的if语句其实跟普通的if语句没什么区别。接下来我们看下控制这个模版的php文件,如下:

  1. <?php  
  2. include 'dwooAutoload.php';  
  3. try {  
  4. $dwoo = new Dwoo();  
  5. $tpl = new Dwoo_Template_File('tmpl/auth.tpl');  
  6. $data = new Dwoo_Data();  
  7. $data->assign('auth', rand(0,1));  
  8. $dwoo->output($tpl$data);  
  9. } catch (Exception $e) {  
  10. echo "Error: " . $e->getMessage();   
  11. }  
  12. ?> 

注意,这里我们使用了new Dwoo_Data();这个Dwoo_Data()方法的优势在于,它比较容易存放大量的数据,比用数组的方法去存储数据方便多了,而且它本身提供了很多不同的方法去获得,清理和删除模版变量。这个例子中,用随机数的方法产生了auth变量,结果可能为如下图:

xiatu

当然,可以使用if elseif语句,比如模版中:

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. {if $auth == 1}  
  5. Logged in as: Anonymous User   
  6. {elseif $auth == 2}  
  7. Logged in as: Administrator   
  8. {else}  
  9. Not logged in  
  10. {/if}  
  11. </body> 
  12. </html> 

2) LOOP循环语句

在Dwoo中,可以使用{loop}进行循环,动态产生数据,下面是例子:

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. <ul> 
  5. {loop $items}  
  6. <li>{escape($item)}</li> 
  7. {/loop}  
  8. </ul> 
  9. </body> 
  10. </html> 

下面是产生数据的php文件:

  1. <?php  
  2. include 'dwooAutoload.php';  
  3. try {  
  4. $dwoo = new Dwoo();  
  5. $tpl = new Dwoo_Template_File('tmpl/list.tpl');  
  6. $data = new Dwoo_Data();  
  7. $items = array();  
  8. $items[] = array('item' => 'red');  
  9. $items[] = array('item' => 'yellow');  
  10. $items[] = array('item' => 'blue');  
  11. $items[] = array('item' => 'green');  
  12. $data->assign('items'$items);  
  13. $dwoo->output($tpl$data);  
  14. } catch (Exception $e) {  
  15. echo "Error: " . $e->getMessage();   
  16. }  
  17. ?> 

这里,我们生成了数组items,然后在模版文件中,通过{loop $items}即可循环输出内容。结果如下图:

jieguo

注意,这里使用了{escape($item)}的方法输出每一行的内容,其中eascape是dwoo中使用的插件,是将所有内容在输出前使用HTML编码格式过滤,这可以防止XSS攻击,是个很好的实践。

而在Dwoo中,可以同样使用{foreach}而达到同样的效果,代码如下:

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. <ul> 
  5. {foreach $items item}  
  6. <li>{escape($item)}</li> 
  7. {/foreach}  
  8. </ul> 
  9. </body> 
  10. </html> 

同样,foreach也可以使用如下的用法,即:

  1. <html> 
  2. <head></head> 
  3. <body> 
  4. <ul> 
  5. {foreach $items key value}  
  6. <li>{upper($key)} is for {$value}</li> 
  7. {/foreach}  
  8. </ul> 
  9. </body> 
  10. </html> 

而配合这个模版,PHP的控制页面中的关联数组的写法如下:

  1. $data = new Dwoo_Data();  
  2. $items = array(  
  3. 'a' => 'apple',  
  4. 'b' => 'ball',  
  5. 'c' => 'cat',  
  6. 'd' => 'dog' 
  7. );  
  8. $data->assign('items'$items); 

这样输出结果如下图:

shuchujieguo

我们既然学会了loop,下面来尝试下从数据库中取出数据集,并通过Dwoo显示出来,下面是模版文件的主要部分:

  1. <body> 
  2. <table> 
  3. <tr class="heading"> 
  4. <td>Author</td> 
  5. <td>Title</td> 
  6. </tr>   
  7. {loop $records}  
  8. <tr> 
  9. <td>{$author}</td> 
  10. <td>{$title}</td> 
  11. </tr>   
  12. {/loop}  
  13. </table> 
  14. </body> 

而PHP文件代码如下,其中使用了PDO去访问数据库:

  1. <? php  
  2. include 'dwooAutoload.php';  
  3. // 连接数据库  
  4. try {  
  5. $dbh = new PDO('mysql:dbname=library;host=localhost''user''pass');  
  6. } catch (PDOException $e) {  
  7. echo "Error: Could not connect. " . $e->getMessage();  
  8. }  
  9. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
  10. try {  
  11. $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";  
  12. $sth = $dbh->query($sql);  
  13. while ($row = $sth->fetchObject()) {  
  14. $records[] = array('author' => $row->author, 'title' => $row->title);  
  15. }  
  16. //关闭数据库连接  
  17. unset($dbh);   
  18. $dwoo = new Dwoo();  
  19. $tpl = new Dwoo_Template_File('tmpl/books.tpl');  
  20. $data = new Dwoo_Data();  
  21. $data->assign('records'$records);  
  22. $dwoo->output($tpl$data);   
  23. } catch (PDOException $e) {  
  24. echo "Error: Could not execute query \"$sql\". " . $e->getMessage();   
  25. unset($dbh);  
  26. } catch (Exception $e) {  
  27. echo "Error: " . $e->getMessage();   
  28. }   
  29. ?> 


www.phpzy.comtrue/php/9537.htmlTechArticle三、Dwoo语法讲解 下面以实例的形式讲解下Dwoo的语法,先来看最常用的if语句。 1) if 语句 下面是一个模版的例子: html head / head body {if$ auth ==0} Notloggedin...

相关文章

相关频道:

PHP之友评论

今天推荐