【发布时间】:2012-02-27 03:50:48
【问题描述】:
问题
在 PHP 中我会这样做
<html>
<body>
<script>
<?php
$templates = array(
'result' => file_get_contents('template.html')
);
echo '__templates = ' . json_encode($templates) . ';';
?>
</script>
</body>
</html>
所以我在 Sinatra 中做了这个
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
__templates = <%= get_templates %>;
</script>
</body>
</html>
我的 Sinatra 路由文件如下所示:
require 'sinatra'
require 'json'
require 'erb'
helpers do
def get_templates
{'result' => (erb :template2)}.to_json
end
end
get '/' do
erb :index
end
但是当我使用来自 jquery 的 __templates 对象时,我的 javascript 对象最终被破坏了。
ruby 的 to_json 做了哪些 PHP 不做的事情?
php结果
{"result":"<!doctype html>\n<html>\n\n<head>\n\t<meta charset=\"UTF-8\">\n\t<title>{{ TITLE }}<\/title>\n\n\t<style type=\"text\/css\">\n\t{{ CSS }}\n\t<\/style>\n<\/head>\n\n<body>\n\n\t{{{ HTML }}}\n\n\t<script src=\"{{{ JSLIB }}}\"><\/script>\n\t<script src=\"{{{ PREFIX }}}\"><\/script>\n\t<script>\n\t\tfunction __run() {\n\t\t\t{{{ JS }}}\n\t\t}\n\t<\/script>\n\n<\/body>\n\n<\/html>"}
红宝石结果
{"result":"<!doctype html>\n<html>\n\n<head>\n <meta charset=\"UTF-8\">\n <title>{{ TITLE }}</title>\n\n <style type=\"text/css\">\n {{ CSS}}\n </style>\n</head>\n\n<body>\n\n {{{ HTML }}}\n\n <script src=\"{{{ JSLIB }}}\"></script>\n <script src=\"{{{ PREFIX }}}\"></script>\n <script>\n function __run() {\n {{{ JS }}}\n }\n </script>\n\n</body>\n\n</html>\n"};
注意 php 版本上的<\/body>。
解决方案(暂时)
我找到了一个基于下面发布的 cmets 的解决方案。但是,我不确定这是否是“一种真正的方式”,如果存在的话。 注意:我想知道这个实现是否会在未来打破其他东西。我将在整个应用程序中依赖此函数。
我最终只是使用gsub 来解决我的问题。我将它包裹在这样的助手中
helpers do
def close embedded_json
embedded_json.gsub('</', '<\/')
end
...
end
这样称呼它
__templates = <%= close get_templates %>;
看起来很笨拙,但在幕后,也许在 rails 中提到的助手做同样的事情?我现在要继续前进,但我仍然想知道以后我是否会因此而中弹。
【问题讨论】:
-
生成的 JSON 输出是什么样的?
-
你得到什么错误?你可以在控制台中检查吗?
-
2:9393/:197Uncaught SyntaxError: Unexpected token ILLEGAL
-
嗯,根据 jsonlint.com,ruby 输出是有效的 JSON
-
Ruby json 示例末尾没有分号,但这可能只是类型/复制过去错误。
标签: php javascript jquery ruby json