Snippetize allows you to include partials in html or plain text fields. Stay dry and reuse your partials using shortcode. Use it for including forms, videos, maps or simply preformated html snippets.
If you want users to be able to add partials without editing html.
So this:
Article.new(content: "<div class='widget'>I need my awesome widget that displays everywhere on the site.</div>")
Becomes:
Article.new(content: "{{widget}}")
Add this line to your application's Gemfile:
gem 'snippetize'
And then execute:
$ bundle
Or install it yourself as:
$ gem install snippetize
All partials will be taken from a snippets
folder in views. For instance, {{awesome_widget}}
assumes you have the following file:
app/views/snippets/_awesome_widget.html.erb
Insert the name of the partial within double brackets {{}}
text = "<div class='widget'>{{awesome_widget}}</div>"
Call snippetize
and pass it your string
snippetize(text).html_safe
The result could look like this:
"<div class='widget'><div class='my-awesome-widget'>This lives in my partial.</div></div>"
You can also pass variables to you partial like you would normaly to a partial
text = "<div class='widget'>{{awesome_widget, count: 2, category: test}}</div>"
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Test your changes (
rspec spec
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request