我正在制作一个样式指南,我在其中输出左侧显示的右侧代码。
我知道添加 %% 转义 ERB
我编写了一个助手,它获取块的内容并在两个地方呈现代码,一个显示 html,我希望另一个显示创建 html 的源 ERB。
问题是我得到了我想要 ERB 的 HTML。
查看代码
<%= display_code do %>
<%= link_to "Button", "/style_guide, class: "btn" %>
<% end %>
助手代码
module StyleGuideHelper
def display_code(&block)
content = with_output_buffer(&block)
html = ""
html << content_tag(:div, content, class: "rendered-code")
html << content_tag(:div, escape_erb(content), class: "source-code-preview")
html.html_safe
end
def escape_erb(code)
code = code.gsub("%=", "%%=")
end
end
预期结果 按钮 <%= link_to "Button", "/style_guide, class: "btn" %>
实际结果 按钮 按钮
干杯