Process content with ERB

Update render helper for ERB processing

Update the render helper to process ERB before sending content to kramdown:

app/helpers/application_helper.rb
module ApplicationHelper
  def render_content_from(page)
    erb_processed_content = render(inline: page.content, layout: false)
    Kramdown::Document.new(erb_processed_content, input: "GFM").to_html.html_safe
  end
end

Use Ruby directly in markdown files:

app/views/erb.md
...

<%- Page.all.select(&:position).sort_by(&:position).each do |page| %>
  - <%= link_to page.title, page.slug %>
<%- end %>

...

To display:

Create additional helpers

Create more helpers for use in page content files:

app/helpers/pages_helper.rb
module PagesHelper
  def link_to_page(slug, html_options = nil)
    link_to Page.find(slug).title, page_path(slug), html_options
  end

  def pages_image_tag(path)
    image_tag "pages/#{@page.slug}/#{path}"
  end
end

The pages_image_tag helper requires that we add the content folder to Propshaft’s assets paths:

config/application.rb
...

module RailsStatic
  class Application < Rails::Application
    ...

    config.assets.paths << Rails.root.join("content")
  end
end

Commit: ERB


Accelerate with Turbo

Deploy with Parklife