I thinking how I can design response to frontend part of website.
I want to know which solution and why is better ?
我想我如何能设计出对网站前端部分的回应。我想知道哪种解决方案更好,为什么更好?
- Response as html
- 响应作为html
- Response as json
- 为json响应
To back-end side I using PHP with template engine TWIG. To front-end side I using jQuery. To front-end I don't use any template engine.
对于后端,我使用PHP和模板引擎TWIG。对于前端,我使用jQuery。对于前端,我不使用任何模板引擎。
I won't use template engine for back-end and front-end side, bacause I will have to duplicate template . It seems for me bad solution. Or maybe should I do it in this way ? maybe exist template engine which are able to use it in both case ?
我不会在后端和前端使用模板引擎,因为我必须复制模板。对我来说,这似乎是一个糟糕的解决方案。或者我应该这样做?也许存在模板引擎可以在两种情况下都使用它?
Please could someone me advise ?
能给我个建议吗?
English is not my native language, so sorry if it hard to understand. I believe someone will know what I mean :)
英语不是我的母语,如果很难理解,很抱歉。我相信有人会明白我的意思。
2 个解决方案
#1
1
JSON will be "better" in the vast majority of cases.
在大多数情况下,JSON会“更好”。
-
The information transferred from the server will be smaller. Consider:
["john","clara","paul"]
vs<ul><li>john</li><li>clara</li><li>paul</li></ul>
从服务器传输的信息将会更小。考虑:“约翰”,“克拉拉”,“保罗”)和< ul > <李> 约翰李< / > <李> 保罗克拉拉< /李> <李> < /李> < / ul >
-
Your back-end will be more flexible in how it can be used, you will be able to update your front-end markup independently of the back-end. JSON is also easier to manipulate with JavaScript than HTML.
后端标记的使用方式将更加灵活,可以独立于后端标记更新前端标记。使用JavaScript比使用HTML更容易操作JSON。
The only exception to this is if you want to hide the logic used to generate the html from the public, but even in that case I'd favor sending JSON for the flexibility advantage.
唯一的例外是,如果您希望隐藏用于向公众生成html的逻辑,但即便如此,出于灵活性的优势,我还是倾向于发送JSON。
#2
1
There's no better solution. I myself prefer all my ajax calls to return JSON because I can just let the front end determine what to do with it. This way I'm maintaining separation of concerns. But like I said, it's a personal preference. Outputting HTML isn't considered bad practice per say.
没有更好的解决方案。我自己更喜欢所有的ajax调用来返回JSON,因为我可以让前端决定如何使用它。这样我就保持了关注点的分离。但就像我说的,这是我个人的偏好。输出HTML并不被认为是不好的做法。
I'm not sure what you mean by back end templates and front end templates though.
我不确定你所说的后端模板和前端模板是什么意思。
#1
1
JSON will be "better" in the vast majority of cases.
在大多数情况下,JSON会“更好”。
-
The information transferred from the server will be smaller. Consider:
["john","clara","paul"]
vs<ul><li>john</li><li>clara</li><li>paul</li></ul>
从服务器传输的信息将会更小。考虑:“约翰”,“克拉拉”,“保罗”)和< ul > <李> 约翰李< / > <李> 保罗克拉拉< /李> <李> < /李> < / ul >
-
Your back-end will be more flexible in how it can be used, you will be able to update your front-end markup independently of the back-end. JSON is also easier to manipulate with JavaScript than HTML.
后端标记的使用方式将更加灵活,可以独立于后端标记更新前端标记。使用JavaScript比使用HTML更容易操作JSON。
The only exception to this is if you want to hide the logic used to generate the html from the public, but even in that case I'd favor sending JSON for the flexibility advantage.
唯一的例外是,如果您希望隐藏用于向公众生成html的逻辑,但即便如此,出于灵活性的优势,我还是倾向于发送JSON。
#2
1
There's no better solution. I myself prefer all my ajax calls to return JSON because I can just let the front end determine what to do with it. This way I'm maintaining separation of concerns. But like I said, it's a personal preference. Outputting HTML isn't considered bad practice per say.
没有更好的解决方案。我自己更喜欢所有的ajax调用来返回JSON,因为我可以让前端决定如何使用它。这样我就保持了关注点的分离。但就像我说的,这是我个人的偏好。输出HTML并不被认为是不好的做法。
I'm not sure what you mean by back end templates and front end templates though.
我不确定你所说的后端模板和前端模板是什么意思。