Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
239 lines (174 sloc) 6 KB

Taggart surrounds your strings with HTML Tags

How and what

  "Hello World!".h1


  <h1>Hello World!</h1>

or try (in bad typographical taste)


to get


or do some more nesting

  ("Label".td + "Value".td).tr.table

to render a small table


Add HTML attributes as Ruby parameters.

  "hello".span(class: 'stuff', id: 'thing')

to get a nice HTML string like this

  <span class="stuff" id="thing">hello</span>

You can also use arrays, how about this table?

  (%w(r1c1 r1c2 r1c3) + %w(r2c1 r2c2 r2c3)

will produce this HTML (author's line breaks and indentation)


Naturally we can do single tags too.

  "Gimme a".br

will return.

  Gimme a<br />

You can choose to close the tag with Taggart.close_ending_tag or keep the tag open (default as of 0.0.8) with Taggart.open_ending_tag.

Also try some of the 'cleverness', for example calling .script on a .js file will yield a different result compared to running it on a Javascript. Like this:

  "alert('This string will pop up an alert!')".script

gives you

  <script type="text/javascript">alert('This string will pop up an alert!')</script>

whereas this string


gives you this

  <script type="text/javascript" src="/script/awesome_script.js"></script>

Calling .ol or .ul on an array, will generate a full ordered or unordered list, like this

  %w(First Second Third).ol



Naturally they all also work with attributes.

See the spec/taggart_spec.rb for more examples (until I have time to do a bigger-better-faster-example file).


If you get lost in Taggart or if you're just curious about what Taggart can do, I've created a few informational things for you. In the IRB console you can now run:  # For generic information about Taggart (or
   puts Taggart.tags  # For a list of different tags and other curious things.

There's also some constants that you can access, such as VERSION, and tags, see the aforementioned and Taggart.tags.


I have a lot of Ruby code that marks some String in an HTML tag, usually with a specific CSS class. Or perhaps a link or so. It's not particularly nice to write "<strong>#{my_variable}</strong>". The more complex the Ruby code is, the worse it is to have ", #, { or } soiling the code. I was looking at the code and I thought, wouldn't it be nice if I could just tell that piece of String that it's supposed to be a <span> or some other tag. One could simply write my_variable.strong to get the job done. Taggart was born.

The Idea

The idea was to simplify the code and make it easier and faster for me to add arbitrary HTML into my non-HTML code. I wanted to stop breaking my fingers trying to get the quote-string-hash-curlybrackets-code-curlybrackets-morestrings-quote right on my keyboard. It was never intended to be a full-fledged HTML page renderer. If you want to use it for that, I'd love to see the result, though.


Install the gem:

  gem install taggart

Then load Taggart with:

  require 'taggart'

Taggart is now active, which means you can play around.


  • Added HTML5 tags - the list is not complete, let me know what I've missed.
  • Added ability to have closed and open endings on single tags (i.e <br> and <br />.
  • Moved help to a separate module.
  • Added informational and Taggart.tags.
  • Moved several tags into arrays for easier reference.
  • Created Taggart::VERSION and Taggart::BUILD for reference and DRYness.
  • Changed some of the labels in the test file.
  • Added <table> to Array that creates single or multi row tables.
  • Added <ol> and <ul> tags to the Array so you can now generate lists in one fast action.
  • Added <script>-tag. You can now add "my-script.js".script and "alert('Hello World!')".script.
  • Added several tags (still not a complete list, tweet or send a pull request for more tags.)
  • Removed several if DEBUG lines
  • Removed examples and most comments from taggart.rb file
  • Converted the README to markdown for a bit better formatting fun.
  • Added .href and .img feature.
  • Created Gem
  • Pushed code to Git.
  • Created test Gem.
  • Added files to create Gem and reorganised the file structure.
  • Made dual_sub() pass all the tests, and added the examples from .tr (translate) Ruby docs to the test.
  • More work on the "dynamic namespace clash resolver", in other words, .tr and sub work in both classic and Taggart way.
  • Initial version of "dynamic namespace clash resolver" to fix issues with .tr.
  • Added basic RSpec test.
  • Added namespacing for Strings and Arrays.
  • Implemented arrays; ["Label", "Value"] and ["First", "Second", "Third"].li.ol.
  • Tidied up things a bit.
  • Added a version of attributes "Red".span(class: 'red').
  • First version. Basic tags.


With your blessing. Like Ozzy said; "The crazier you get, the crazier Ozzy gets!", or something.

  • Potential validations, could check file, size, etc
  • Full fledged examples.
  • Please send suggestions.


  • "hello".sub('world', 'world') returns <sub world world>hello</sub>. Not really perfect.
  • Please help me test it out.

Feedback welcome!!

Author: Jocke Selin @jockeselin

Date: 2012-10-10

Version: 0.0.8 Build 013


You can’t perform that action at this time.