TemplateTags
- Usage
- Missing features
- Contributing
- Installation
- License
- See also
There are several ways to provide client-side templates. One possibility is to
put them inside <script>
elements.
Unfortunately, many text editors interpret all such content as JavaScript and enable unwanted syntax highlight mode. However, this can be remedied by using a dedicated helper method. I found myself copying that helper method into several projects, therefore I decided to extract it into a gem.
Usage
This gem provides a #template_tag
which renders a proper script tag. Template
can be either:
-
Passed as a block:
<%= template_tag "simple-tpl" do %> <div id="div-with-link"> <%= link_to "Example", "http://example.test" %> </div> <% end %>
-
Indicated by passing a partial name:
<%= template_tag "simple-tpl", partial: "/enclose_me", locals: {url: "http://example.test"} %>
Provided that following view partial is defined in
/app/views/_enclose_me.html.erb
:
<div id="div-with-link">
<%= link_to "Example", url %>
</div>
Both will return the same HTML fragment:
<script type="text/x-tmpl" id="simple-tpl">
<div id="div-with-link">
<a href="http://example.test">Example</a>
</div>
</script>
Missing features
This gem is ready for use. That said, there are several less-important features which could be added:
-
Ability to specify script element’s
type
attribute, either per use or with single application-wide setting. -
Some ready-to-go
getTemplate()
JavaScript function, althoughdocument.getElementById()
is no rocket science. -
Ability to render HTML5’s
<template>
element, not widely supported yet.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/skalee/template_tags.
When run with Guard, tests may fail randomly for some reason. Retrying always helps. Never happens outside Guard.
Installation
Add this line to your application’s Gemfile:
gem 'template_tags'
And then execute:
$ bundle
Or install it yourself as:
$ gem install template_tags
License
Template Tags is licensed under either the terms of Ruby, ISC, or MIT (Expat) licenses.
See also
-
HTML’s New Template Tag — nice HTML5 Rocks article about
<template>
tag.