http://qs321.pair.com?node_id=1168763


in reply to Re^7: perl dancer route template hashref pass complex json file to server issue (escape/filter)
in thread perl dancer route template hashref pass complex json file to server issue

thanks for the reply , but isn't there any way to disable template filter , or any other way to pass parameter between client and server without changing the param value
  • Comment on Re^8: perl dancer route template hashref pass complex json file to server issue (escape/filter)

Replies are listed 'Best First'.
Re^9: perl dancer route template hashref pass complex json file to server issue (the reverse)
by tye (Sage) on Jul 28, 2016 at 22:27 UTC

    You have it backward. You don't need to disable some template filter. You need to create and then enable an appropriate template filter.

    Your templated javascript contains lines like:

    var test='<%passtoserver%>';

    If the value provided for 'passtoserver' was "Don't do this", then the javascript generated by your template would be:

    var test='Don't do this';

    That is a syntax error. So you need to change your template to contain lines more like:

    var test='<% passtoserver | js_str %>';

    or, perhaps even better:

    var test=<% passtoserver | js_str %>;

    where the "| js_str" tells the template to properly escape any characters that need to be escaped in order to be included verbatim inside of a javascript string literal (and, in the second case, also adds the enclosing quote marks).

    And, no, Template::Toolkit doesn't come with a pre-built js_str filter so you'll have to create that as well.

    Your problem case is due to the \ character not being escaped for similar reasons. So your template produces javascript code like:

    var test='­[{"name":"­test","pro­blem":"her­e is the problem \" com +ma "}]';

    And, in javascript, '\"' is the same value as '"'.

    - tye        

      Doesn't add quotes but it exists Template::Plugin::JavaScript - Encodes text to be safe in JavaScript  document.write("[% sometext | js %]");

        thanks , the following worked with me
        <!DOCTYPE html> <html> <head> </head> <body> <div style = "padding: 100px 100px 10px;"> <script> var test2=<%passtoserver | replace('"', '"') %>; var test3=JSON.stringify(test2); alert(test3); </script> <button type="text" > </div> </body> </html>
        without any change in client side, Rami D.
      this worked for me
      <%passtoserver | replace('"', '"')%>;
      thanks