如何存储/提供用户可定制的 HTML 模板?
我想提供类似 tumblr 的功能,您可以在其中选择模板,并可以选择在浏览器中自定义模板的 HTML 并保存它。
当前堆栈:Mongo、Sinatra(用于 REST API)作为原型。稍后可能会转向编译型静态类型语言。
想知道如何最好地实现这一目标。我考虑过的选项:
将 HTML 存储在 mongo 中,并为所有用户帐户复制它。因此,您选择的模板的 HTML 会写入您的帐户中。明显的缺点是空间效率低下,并且如果模板发生更改,则需要更新所有使用该模板的用户(如果未自定义 - 您自定义它,它就会成为您自己的,我永远不会碰它)。
将模板存储在模板集合中,并将自定义模板放入同一集合中或放入模板所有者的用户集合中。用户引用模板 ID。我认为这显然比 1 更好。特别是因为我不需要每次拉取用户对象时都拉取模板。
一些第三方库?在这里接受建议。
文件系统。
我需要打包这些模板(插入 js 和用户不应该接触到的内容),然后提供它们。非常感谢任何关于如何最好地解决这个问题的建议。
I want to offer a tumblr-like functionality where you can select a template, and optionally customize the HTML of your template in the browser and save it.
Current stack: Mongo, Sinatra (for REST API) for prototype. Will likely be moving to a compiled, statically-typed language later.
Wondering how best to accomplish this. Options I've considered:
Store the HTML in mongo, and duplicate it for all user accounts. So the HTML for the template you choose gets written into your account. Obvious cons of space inefficiency and need to update all users that use that template (if un-customized - you customize it it becomes your own and I won't ever touch it) if the template changes.
Store the templates in templates collection, and put custom templates either into this same collection or into the user collection with the owner of the template. User references a template id. This is quite clearly better than 1 I believe. Especially because I won't need to pull the template every time the user object is pulled.
Some third party library? Open to suggestions here.
File system.
I will need to package up these templates (insert js and stuff the user shouldn't be exposed to) and then serve them. Any advice on how best to approach this is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法将取决于您预见人们定制模板与仅使用标准的频率。混合方法怎么样?
也就是说,在用户文档中有一个延迟创建的字段(在使用时),该字段要么存储自定义模板,要么可能与其中一个标准不同(不确定您计划允许的自定义级别)。
然后,您可以拥有上面 2 中描述的模板字段,并为自定义模板设置“特殊”设置。虽然您仍然担心每次提取模板,但您确实有一个优势,即知道这些是您的一些更专注的用户 - 节省一次数据库访问可能是有利的,或者您可能不关心。
如果您不关心每个用户两次访问数据库,那么您可以采用方法 2,将自定义模板添加到模板集合中,并简单地为每个自定义用户引用新 ID。
这是一种平衡行为 - 每次提取模板的额外数据开销是否值得节省一次到数据库的往返,或者您是否希望以多次查询数据库为代价来提高每次获取数据的效率 -只有您可以根据您如何设计应用程序以及人们如何使用它来回答这个问题。
对于链接方法,您可能需要查看 数据库参考 和 架构设计。
Your approach will depend on how often you foresee people customizing the template versus just going with a standard. How about a hybrid approach?
That is, have a field in the user document that is created lazily (on use) that either stores the custom template, or maybe a diff from one of the standards (not sure about the level of customization you are planning to allow).
Then you can have the template field you describe in 2 above, with a "special" setting for custom templates. While you still have the concern about pulling a template each time, you do have the advantage of knowing that these are some of your more dedicated users - saving a trip to the DB might be advantageous, or you might not care.
If you don't care about 2 trips to the DB for every user, then you take approach 2, add the custom templates to the templates collection and simply reference the new ID for each user that customizes.
It's a balancing act - is the extra data overhead in terms of pulling the template each time worth saving a round trip to the DB or do you want efficiency in terms of the data you get each time at the cost of multiple queries to the DB - only you can answer that one based on how you design your app and how people use it.
For the linked approach you might want to take a look at Database References and Schema Design in the MongoDB docs.