2013-04-27 69 views
1

我有一个简单的小枝条网站(无Symfony的安装,所以config.yml是不是与此有关),这是我的代码:嫩枝,截断文字和PHP错误

.htaccess文件:

php_flag display_startup_errors on 
php_flag display_errors on 
php_flag html_errors on 

employees.html:

<html> 
    <head> 
    <style type="text/css"> 
     table { 
     border-collapse: collapse; 
     }   
     tr.heading {  
     font-weight: bolder; 
     }   
     td { 
     border: 1px solid black; 
     padding: 0 0.5em; 
     }  
    </style> 
    </head> 
    <body> 
    <h2>Employees</h2> 
    <table> 
     <tr class="heading"> 
     </tr> 
     {% for d in data %} 
     <tr> 
     <td>{{ d.name|escape }}</td> 
     <td>{{ d.role|escape }}</td> 
     <td>{{ d.salary|escape }}</td> 
     </tr> 
     {% endfor %} 
    </table> 
    </body> 
</html> 

员工为varchar(255),作用是在MySQL的varchar(255)。

和我的代码:

<?php 
// include and register Twig auto-loader 
include 'Twig/Autoloader.php'; 
Twig_Autoloader::register(); 

// attempt a connection 
try { 
    $dbh = new PDO('mysql:dbname=employedb1;host=localhost', 'test', 'test'); 
} catch (PDOException $e) { 
    echo "Error: Could not connect. " . $e->getMessage(); 
} 

// set error mode 
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

// attempt some queries 
try { 
    // execute SELECT query 
    // store each row as an object 
    $sql = "SELECT name, role, salary FROM employees"; 
    $sth = $dbh->query($sql); 
    while ($row = $sth->fetchObject()) { 
    $data[] = $row; 
    } 

    // close connection, clean up 
    unset($dbh); 

    // define template directory location 
    $loader = new Twig_Loader_Filesystem('templates'); 

    // initialize Twig environment 
    $twig = new Twig_Environment($loader); 

    // load template 
    $template = $twig->loadTemplate('employees.html'); 

    // set template variables 
    // render template 
    echo $template->render(array (
    'data' => $data 
)); 

} catch (Exception $e) { 
    die ('ERROR: ' . $e->getMessage()); 
} 
?> 

它的工作,直到我做了如下修改的index.php(原代码添加注释上面树枝延伸,实际上不是): http://pastebin.com/QMaQXEip

它的工作,直到我添加了文本扩展名,并生成了以下错误: 致命错误:在第34行(第34行,引用上面的Twig_Extensions_Extension_Text())中找不到/Applications/MAMP/htdocs/employtesttwig/index.php中的类'Twig_Extensions_Extension_Text'。

为什么会发生这种情况,我该如何解决这个错误?

谢谢。

+0

那么,你有课吗? – Maerlyn 2013-04-27 20:48:59

回答

0

正如您所说,您只需在您的自定义设置中使用Twig。
我猜你必须添加枝条的扩展,它可以在这里找到:
https://github.com/fabpot/Twig-extensions

一个缺少的类:Twig_Extensions_Extension_Text是树枝,扩展LIB的一部分,而不是核心枝杈的。

您是否下载了软件包并包含或自动加载了必要的文件?

举文档:

Of course, you need to first load the extension file by either using require_once() or by using an autoloader (see spl_autoload_register()).