Tritium (programming language) explained

Tritium
Designer:Hampton Catlin
Influenced By:XSLT, Sass (stylesheet language)
Website:tritium.io
File Ext:.ts

Tritium is a simple scripting language for efficiently transforming structured data like HTML, XML, and JSON. It is similar in purpose to XSLT but has a syntax influenced by jQuery, Sass, and CSS versus XSLT's XML based syntax.

History

Tritium was designed by Hampton Catlin, the creator of languages Sass and Haml and is currently bundled with the Moovweb mobile platform.[1]

As with Sass (created to address deficiencies in CSS) and Haml (created to address deficiencies in coding HTML templates), Catlin designed Tritium to address issues he saw with XSLT while preserving the core benefits of a transformation language. Much of this was based on his prior experience porting Wikipedia's desktop website to the mobile web.[2]

Open Tritium[3] is the open source implementation of the Tritium language. It was presented at O'Reilly Open Source Convention 2014[4] and the compiler is implemented in Go.

Concept

Tritium takes as input HTML, XML, or JSON documents and outputs HTML, XML, or JSON data that has been transformed according to the rules defined in the Tritium script. Like jQuery, idiomatic Tritium code is structured around selecting a collection of elements via a CSS or XPath selector and then chaining a series of operations on them.[5]

For example, the following script will select all the HTML table elements with id of foo and change their width attributes to 100%.

  1. Select all HTML nodes that are table elements with ID foo.
  2. The $$ function takes a regular CSS selector

$$(“table#foo”)

While Tritium supports both XPath and CSS selectors via the $ and $$ functions (respectively), the preferred usage is XPath. For example, the above code rewritten to use the equivalent XPath selector would be:

  1. Select all HTML nodes that are table elements with ID foo.
  2. The $ uses XPath

$(“//table[@id=’foo’]”)

Comparison to XSLT

Both Tritium and XSLT are designed for transforming data. However, Tritium differs in key ways to make it more familiar and easier to use for web developers:[6]

External links

Notes and References

  1. Web site: Episode 070 . ShopTalkShow . 2013-05-30 . 2013-07-10 . C. Coyier, D. Rupert..
  2. Web site: Interview with Hampton Catlin . HTML5 Developer Conference . 2013-05-07 . 2013-07-10 . D. Nugent.
  3. Web site: Open Tritium . https://archive.today/20150119174052/http://www.open-tritium.io/ . dead . 2015-01-19 . Moovweb . 2014-07-24 . 2015-01-19.
  4. Web site: Mobile and Multi-Device Web Development with Tritium . O'Reilly . 2014-07-24 . 2015-01-19.
  5. Web site: From Desktop to Mobile . Future Insights Live - Las Vegas, 2013 . 2013-04-30 . 2013-07-10 . I. Anand.
  6. Web site: Applying Transformations to Responsive Web Design . Smashing Magazine . 2014-02-06 . 2014-02-07 . I. Anand.