简体   繁体   English

树枝,截断文本和PHP错误

[英]Twig, truncating text and PHP error

I have a small simple Twig site (no Symfony install, so config.yml is not relevant here) and this is my code: 我有一个简单的Twig小站点(未安装Symfony,因此config.yml与此处无关),这是我的代码:

.htaccess file: .htaccess文件:

php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on

employees.html: employee.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 is varchar(255), role is varchar(255) in MySQL. 在MySQL中,员工是varchar(255),角色是varchar(255)。

and my code: 和我的代码:

<?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());
}
?>

It worked until I made the following modification to index.php (comment added above twig extension, not actually in original code): http://pastebin.com/QMaQXEip 它一直起作用,直到我对index.php进行了以下修改(注释在树枝扩展名上方添加,实际上未在原始代码中添加): http : //pastebin.com/QMaQXEip

It worked until I added the Text extension, and generated this error: Fatal error: Class 'Twig_Extensions_Extension_Text' not found in /Applications/MAMP/htdocs/employtesttwig/index.php on line 34 (line 34 referring to Twig_Extensions_Extension_Text() above). 它一直起作用,直到我添加了文本扩展名,并产生此错误:致命错误:在/Applications/MAMP/htdocs/employtesttwig/index.php的第34行(第34行引用了上面的Twig_Extensions_Extension_Text())中找不到类'Twig_Extensions_Extension_Text'。

Why is this happening and how can I resolve this error? 为什么会发生这种情况,如何解决此错误?

Thanks. 谢谢。

As you say, you just use Twig in your custom setup. 正如您所说,您只需在自定义设置中使用Twig。
I guess you have to add Twig-extensions, which can be found here: 我想您必须添加Twig扩展名,可以在这里找到:
https://github.com/fabpot/Twig-extensions https://github.com/fabpot/Twig-extensions

The class which is missing: Twig_Extensions_Extension_Text is part of the Twig-Extensions lib and not of the Twig core. 缺少的类: Twig_Extensions_Extension_Text是Twig-Extensions库的一部分,而不是Twig核心。

Did you download the package and included or autoloaded the necessary files? 您是否下载了软件包并包含或自动加载了必要的文件?

To cite the docs: 引用文档:

Of course, you need to first load the extension file by either using require_once() or by using an autoloader (see spl_autoload_register()). 当然,您首先需要使用require_once()或使用自动加载器来加载扩展文件(请参见spl_autoload_register())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM