Twig and truncating text


I created this simple Twig page on localhost on MAMP:

<html> <head> <style type="text/css"> table { border-collapse: collapse; } tr.heading { font-weight: bolder; } td { border: 0.5px solid black; padding: 0 0.5em; } </style> </head> <body> <h2>Automobiles</h2> <table> <tr class="heading"> <td>Vehicle</td> <td>Model</td> <td>Price</td> </tr> {% for d in data %} <tr> <td>{{ d.manufacturer|escape }}</td> <td>{{ d.model|escape }}</td> <td>{{ d.modelinfo|raw }}</td> </tr> {% endfor %} </table> </body> </html>

and this is the code behind it:

<?php // include and register Twig auto-loader include 'Twig/Autoloader.php'; Twig_Autoloader::register(); // attempt a connection try { $dbh = new PDO('mysql:dbname=world;host=localhost', 'root', 'mypass'); } 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 manufacturer, model, price FROM automobiles"; $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('cars.html'); // set template variables // render template echo $template->render(array ( 'data' => $data )); } catch (Exception $e) { die ('ERROR: ' . $e->getMessage()); } ?>

However, I am planning to truncate the text in the modelinfo field, I believe this can be done in MySQL with the select LEFT function, but how should I modify the query?

All help is appreciated!


You can truncate text in your Twig template like this:

{{ d.modelinfo[:10] }}

That should return the first 10 characters in d.modelinfo.

Take a look at the <a href="http://twig.sensiolabs.org/doc/filters/slice.html" rel="nofollow">slice filter</a> documentation page.


Twig has a truncate filter but you have to enable the text extensions by adding the following to your config.yml file.

services: twig.extension.text: class: Twig_Extensions_Extension_Text tags: - { name: twig.extension }

Then in your template you can use the truncate filter and pass it an integer which denotes the length of the truncation.

{{ d.modelinfo|truncate(50) }}


  • Launch box spout
  • Cant get the zend loader to load classes only under wamp
  • How use custom libraries in Kohana 3
  • Curried UDF - Pyspark
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Magento get URL before current
  • Hibernate to update table schema
  • Installing iPhone App to iPhone
  • Jackson Parser: ignore deserializing for type mismatch
  • Typescript - Unable to get 'import' statement to function
  • Change multiple background-images with jQuery
  • Date difference with leap year
  • How would I use PHP exceptions to define a redirect?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Running a C# exe file
  • Join two tables and save into third-sql
  • JSON with duplicate key names losing information when parsed
  • Trying to switch camera back to front but getting exception
  • Display Images one by one with next and previous functionality
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Matplotlib draw Spline from multiple points
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • A cron job substitute?
  • json Serialization in asp
  • Buffer size for converting unsigned long to string
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to get Windows thread pool to call class member function?
  • How can I remove ASP.NET Designer.cs files?
  • Running Map reduces the dimensions of the matrices
  • java string with new operator and a literal