Js模板内容

时间:2016-06-30 18:01:17

标签: javascript php jquery json symfony

我正在社交网络上工作,我决定在Json而不是html中返回ajax响应,然后使用Handlebars.js或任何模板系统将其转换为html,但问题如下: 我正在使用Symfony Framework,所以我检查请求是否是XHR然后我返回一个JsonResponse,但是我应该为js模板转换做什么,当响应不是XHR时,我渲染一个php / Twig响应使用翻译组件,但js模板怎么样? 一个例子:



<h1> Product Title: {{product.title}} </h1>
<a href="/edit-{{product.id}}">
    Edit Product
</a>

<h1> Titre de produit: {{product.title}} </h1>
<a href="/edit-{{product.id}}">
    Editer le produit
</a>
&#13;
&#13;
&#13;

我想我不应该为不同语言创建100x js templtes,它将无法维护。像Twitter这样的网站如何操纵他们的Json响应?他们是否使用Json内容发送翻译?

我为我糟糕的英语道歉,我是法国人。谢谢你!

1 个答案:

答案 0 :(得分:0)

如果您只想翻译,可以使用JsTranslationBundle

您将能够使用javascript的翻译形式,或者 您可以添加将转换文件转换为json对象并将其与响应一起发送。

use Symfony\Component\Yaml\Yaml;

$translationArray = Yaml::parse(file_get_contents('/path/to/tranlation/file.yml'));

// $resopnse is the array you convert it to JsonResopnse;

$response['translation'] = $translationArray;