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

**URL:** <https://www.ruby-forum.com/t/how-do-you-replace-snippet-tags-in-a-pages-body/149096>\
**Category:** Rails\
**Created:** [September 20, 2008, 10:10am UTC](https://www.ruby-forum.com/t/how-do-you-replace-snippet-tags-in-a-pages-body/149096 "2008-09-20T10:10:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 20, 2008, 10:10am UTC](https://www.ruby-forum.com/t/how-do-you-replace-snippet-tags-in-a-pages-body/149096/1 "2008-09-20T10:10:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 21, 2008, 7:15am UTC](https://www.ruby-forum.com/t/how-do-you-replace-snippet-tags-in-a-pages-body/149096/2 "2008-09-21T07:15:01Z")

</div>

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}”  
end  
end  
end

Then in your view you can use:  
@page.processed\_body

-Dan M.  
[http://www.dcmanges.com/blog](http://www.dcmanges.com/blog)

On Sep 20, 3:10 am, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 21, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/how-do-you-replace-snippet-tags-in-a-pages-body/149096/3 "2008-09-21T23:29:44Z")

</div>

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

That works absolutely perfectly. Thank you!
