3

What I need

  • I need to show special symbols in view page.

           say name: Sara Fernström oksss.
    

    php code

          {% set  name=value.metadata.name %}
    
      <h3>
    
         {%if name|length < 40 %}
    
    {% set strategy = 'html' %}
    
     {% autoescape 'html' %}
     {{name|escape(strategy)|raw}}
    {% endautoescape %}
    

refrence

output :

    Sara Fernstru00f6m

but o/p should be:

    Sara Fernström oksss
Matteo
  • 37,680
  • 11
  • 100
  • 115
afeef
  • 547
  • 3
  • 9
  • 25
  • 1
    Have you ever tried to simply output `{{ name }}`? Without all this strategy an escaping thingy? Twig should handle strings properly from the beginning. – Emii Khaos Feb 07 '15 at 08:05
  • 1
    If you retrieve the value from a database check the correct encoding on that. – Matteo Feb 07 '15 at 09:37
  • Hi @user4041461 have you find a solutions? – Matteo Feb 10 '15 at 15:17
  • no problem has not solved , when i change spansih char in db my api wn"t run only print_r works>>@@@ – afeef Feb 11 '15 at 10:48
  • Hi, i don't understand: if you change the charset, you can print correctly via twig? What problem do you have in the API? PS: if you don't tag me in the comment i will not notify of your comment – Matteo Feb 13 '15 at 20:55

3 Answers3

1

Your issue is not with Twig, your issue is with your page and string encoding.

Make sure the string is UTF-8 before it is passed to Twig. Make sure the document is UTF-8.

Then use

{{ data }}

to output your string.

I work with åöä all the time(swedish) and literally never had to use anything else.

MichaelHindley
  • 485
  • 3
  • 7
0

You will need to decode the string in the proper charset:

{{ data|convert_encoding('UTF-8', 'YourCharset') }}

data will be your string, the first value is the output-charset the second the input-charset, you can also see the twig doc for this: http://twig.sensiolabs.org/doc/filters/convert_encoding.html

If the this doesn't work maybe try to decode the variable inside your php code:
For your code it would be something like this I think:

value.metadata.setName(utf8_decode(value.metadata.getName());
Nickolaus
  • 4,785
  • 4
  • 38
  • 60
0

Check how you store the data in your database with the charset and collate params as example:

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity(repositoryClass="MyEntityRepository")
 * @ORM\Table(name="my_entity",
 *      options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"})})
 */
class MyEntity
{
  ....

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115
  • Hi @user4041461 if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. please consider to upvote my answer if you find it useful – Matteo Feb 16 '15 at 06:26