Search code examples
phptwigapostrophe

Twig and Apostrophes


I recently implemented Twig on a PHP site. The site has a MySQL database in which each entry is a report. After implementing Twig apostrophes aren't being displayed properly. An example of this problem is below:

Evaluating the State�s Workers� Compensation

Charset on the webpages are the same as they were previously.

Before using Twig, I displayed results like this and everything worked fine:

    echo "<table border='1' width=100%><tr>";
    // printing table headers
    for($i=0; $i<$fields_num; $i++){
        $field = mysql_fetch_field($result);
        echo "<td>{$field->name}</td>";
    }

    while($row = mysql_fetch_assoc($result)){
        echo "<tr>";
        // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        foreach($row as $cell)
            echo "<td>$cell</td>";
            echo "</tr>\n";
    }

    echo "</table>";

Instead, I now use Twig's template tags:

{% for i in queryResult %}
    <tr>
        <td> {{ i.Month }} </td>
        <td> {{ i.Year }} </td>
        <td> {{ i.Title}) }} </td>
{% endfor %}

Solution

  • The solution was to use the raw filter to prevent auto-escaping.

    This involved changing:

    <td> {{ i.Title }) }} </td>
    

    To:

    <td> {{ i.Title|raw }) }} </td>
    

    Thanks to Alain Tiemblo's comment for pointing me in the right direction.