小枝,截断文本和PHP错误


Twig, truncating text and PHP error

我有一个小的简单的Twig网站(没有Symfony安装,所以配置。这里不涉及Yml),这是我的代码:

。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>

Employees是varchar(255), role是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

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

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

谢谢。

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

缺少的类:Twig_Extensions_Extension_Text是Twig- extensions库的一部分,而不是Twig核心的一部分。

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

引用文档:

当然,您需要首先通过以下方式加载扩展文件Require_once()或使用自动加载器(参见spl_autoload_register ()) .