Project

triplet

0.0
No commit activity in last 3 years
No release in over 3 years
Pure Ruby template language
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.0
~> 0.13
>= 6.1

Runtime

>= 5.0.0, < 7.0
 Project Readme

Triplet

A simple Ruby DSL for defining templates. (Maybe) useful for defining single file view components.

Features:

  • Easy to use "AST" for defining HTML tags. [:a, { href: "/" }, "Home"]
  • DSL methods to make defining triplets easier. a(href: "/") { "Home" }
  • Supports Rails helper methods. e.g. form_for, text_field_tag, link_to, etc.
  • View Component support via include Triplet::ViewComponent

Installation

Add this line to your application's Gemfile:

gem 'triplet'

And then execute: bundle install in your shell.

Usage

nav_items = { "home": "/", "Sign Up": "/sign-up" }

Triplet.template {[
  nav(class: "max-w-3xl mx-auto flex") {[
    h1(class: "font-3xl") { "My App" },
    ul(class: "") {[
      nav_items.map do |name, link|
        li(class: "bold font-xl") {[ a(href: link.html_safe) { name } ]}
      end
    ]}
  ]},
  "Hello",
  span(class: "bold") { "world" },
]}

Will output the equivalent HTML:

<nav class="max-w-3xl mx-auto flex">
   <h1 class="font-3xl">My App</h1>
   <ul class="">
      <li class="bold font-xl"><a href="/">home</a></li>
      <li class="bold font-xl"><a href="/sign-up">Sign Up</a></li>
   </ul>
</nav>
Hello<span class="bold">world</span>

The tag methods (e.g. nav, h1, p) are helpers that turn Ruby code into triples, or 3 element arrays.

e.g. p(class: "font-xl") { "hello world!" } becomes [:p, { class: "font-xl" }, "hello world!"]

The two formats can be used interchangeably in templates.

If you need a custom tag, you can return a triplet directly:

[:"my-tag", { "custom-attribute" => "value" }, ["body content"]]
# <my-tag custom-attribute="value">body content</my-tag>

View Component Support

To use in view components, include the Triplet::ViewComponent module and define a call method. The module will handle the rest.

class NavComponent < ViewComponent::Base
  include Triplet::ViewComponent

  def template
    [
      h1 { "hello world" },
      render NavItemComponent.new(title: "Home", path: "/"),
      render NavItemComponent.new(title: "Pricing", path: "/pricing")
    ]
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/BlakeWilliams/triplet.

License

The gem is available as open source under the terms of the MIT License.