This section describes how to use ERB syntax and view helpers in your markdown files and ERB views in conjunction with MarkdownRecord models to make navigating across all your rendered content easy.
MarkdownRecord supports the use of ERB syntax in your markdown source files. It also provides the following local variables for use in your ERB code:
filename
: the name of the file being rendered.subdirectory
: the subdirectory of the file being rendered.frag_id
: the id of the fragment corresponding to the file being rendered.fragment
: the content fragment model instance corresponding to the file being rendered.scope
: the scope the file falls under, if any.All the standard view helpers that come with Rails should be accessible within your markdown source files when rendered as ERB files, such as tag helpers and path helpers.
For example, the image below is rendered using the image_tag
tag helper:
MarkdownRecord models can also be passed to dom_id
for use in Turbo-frames, etc:
<%= dom_id(fragment) %>
MarkdownRecord provides the following view helpers to use in your ERB views as well as in your markdown content source files:
render_fragment
: renders the html content of the passed in contend fragment, or the fragment corresponding to the file where a MarkdownRecor model is defined.
For example:
<%= render_fragment(@post) %>
Or
<%= render_fragment(@post.fragment) %>
link_to_markdown_record
: generates a link to the content fragment you pass in, or else the content fragment corresponding to the MarkdownRecord model you pass in.
For example, for a content fragment such as:
fragment
=> #<MarkdownRecord::ContentFragment filename: "content", id: "content/erb_syntax_and_view_helpers", meta: {"name"=>"ERB Syntax and View Helpers", ...}, ...>
When you pass it into the view helper like so:
<%= link_to_markdown_record(fragment) %>
The result would be the following markup:
<a href="/mdr/content/erb_syntax_and_view_helpers">ERB Syntax and View Helpers</a>
Notice how the text in the link is the name value found in the content fragment meta attribute. This is because MarkdownRecord checks to see if the model passed in responds to name
, and MarkdownRecord::ContentFragment
has a name
method that returns the name
field, if present, from its meta hash.
A fragment without a name will be rendered using the corresponding file name like this:
<a href="/mdr/content/erb_syntax_and_view_helpers">view_helpers</a>
Unless you pass in a name explicitly:
<%= link_to_markdown_record(fragment, "Click Here!") %>
Which will produce:
<a href="/mdr/content/erb_syntax_and_view_helpers">Click Here!</a>
You may have noticed that this view helper works almost identically to the standard Rails link_to
method, and in fact this helper simply wraps link_to
. As such, it has the same API that accepts html_options
and a block
as well. The only difference is that you have to pass in a MarkdownRecord
model as the first parameter.
link_to_markdown_record_html
: this view helper works identically to link_to_markdown_record
, but adds the html
prefix to the route so that it can only return HTML.
link_to_download_markdown_record_html
: same as link_to_markdown_record_html
but downloads a file instead.
link_to_markdown_record_json
: this view helper works identically to link_to_markdown_record
, but adds the json
prefix to the route so that it can only return HTML.
link_to_download_markdown_record_html
: same as link_to_markdown_record_json
but downloads a file instead.
Using the view helpers in conjunction with content fragments, you can easily build navigation that automatically updates when you rerender your content. This guide uses this technique and you can find the following code in the _global_layout.html.erb
layout:
<% fragment.parents_from("content/home").each_with_index do |frag, index| %>
<% unless index == 0%>
<span class="breadcrumb_divider">/</span>
<% end %>
<li><%= link_to_markdown_record(frag) %>
<% end %>
The above content was rendered from source files at: content/v_0_1/erb_syntax_and_view_helpers