2012-09-27 24 views
0

我刚刚使用h2O模板引擎(h ttp://www.h2o-template.org/H2O模板引擎和PDO/Mysql - 如何查询数据库?

虽然它对基本的html工作正常,因为我使用它附带的演示文件。

我的代码:

<?php 
    /** 
    * Simple example rendering a user list 
    * ------------------------------------ 
    * 
    * @credit - adapt from ptemplates sample 
    */ 
    require 'h2o.php'; 

    $template = new H2o('test.html', array(
     'cache_dir' => dirname(__FILE__) 
    )); 

    $time_start = microtime(true); 

    echo $template->render(array(
     'users' => array(
      array(
       'username' =>   'peter <h1>asdfasdf</h1>', 
       'tasks' => array('school', 'writing'), 
       'user_id' =>   1, 
      ), 
      array(
       'username' =>   'anton', 
       'tasks' => array('go shopping <h1>je</h1'), 
       'user_id' =>   2, 
      ), 
      array(
       'username' =>   'john doe', 
       'tasks' => array('write report', 'call tony', 'meeting with arron'), 
       'user_id' =>   3 
      ), 
      array(
       'username' =>   'foobar', 
       'tasks' => array(), 
       'user_id' =>   4 
      ) 
     ) 
    )); 

    echo "in ".(microtime(true) - $time_start)." seconds\n<br/>"; 

和模板文件的test.html:

{% extends 'layout.html' %} 
{% block title %}Userlist | {{ block.super }}{% endblock %} 
{% block content %} 
    <h2>Userlist</h2> 
    <ul> 
    {% for user in users limit:3 %} 
    <li> 
     <a href="https://stackoverflow.com/users/{{ user.username|urlencode }}">{{ user.username|truncate 4 }}</a> - 

     {{ user.tasks.first | safe }} 
    </li> 

    {% if not user.username == 'foobar' %} 
     haha 
    {% endif %} 

    {% endfor %} 
    </ul> 

{% endblock %} 

我将如何做到这一点的PDO/MySQL作为提及here - 语法类似于树枝的一种方式,但在H2O的网站上没有关于从数据库查询的文档。

回答

1

你必须使用php,而不是h2o ...