6

Is there a helper function that will properly escape a string to be rendered as a single quote quoted JavaScript string literal?

I know of jsQuoteEscape but it only handles quotes and does not treat \n & \r etc.

so if my string is 'line1\nlineb' (i.e. two lines with a newline between them)

and I use

var jsvar='<?php echo $this->helper('myextension')->jsQuoteEscape($mystring); ?>';

I will get in the rendered content

    var jsvar='line1
line2';

which is a syntax error.

Thanks, Eyal

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
epeleg
  • 10,347
  • 17
  • 101
  • 151

1 Answers1

5

Yes

$string = 'Hello
There';                     
var_dump( Mage::helper('core')->jsonEncode($string) );
var_dump( json_encode($string) );

I've never been clear if this encoding a non-object string datatypes as a javascript string is a side-effect of the JSON encoding, or if it's true, according to Hoyle Crockford JSON, so I always like to wrap my strings in an object when passing them around

$o = new stdClass();
$o->param = 'This is my 
Param';         
$json = json_encode($o);            
echo 'var data = ' . $json . ';' . "\n";
echo 'var jsdata = data.param';

This is how you'd handle this with javascript. There's no method that's build specifically for this. If you're interested in seeing the helper methods you do have available from a block, checkout the methods in

app/code/core/Mage/Core/Block/Abstract.php        
app/code/core/Mage/Core/Block/Template.php        

and if you're dealing with a template that's part of a block higher up the chain, get its class and then check its definition

var_dump get_class($this);
Community
  • 1
  • 1
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • Although, as I answered on your own Q, a string does not seem to qualify as json I have a feeling that this is a bit too much. (at least in terms of the overhead on the javascript side. If you worry about calling json_encode with a non object I would personally prefer to pass the string as a single member in an array and then trim from the result anything up-to and including the first `[` and anything from and after the last `]`. This results with JS which is exactly what you want and nothing more. To be practical, I doubt if something (probably) so widely used would ever be broken. – epeleg Jan 23 '11 at 08:13
  • That said, would you go with `Mage::helper('core')->jsonEncode($string)` or with `json_encode($string)` ? I tend to pick the second as it shorter on one hand and looks like it has much less overhead so performance would be better. (I am assuming the second one is just part of PHP ? or is it not?). – epeleg Jan 23 '11 at 08:22
  • 1
    You're probably right, although every time I've said to myself "I'll just write this simple string replacement that surely won't have unexpected edge cases", I end up getting bitten by the edge cases. Because of that I've gotten in the habit of passing any dynamic JS vars through as json, as per above. – Alana Storm Jan 23 '11 at 08:29
  • @epeleg Yes, the second is the native PHP function. The first is a wrapper to Zend's json encoding methods in Zend_Json, which in turns just ends up calling json_encode. That's if json_encode is present. If it's not (PHP<5.2, or a weird custom php install), Zend encode uses a custom parser written in native PHP. I'd use the helper method myself, but I'm more concerned with abstraction these days than raw PHP performance. – Alana Storm Jan 23 '11 at 08:36