0.0
No commit activity in last 3 years
No release in over 3 years
Simple view helper for Rails which makes embedding client-side templates in views bit easier.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 0.5.3
~> 10.0

Runtime

< 6.0, >= 4.0
 Project Readme

TemplateTags

Table of Contents
  • Usage
  • Missing features
  • Contributing
  • Installation
  • License
  • See also

Version Build Status

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:

  1. Passed as a block:

    <%= template_tag "simple-tpl" do %>
      <div id="div-with-link">
        <%= link_to "Example", "http://example.test" %>
      </div>
    <% end %>
  2. 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:

  1. Ability to specify script element’s type attribute, either per use or with single application-wide setting.

  2. Some ready-to-go getTemplate() JavaScript function, although document.getElementById() is no rocket science.

  3. 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