How Do You Replace Snippet Tags in a Page's Body?

I’m trying to replicate the Snippet functionality in typical CMSs. I
have a table called “Pages” and “Snippets”, where the latter is inserted
into the former.

I want the @page.body to replace anything between these tags {{ }} with
its appropriate snippet title (e.g., {{ Footer }}).

I’m thinking something along the lines of:

<%=h @page.body.gsub(“ANYTHING_BETWEEN_{{_}}_TAGS”,
Snippet.find_by_title(“APPROPRIATE_SNIPPET_TITLE_HERE”).body ) %>

Any ideas?

I’d go with something like this:

class Page
def processed_body
body.gsub(/{{(.+?)}}/) do
snippet = Snippet.find_by_title($1)
snippet ? snippet.body : “could not find snippet: #{$1}”

Then in your view you can use:

-Dan M.

On Sep 20, 3:10 am, Bob S. [email protected]

You’re amazing Dan! Thanks so much for your great help!! =)

That works absolutely perfectly. Thank you!