Joh*_*hir 5 ruby haml ruby-on-rails
我想拥有独立的 .markdown 文件,然后将其包含在我的 haml 模板中。所以我想以某种方式在模板中包含——而不是渲染——一个外部文件。我希望父文件包含:markdown在其中,直接包含在其下方,然后 .markdown 文件只是纯降价。
或者:有没有办法只使用 markdown 作为 rails 模板语言(同样的方式我可以用 erb 或 haml 编写模板或部分,而 rails 只是想出来)?
这类似于您的解决方案,但使用:markdown过滤器。Haml 对任何过滤后的文本进行字符串插值,因此您可以像这样阅读 Markdown 文件。
:markdown
#{File.read(File.join(File.dirname(__FILE__), "foo.markdown"))}
Run Code Online (Sandbox Code Playgroud)
您可以将其放入帮助程序中,但您必须小心文件路径。
我能想到的最简单的方法是为 Markdown创建一个自定义模板处理程序。您可以使用 Markdown 代码作为部分代码(还可以免费获得本地支持)。
module Markdown
class Template < ActionView::Template::Handler
include ActionView::Template::Handlers::Compilable
self.default_format = Mime::HTML
def compile(template)
'"' + Maruku.new(template.source).to_html + '".html_safe'
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后使用 markdown 扩展注册它(在 application.rb 或自定义初始化程序中):
ActionView::Template.register_template_handler(:md, Markdown::Template)
Run Code Online (Sandbox Code Playgroud)
然后用户像任何部分一样进行渲染:)
# for file foo.md
= render 'foo'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1530 次 |
| 最近记录: |