是否有更简洁的方法在 HAML 中启用 markdown 输出?
在 Rails 中使用 HAML,只要我有某种类型的文本字段,我希望用户能够轻松地设置格式,我就会使用 HAML 的 markdown 过滤器。在看起来像这样的模板中:
.description
:markdown
#{folder.description}
这可行,但我从来没有真正喜欢过它。如果我不需要在此文本字段上进行降价,我就会这样做:
.description= folder.description
在我的一些视图中,有许多可以进行降价格式化的文本实例,并且它总是会破坏模板的插入流程。
是否有任何更清晰或更简单的方法可以在 HAML 模板中使用 Markdown 格式的文本,或者是否有另一种方法可以将简单的文本格式添加到文本字段,从而生成更清晰的模板代码?
谢谢!
Using HAML in Rails, whenever I have a text field of some kind that I want the user to be able to easily format, I use HAML's markdown filter. In the template that looks like this:
.description
:markdown
#{folder.description}
This works, but I've never really liked it. If I didn't need markdown on this text field I would just be doing:
.description= folder.description
In some of my views there are many instances of text that can be markdown formatted, and it always breaks the flow of the template to insert them.
Is there any cleaner or simpler way to use markdown formatted text in a HAML template, or is there perhaps an alternative method of adding simple text formatting to a text field that results in cleaner template code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议一个简单的助手,例如:
I would suggest a simple helper, something like:
当我有可编译的格式(无论是 Markdown 还是 Textile)时,我通常将 html 版本和格式版本存储在数据库中。
因此,我可能会有
folder.description_markdown
和folder.description_html
字段,并且在文件夹或其他适当位置的 before_save 中,我将在那里执行 markdown 解析,并保存html 值。然后,在您看来,您所需要的只是:
如果您有描述的编辑页面,您将确保文本字段正确编辑description_markdown 字段。
我现在用来保存 Textile 格式的 before_save 回调示例就在这里:
我觉得这使得 haml 模板更易于遵循,并且还减少了每次检索字段时必须解析 markdown 的开销。
When I have compilable formatting (whether it be markdown, or textile) I usually store both the html version, and the format version in the database.
So I would probably have
folder.description_markdown
andfolder.description_html
fields, and in a before_save on folder or some other appropriate location I would perform the markdown parsing there, and save the html value.Then, in your view, all you would need is:
And if you have an edit page for the description you would make sure the text field is correctly editing the description_markdown field.
An example before_save callback I have right now to save Textile formatting is right here:
I feel like this makes the haml template easier to follow, and also reduces overhead from having to parse the markdown every time the field is retrieved.