AtanuCSE
AtanuCSE

Reputation: 8940

Apostrophe converted into & # 039 ; in twig

This question has been asked before here:

Question

But it doesn't solve my problem.

After I got data from database with apostrophe, I'm putting it inside a text input using

c_name='{{company_name}}';
$("#company_name").val(c_name);

It's giving result like

some Hatchery & Shrimp & #039;s Culture ltd.

So I applied the solution of that question like

c_name='{{company_name|raw}}';
$("#company_name").val(c_name);

It's giving me an error like

SyntaxError: missing ; before statement 
c_name='some Hatchery & Shrimp's Culture ltd.';

Showing error just after the Shrimp' and before s. Obviously string ended before it actually ends and expecting a ;

So I tried again with

c_name={{company_name|raw}};

SyntaxError: missing ; before statement
c_name=some Hatchery & Shrimp's Culture ltd.;

Now it's showing error just after the first white space,in this case before 'H'

My question is how can I handle apostrophe in twig? '|raw' is causing problem for me.

Upvotes: 9

Views: 9945

Answers (1)

deceze
deceze

Reputation: 522024

c_name = '{{ company_name|e('js')|raw }}';

Since you're in a Javascript context, you need to escape for Javascript (and then add raw to avoid additional automatic escaping for HTML).

Upvotes: 18

Related Questions