通过网络浏览器获取外部模板脚本
现代客户端模板库使用 text/html 类型的标签。
我读过的每一篇关于它的教程/文章都显示了嵌入到 html 页面的此类脚本的示例。
您是否知道有什么方法可以制作外部文本/html 脚本,该脚本将由浏览器加载,而无需使用ajax 或在服务器端自动生成页面?
我知道这两种方法都是可行的,但我希望尽可能地 KISS。
包含许多模板的页面很快就会变得一团糟。
Modern client side template libraries use tags of type text/html.
Every tutorial / article about it I read shows an example of such script which is embedded to the html page.
Are you aware of any way to make an external text/html script which would be loaded by the browser without using ajax or autogenerating the page on the server side?
I know that these two approaches are possible, but I want to be KISS as possible.
A page with many templates in it becomes a mess quickly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用什么模板引擎?大多数都接受字符串,在这种情况下,您只需通过 AJAX 请求模板文件并将字符串提供给模板引擎即可。
What templating engine are you using? Most will accept a string in which case you can just request the template file via AJAX and provide the string to the template engine.
经过一段时间和一些经验后,我想我知道一个可以满足我的问题的答案。
该方法稳健且易于使用。
有一个名为 require.js 的脚本加载器。它处理从服务器异步加载 JavaScript、文本文件和 i18n 数据。设置起来非常简单。
此外,它还允许预处理站点并内联这些 javascript/文本文件以在生产模式中使用。
这样,具有良好结构的开发就很容易,并且对于用户来说也应该可以快速工作(没有额外的请求)。
After some time and some experience I think I know an answer which would satisfy my question.
The method is robust and easy to use.
There is a script loader which is called require.js. It handles loading javascripts, text files and i18n data from server asynchronously. It is quite simple to set up.
Additionally it allows to preprocess the site and inline those javascripts/text files for use in production mode.
This way development with good structure is easy and it also should work fast (no additional requests) for users.