magento转义字符串为javascript [英] magento escape string for javascript
问题描述
我知道jsQuoteEscape,但它只处理引号并且不对待\\\
& \r等。
所以如果我的字符串是'line1\\\
lineb'(即两行之间有一个换行符)
我使用
var jsvar ='<?php echo $ this-> helper myextension') - > jsQuoteEscape($ MyString的); ?>;
我将收到呈现的内容
var jsvar ='line1
line2';
这是一个语法错误。
谢谢,
Eyal
是
$ string ='Hello
There';
var_dump(Mage :: helper('core') - > jsonEncode($ string));
var_dump(json_encode($ string));
我有从来没有被清除如果将非对象字符串数据类型的编码作为JavaScript字符串是JSON编码的副作用,或者如果它是真的,根据
$ o = new stdClass();
$ o-> param ='这是我的
Param';
$ json = json_encode($ o);
echo'var data ='。 $ json。 ';'。 \\\
;
echo'var jsdata = data.param';
这是用javascript处理这个问题。没有专门为此构建的方法。如果您有兴趣看到您从块中获得的帮助方法,请在
应用程序/代码/核心/法师/核心/块/ Abstract.php
应用程序/代码/核心/法师/核心/块/ Template.php
,如果您正在处理一个更高链接的块的模板,请获取其类,然后检查其定义
var_dump get_class($ this);
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
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);
这篇关于magento转义字符串为javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!