PHP 和 JavaScript 模板的可互换性(防止重复)
我使用 MVC PHP 框架来使我的 Web 应用程序尽可能保持干燥。我的所有 HTML 模板都整齐地隐藏在项目应用程序范围内的一个文件夹中。
问题是,每当我使用 JSON 字符串通过 AJAX 构建页面时,我都需要重用这些模板中的大量行并将它们复制到 JavaScript 文件中的某个位置。这意味着我的 JavaScript 文件中的模板和 PHP 应用程序中的模板之间存在代码重复。
我想知道如何防止这种重复。一种方法当然是使用 AJAX 加载模板,但这样我最终会得到一页的双重 AJAX 请求。此外,PHP 模板使用与 MooTools 不同的标签样式来表示变量,但 HTML 设置是相同的。
总结一下:是否有任何巧妙的方法或工具来防止模板重复,以便一个文件可以在 PHP 和 JavaScript 中使用?郑重声明:我使用 MooTools 框架来构建 JavaScript 文件。
编辑
经过一番研究,我找到了我认为最好的答案。对于那些感兴趣的人:
PURE 将 HTML 表示和完全 JavaScript 逻辑,因此您不必费心在脚本中包含 HTML 元素。该模板可以简单地在 HTML 文件本身中提供。
示例:
// JSON string
{ 'who': 'me' }
// In your rendered HTML page:
<div id="who"></div>
// After the JSON string is sent back
<div id="who">me</div>
此外,它可以被多种库使用:MooTools、jQuery、dojo、Prototype 等。
I use a MVC PHP framework to keep my web applications as DRY as possible. All of my HTML templates are neatly tucked away in one folder in the application scope of my project.
The problem is that whenever I use a JSON string to build a page with AJAX, I need to reuse a lot of lines from these templates and copy them somewhere in my JavaScript files. This means there is code duplication between templates in my JavaScript files and templates in my PHP application.
I was wondering how this duplication can be prevented. One way is of course to load the template using AJAX, but then I would end up with a double AJAX request for one page. Furthermore, the PHP templates uses different tag styles to represent variables than MooTools, but the HTML setup is the same.
So to summarize: is there any neat way or a tool to prevent duplication of templates so one file could be used in both PHP and JavaScript? For the record: I use the MooTools framework to build my JavaScript files.
Edit
After some research, I found the best answer yet in my opinion. For those who are interested:
PURE separates HTML representation and JavaScript logic completely so you don't have to bother including HTML elements in your scripts. The template can simply be provided in the HTML file itself.
Example:
// JSON string
{ 'who': 'me' }
// In your rendered HTML page:
<div id="who"></div>
// After the JSON string is sent back
<div id="who">me</div>
Furthermore, it can be used by a wide selection of libraries: MooTools, jQuery, dojo, Prototype etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的问题,我有时也在努力解决。
您可以将 html 放入您的 javascript 代码中,这是重复的,您希望避免
您可以使用单独的 ajax 调用加载 html,这会导致运行更多的 ajax 调用,并可能减慢应用程序的速度。您可能想避免它。
您可以在加载数据的 Ajax 调用中传递 html。这样一来,您只需拨打一个电话。让 PHP 打开模板,并将它们添加到 data-json 流中。
您可以将模板放入原始 html 中,将其隐藏。
如果模板很小且有限,我会采用解决方案 3,或者可能采用解决方案 4。
JSon 会类似于 {"data": ... 你的原始数据对象, "templates":{...}}
Interesting question that I'm struggling with sometimes too.
you can put your html in your javascript code, which is duplicating and which you want to avoid
you can load your html with a separate ajax call, which causes more ajax calls to be run, and possible slowing down of your app. you may want to avoid it.
you can pass your html within the Ajax call that will load the data. That way, you only have one call. Let your PHP open your templates, and add them to the data-json stream.
you can put the template inside your original html, put it as hidden.
I'd go for solution 3, or possibly 4 if templates are small and limited.
The JSon would then be something like {"data": ... your original data object, "templates":{...}}
您可能正在寻找可用于多种语言的模板语言,以便您可以跨语言边界重复使用模板。
其中一种模板语言是 {{mustache}},适用于 PHP 和 Javascript > 以及更多语言。
You might be looking for a template langugage that is available for multiple languages so you can re-use your templates across language boundaries.
One such template language is {{mustache}}, works in both PHP and Javascript and many more languages.