2

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 %}
indigochild
  • 350
  • 1
  • 5
  • 21
  • _apostrophes aren't being displayed properly._ - How are them displayed? – Federkun Oct 13 '15 at 19:58
  • Likely you have the curly apostrophes common to MS applications such as word. This would be a character encoding issue. – ArtisticPhoenix Oct 13 '15 at 21:00
  • 1
    If you're speaking about [this](http://twigfiddle.com/9x4pgs), that's because Twig uses auto escaping for security reasons. It shouldn't trouble browsers anyway. – Alain Tiemblo Oct 13 '15 at 21:18
  • @ArtisiticPhoenix - Why would implementing Twig create a character encoding issue? – indigochild Oct 15 '15 at 16:56
  • @indigochild - because of htmlentities and the Curly/Smart quote curse. (non SO link http://www.personal.psu.edu/ejp10/blogs/gotunicode/2007/02/smart_quotes_entity_codes_1.html ) and http://stackoverflow.com/questions/175785/how-do-i-convert-word-smart-quotes-and-em-dashes-in-a-string Same applies to the ndash/mdash/ellipsis – ArtisticPhoenix Oct 16 '15 at 21:22
  • Essentially this bit of code `htmlentities($str, ENT_QUOTES, "UTF-8");` see `5.4.0 The default value for the encoding parameter was changed to UTF-8. ` "smart" quotes, mdash and ndash are not good with `UTF-8` they are like `ISO-8859-1` or such and twig uses htmlentities to escape html ... well entities ... :) – ArtisticPhoenix Oct 16 '15 at 21:28

1 Answers1

1

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.

indigochild
  • 350
  • 1
  • 5
  • 21
  • This is fine if you are sure there no possible way of html being used in those fields or `users` filling the value in. However a better solution would be to change the character encoding to `ISO-8859-1` ( I think that is the one ) on `htmlentities` or replace the offending characters with the "web safe" ones. Otherwise you open your site up to XSS possibilities, if users can supply whatever i.Title is. Likely too, your Database table is not UTF-8. – ArtisticPhoenix Oct 16 '15 at 21:33