https://github.com/jeremyevans/tilt
Generic interface to multiple Ruby template engines
https://github.com/jeremyevans/tilt
Keywords from Contributors
rubygems activerecord rack activejob mvc sinatra rspec ruby-gem minitest background-jobs
Last synced: about 11 hours ago
JSON representation
Repository metadata
Generic interface to multiple Ruby template engines
- Host: GitHub
- URL: https://github.com/jeremyevans/tilt
- Owner: jeremyevans
- License: mit
- Created: 2015-04-29T19:56:29.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2025-09-27T19:36:56.000Z (3 months ago)
- Last Synced: 2025-12-04T16:54:14.682Z (8 days ago)
- Language: Ruby
- Homepage:
- Size: 1.17 MB
- Stars: 71
- Watchers: 6
- Forks: 10
- Open Issues: 0
- Releases: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: COPYING
README.md
Tilt
Tilt is a thin interface over a bunch of different Ruby template engines in
an attempt to make their usage as generic as possible. This is useful for web
frameworks, static site generators, and other systems that support multiple
template engines but don't want to code for each of them individually.
The following features are supported for all template engines (assuming the
feature is relevant to the engine):
- Custom template evaluation scopes / bindings
- Ability to pass locals to template evaluation
- Support for passing a block to template evaluation for
yield - Backtraces with correct filenames and line numbers
- Template file caching and reloading
- Fast, method-based template source compilation
The primary goal is to get all of the things listed above right for all
template engines included in the distribution.
Support for these template engines is included with Tilt:
| Engine | File Extensions | Required Libraries |
|---|---|---|
| {Asciidoctor}[rdoc-ref:lib/tilt/asciidoc.rb] | .ad, .adoc, .asciidoc | asciidoctor |
| {Babel}[rdoc-ref:lib/tilt/babel.rb] | .es6, .babel, .jsx | babel-transpiler |
| {Builder}[rdoc-ref:lib/tilt/builder.rb] | .builder | builder |
| {CoffeeScript}[rdoc-ref:lib/tilt/coffee.rb] | .coffee | coffee-script (+ javascript) |
| {CoffeeScriptLiterate}[rdoc-ref:lib/tilt/coffee.rb] | .litcoffee | coffee-script (+ javascript) |
| {CommonMarker}[rdoc-ref:lib/tilt/commonmarker.rb] | .markdown, .mkd, .md | commonmarker |
| {CSV}[rdoc-ref:lib/tilt/csv.rb] | .rcsv | csv (ruby stdlib) |
| {ERB}[rdoc-ref:lib/tilt/erb.rb] | .erb, .rhtml | erb (ruby stdlib) |
| {Erubi}[rdoc-ref:lib/tilt/erubi.rb] | .erb, .rhtml, .erubi | erubi |
| {Etanni}[rdoc-ref:lib/tilt/etanni.rb] | .ern, .etanni | none |
| {Haml}[rdoc-ref:lib/tilt/haml.rb] | .haml | haml |
| {Kramdown}[rdoc-ref:lib/tilt/kramdown.rb] | .markdown, .mkd, .md | kramdown |
| {Liquid}[rdoc-ref:lib/tilt/liquid.rb] | .liquid | liquid |
| {LiveScript}[rdoc-ref:lib/tilt/livescript.rb] | .ls | livescript (+ javascript) |
| {Markaby}[rdoc-ref:lib/tilt/markaby.rb] | .mab | markaby |
| {Nokogiri}[rdoc-ref:lib/tilt/nokogiri.rb] | .nokogiri | nokogiri |
| {Pandoc}[rdoc-ref:lib/tilt/pandoc.rb] | .markdown, .mkd, .md | pandoc |
| {Plain}[rdoc-ref:lib/tilt/plain.rb] | .html | none |
| {Prawn}[rdoc-ref:lib/tilt/prawn.rb] | .prawn | prawn |
| {Radius}[rdoc-ref:lib/tilt/radius.rb] | .radius | radius |
| {RDiscount}[rdoc-ref:lib/tilt/rdiscount.rb] | .markdown, .mkd, .md | rdiscount |
| {RDoc}[rdoc-ref:lib/tilt/rdoc.rb] | .rdoc | rdoc |
| {Redcarpet}[rdoc-ref:lib/tilt/redcarpet.rb] | .markdown, .mkd, .md | redcarpet |
| {RedCloth}[rdoc-ref:lib/tilt/redcloth.rb] | .textile | redcloth |
| {RstPandoc}[rdoc-ref:lib/tilt/rst-pandoc.rb] | .rst | pandoc |
| {Slim}[rdoc-ref:lib/tilt/slim.rb] | .slim | slim |
| {Sass}[rdoc-ref:lib/tilt/sass.rb] | .sass | sass-embedded, sassc, or sass |
| {Scss}[rdoc-ref:lib/tilt/sass.rb] | .scss | sass-embedded, sassc, or sass |
| {String}[rdoc-ref:lib/tilt/string.rb] | .str | none |
| {TypeScript}[rdoc-ref:lib/tilt/typescript.rb] | .ts | typescript (+ javascript) |
| {Yajl}[rdoc-ref:lib/tilt/yajl.rb] | .yajl | yajl-ruby |
See https://tilt.jeremyevans.net for formatted documentation for Tilt.
Basic Usage
Instant gratification:
require 'tilt'
require 'tilt/erb'
template = Tilt.new('templates/foo.erb')
=> #<Tilt::ERBTemplate @file="templates/foo.erb" ...>
output = template.render
=> "Hello world!"
It's recommended that calling programs explicitly require the Tilt template
engine libraries (like 'tilt/erb' above) at load time, or finalize the
mapping (see section below). Tilt attempts to lazy require the template engine
library the first time a template is created, but this is prone to error in
threaded environments.
The Tilt::Template class is an abstract base class that used by all supported
template engines. Each template class adheres to the same interface for
creation and rendering. In the instant gratification example, we let Tilt
determine the template implementation class based on the filename, but
Tilt::Template implementations can also be used directly:
require 'tilt/haml'
template = Tilt::HamlTemplate.new('templates/foo.haml')
output = template.render
The render method takes an optional evaluation scope and locals hash
arguments. Here, the template is evaluated within the context of the
Person object with locals x and y:
require 'tilt/erb'
template = Tilt::ERBTemplate.new('templates/foo.erb')
joe = Person.find('joe')
output = template.render(joe, :x => 35, :y => 42)
If no scope is provided, the template is evaluated within the context of an
object created with Object.new.
A single Template instance's render method may be called multiple times
with different scope and locals arguments. Continuing the previous example,
we render the same compiled template but this time in jane's scope:
jane = Person.find('jane')
output = template.render(jane, :x => 22, :y => nil)
Blocks can be passed to render for templates that support running
arbitrary ruby code (usually with some form of yield). For instance,
assuming the following in foo.erb:
Hey <%= yield %>!
The block passed to render is called on yield:
template = Tilt::ERBTemplate.new('foo.erb')
template.render { 'Joe' }
# => "Hey Joe!"
For template engines that always result in the same output for the same
template and do not accept local variables, scope class, or yield,
the Tilt::StaticTemplate class should be used instead of Tilt::Template.
Fixed Locals
By default, Tilt templates that support local variables can be called with
any locals, and a separate template method is compiled for each combination
of local variable names. This causes multiple issues:
- It is inefficient, especially for large templates that are called with
many combinations of locals. - It hides issues if unused local variable names are passed to the template
- It does not support default values for local variables
- It does not support required local variables
- It does not support cases where you want to pass values via a keyword splat
- It does not support named blocks
You can pass the :fixed_locals option when creating the template to fix the
local variables. This will only compile a single template method per template
(per scope class, see below).
The value of the :fixed_locals option is a Ruby method parameter string, which
should start and end with parentheses. For example, if the template does not
use local variables, you can set it to "()". This will cause an ArgumentError
to be raised if you call the template with locals:
template = Tilt::ERBTemplate.new('templates/foo.erb', fixed_locals: "()")
output = template.render(Object.new) # No ArgumentError
output = template.render(Object.new, x: 1) # ArgumentError
If the template must be passed the x local variable to work correctly, and
optionally can be provided the y local variable:
template = Tilt::ERBTemplate.new('templates/foo.erb', fixed_locals: "(x:, y: nil)")
output = template.render(Object.new) # ArgumentError
output = template.render(Object.new, x: 1) # No ArgumentError
output = template.render(Object.new, x: 1, y: 2) # No ArgumentError
output = template.render(Object.new, x: 1, y: 2, z: 3) # ArgumentError
If the template wants to accept arbitrary local variables, in order to pass
the variables to a method inside the template, you can provide a keyword splat
or a single positional argument (with an optional empty hash value if you want
to support being called with no local variables):
template = Tilt::ERBTemplate.new('templates/foo.erb', fixed_locals: "(**args)") # or "(args={})"
If you would like to name the block passed to the template, so you can pass
it to a method inside the template:
template = Tilt::ERBTemplate.new('templates/foo.erb', fixed_locals: "(&block)")
Embedded Fixed Locals
In many cases, Tilt is used in situations where you do not have direct control
over the options passed when creating each separate template. In these cases
and others, it can be helpful to embed the fixed locals inside the template
using a magic comment. This can be enabled using the :extract_fixed_locals
template option. It can also be enabled globally via:
Tilt.extract_fixed_locals = true
If :extract_fixed_locals option is given, or extraction is globally enabled,
and the :fixed_locals option is not provided when creating the template,
Tilt will scan the template code looking for a magic comment of
the form (whitespace around locals: is optional but recommended):
# locals: ()
In ERB templates, you can use the following comment format:
<%# locals: () %>
In string templates, it is a little ackward, but still possible (note that the
closing } goes on a separate line:
#{# locals: ()
}
If Tilt finds the magic comment, it will use it as fixed locals. To disable
the scanning for fixed locals even if Tilt.extract_fixed_locals = true is
set, pass the fixed_locals: false or extract_fixed_locals: false option.
When embedded fixed locals are supported, it can be useful to support a
default for fixed locals if they are not specified in the template. This
is useful mostly to default templates to not supporting local variables
without having to specify that in each template. Tilt support this via the
:default_fixed_locals option.
To recap, in order of preference, Tilt will use fixed locals from the
following sources:
:fixed_localstemplate option- embedded fixed locals magic comment (if
:extract_fixed_localstemplate
option is given orTilt.extract_fixed_locals = true) :default_fixed_localstemplate option
It is expected that embedded fixed locals magic comments will be supported
by default in Tilt 3 (i.e. Tilt.extract_fixed_locals will default to true).
:scope_class option
You can now specify the :scope_class option when creating the template, which
will fix the scope class for the template. By default, Tilt uses the class
of the provide scope, and will compile a separate method per scope class. By
using the :scope_class option to fix the scope class, and using fixed locals,
you can ensure only a single template method is compiled per Tilt::Template
instance.
Template Mappings
The Tilt::Mapping class includes methods for associating template
implementation classes with filename patterns and for locating/instantiating
template classes based on those associations.
The Tilt module has a global instance of Mapping that is populated with the
table of template engines above.
The Tilt.register method associates a filename pattern with a specific
template implementation. To use ERB for files ending in a .bar extension:
>> Tilt.register Tilt::ERBTemplate, 'bar'
>> Tilt.new('views/foo.bar')
=> #<Tilt::ERBTemplate @file="views/foo.bar" ...>
Retrieving the template class for a file or file extension:
>> Tilt['foo.bar']
=> Tilt::ERBTemplate
>> Tilt['haml']
=> Tilt::HamlTemplate
Retrieving a list of template classes for a file:
>> Tilt.templates_for('foo.bar')
=> [Tilt::ERBTemplate]
>> Tilt.templates_for('foo.haml.bar')
=> [Tilt::ERBTemplate, Tilt::HamlTemplate]
The template class is determined by searching for a series of decreasingly
specific name patterns. When creating a new template with
Tilt.new('views/foo.html.erb'), we check for the following template
mappings:
views/foo.html.erbfoo.html.erbhtml.erberb
Template Pipelines
In some cases, it is useful to take the output of one template engine,
and use it as input to another template engine. This can be useful
when a template engine does not support locals or a scope, and you
want to customize the output per different locals. For example, let's
say you have an scss file that you want to allow customization with
erb, such as:
.foo {
.bar {
.<%= hide_class %> {
display: none;
}
}
}
You can do this manually:
scss = Tilt.new("file.scss.erb").render(nil, hide_class: 'baz')
css = Tilt.new("scss"){scss}.render
A more automated way to handle it is to register a template pipeline:
Tilt.register_pipeline("scss.erb")
Then Tilt will automatically take the output of the erb engine,
and pass it to the scss engine, automating the above code.
css = Tilt.new("file.scss.erb").render(nil, hide_class: 'baz')
Finalizing Mappings
By default, Tilt::Mapping instances will lazy load files for template
classes, and will allow for registering an unregistering template classes.
To make sure this is safe in a multithreaded environment, a mutex is used
to synchronize access. To improve performance, and prevent additional lazy
loading of template classes, you can finalize mappings. Finalizing a mapping
returns a new finalized mapping that is frozen, cannot be modified, and will
not lazy load template classes not already loaded. Users of Tilt are
encouraged to manually require the template libraries they desire to use,
and then freeze the mappings. Tilt.finalize! will replace Tilt's default
mapping with a finalized versions, as well as freeze Tilt so that no
further changes can be made.
require 'tilt/erubi'
require 'tilt/string'
require 'tilt/sass'
Tilt.finalize!
Tilt['erb'] # => Tilt::ErubiTemplate
Tilt['str'] # => Tilt::StringTemplate
Tilt['scss'] # => Tilt::ScssTemplate
Tilt['haml'] # => nil # even if haml is installed
Encodings
Tilt needs to know the encoding of the template in order to work properly:
Tilt will use Encoding.default_external as the encoding when reading external
files. If you're mostly working with one encoding (e.g. UTF-8) we highly
recommend setting this option. When providing a custom reader block (Tilt.new { custom_string }) you'll have ensure the string is properly encoded yourself.
Most of the template engines in Tilt also allows you to override the encoding
using the :default_encoding-option:
tmpl = Tilt.new('hello.erb', :default_encoding => 'Big5')
Ultimately it's up to the template engine how to handle the encoding: It might
respect :default_encoding, it might always assume it's UTF-8 (like
CoffeeScript), or it can do its own encoding detection.
Template Compilation
Tilt compiles generated Ruby source code produced by template engines and reuses
it on subsequent template invocations. Benchmarks show this yields a 5x-10x
performance increase over evaluating the Ruby source on each invocation.
Template compilation is currently supported for these template engines:
StringTemplate, ERB, Erubi, Etanni, Haml, Nokogiri, Builder, CSV,
Prawn, and Yajl.
LICENSE
Tilt is distributed under the MIT license. See the COPYING file for more info.
Owner metadata
- Name: Jeremy Evans
- Login: jeremyevans
- Email:
- Kind: user
- Description: Ruby Committer. Author of "Polished Ruby Programming". Lead developer of Sequel, Roda, and Rodauth. OpenBSD ruby ports maintainer.
- Website: http://code.jeremyevans.net
- Location: Sacramento, California, USA
- Twitter: jeremyevans0
- Company:
- Icon url: https://avatars.githubusercontent.com/u/3846?u=42e153d8b1f8e31db8d838217fd6f849ad0f6b1c&v=4
- Repositories: 218
- Last ynced at: 2023-04-10T07:45:58.723Z
- Profile URL: https://github.com/jeremyevans
GitHub Events
Total
- Watch event: 21
- Delete event: 1
- Issue comment event: 14
- Push event: 20
- Pull request event: 10
- Fork event: 3
- Create event: 5
Last Year
- Watch event: 19
- Delete event: 1
- Issue comment event: 12
- Push event: 20
- Pull request event: 10
- Fork event: 3
- Create event: 5
Committers metadata
Last synced: 19 days ago
Total Commits: 918
Total Committers: 116
Avg Commits per committer: 7.914
Development Distribution Score (DDS): 0.734
Commits in past year: 42
Committers in past year: 3
Avg Commits per committer in past year: 14.0
Development Distribution Score (DDS) in past year: 0.095
| Name | Commits | |
|---|---|---|
| Magnus Holm | j****r@g****m | 244 |
| Jeremy Evans | c****e@j****t | 207 |
| Ryan Tomayko | r****o@g****m | 186 |
| Konstantin Haase | k****s@g****m | 44 |
| Joshua Peek | j****h@j****m | 16 |
| Joshua Muheim | j****h@j****h | 14 |
| Martin Fenner | m****r@d****g | 8 |
| Akira Matsuda | r****e@d****p | 7 |
| 7rans | t****e@g****m | 6 |
| Raphaël Pinson | r****n@c****m | 6 |
| Scott Taylor | s****t@r****m | 6 |
| Simone Carletti | w****s@w****t | 5 |
| Alex Gibbons | a****s@g****m | 5 |
| Tom May | t****m@t****t | 4 |
| Simon Chiang | s****g@g****m | 4 |
| Nathanael Jones | n****s@g****m | 4 |
| Kematzy | k****y@g****m | 4 |
| Davide D'Agostino | d****o@l****m | 3 |
| Marcin Domański | me@k****o | 3 |
| Aslak Knutsen | a****k@r****m | 3 |
| minad | m****l@d****e | 3 |
| なつき | i@n****e | 3 |
| Tim Riley | t****m@r****u | 3 |
| Tim Felgentreff | t****f@g****m | 3 |
| Matt Wildig | m****t@m****k | 3 |
| Nathan Esquenazi | n****a@g****m | 3 |
| Andrew Marshall | a****w@j****m | 3 |
| Jonas Mueller | j****s@t****d | 3 |
| HannesG | h****g@i****e | 3 |
| Dylan Egan | me@d****m | 3 |
| and 86 more... | ||
Committer domains:
- redhat.com: 2
- lifesnapz.com: 1
- simulacre.org: 1
- ozmm.org: 1
- binaryhex.com: 1
- hughbien.com: 1
- lacour.me: 1
- josephholsten.com: 1
- gusg.us: 1
- cogini.com: 1
- bamaru.de: 1
- dylanegan.com: 1
- informatik.uni-kiel.de: 1
- tigger.cloud: 1
- johnandrewmarshall.com: 1
- mattwildig.co.uk: 1
- riley.id.au: 1
- ntk.me: 1
- daniel-mendler.de: 1
- kabturek.info: 1
- lipsiasoft.com: 1
- tommay.net: 1
- weppos.net: 1
- railsnewbie.com: 1
- camptocamp.com: 1
- dio.jp: 1
- datacite.org: 1
- josh.ch: 1
- joshpeek.com: 1
- jeremyevans.net: 1
- keveney.com: 1
- suse.de: 1
- yandex.ru: 1
- hacktivista.org: 1
- schito.me: 1
- 10tok.net: 1
- chipcastle.com: 1
- thefrontside.net: 1
- benhollis.net: 1
- mediadrive.ca: 1
- entryway.net: 1
- mrvinn.com: 1
- baucloud.com: 1
- yahoo.co.jp: 1
- gun.io: 1
- maxedmands.com: 1
- nleger.com: 1
- neopoly.de: 1
- saimonmoore.net: 1
- comverge.com: 1
- pinnacol.com: 1
- shopify.com: 1
- magnificent-tears.com: 1
- fastignite.com: 1
- timcraft.com: 1
- artif.org: 1
- thisismedium.com: 1
- alexbcoles.com: 1
- ffiirree.com: 1
- univ.gda.pl: 1
Issue and Pull Request metadata
Last synced: 8 days ago
Total issues: 6
Total pull requests: 13
Average time to close issues: about 5 hours
Average time to close pull requests: 19 days
Total issue authors: 5
Total pull request authors: 6
Average comments per issue: 2.5
Average comments per pull request: 6.54
Merged pull request: 8
Bot issues: 0
Bot pull requests: 0
Past year issues: 1
Past year pull requests: 8
Past year average time to close issues: about 6 hours
Past year average time to close pull requests: about 1 month
Past year issue authors: 1
Past year pull request authors: 2
Past year average comments per issue: 2.0
Past year average comments per pull request: 7.38
Past year merged pull request: 3
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- voxik (2)
- adenta (1)
- momolog (1)
- oehlschl (1)
- AlexWayfer (1)
Top Pull Request Authors
- muellerj (6)
- rickenharp (2)
- unasuke (2)
- timriley (1)
- adam12 (1)
- minad (1)
Top Issue Labels
Top Pull Request Labels
Package metadata
- Total packages: 3
-
Total downloads:
- rubygems: 1,407,181,439 total
- Total docker downloads: 2,066,036,660
- Total dependent packages: 735 (may contain duplicates)
- Total dependent repositories: 898,062 (may contain duplicates)
- Total versions: 109
- Total maintainers: 1
gem.coop: tilt
Generic interface to multiple Ruby template engines
- Homepage: https://github.com/jeremyevans/tilt
- Documentation: http://www.rubydoc.info/gems/tilt/
- Licenses: MIT
- Latest release: 2.6.1 (published 5 months ago)
- Last Synced: 2025-12-07T04:32:17.967Z (5 days ago)
- Versions: 45
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 704,078,279 Total
- Docker Downloads: 1,033,018,330
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Downloads: 0.021%
- Average: 0.026%
- Docker downloads count: 0.084%
- Maintainers (1)
rubygems.org: tilt
Generic interface to multiple Ruby template engines
- Homepage: https://github.com/jeremyevans/tilt
- Documentation: http://www.rubydoc.info/gems/tilt/
- Licenses: MIT
- Latest release: 2.6.1 (published 5 months ago)
- Last Synced: 2025-12-03T16:04:26.879Z (9 days ago)
- Versions: 45
- Dependent Packages: 735
- Dependent Repositories: 898,062
- Downloads: 703,103,160 Total
- Docker Downloads: 1,033,018,330
-
Rankings:
- Dependent repos count: 0.011%
- Downloads: 0.019%
- Dependent packages count: 0.062%
- Docker downloads count: 0.114%
- Average: 4.048%
- Stargazers count: 9.011%
- Forks count: 15.072%
- Maintainers (1)
proxy.golang.org: github.com/jeremyevans/tilt
- Homepage:
- Documentation: https://pkg.go.dev/github.com/jeremyevans/tilt#section-documentation
- Licenses: mit
- Latest release: v2.6.1+incompatible (published 5 months ago)
- Last Synced: 2025-12-03T16:04:26.477Z (9 days ago)
- Versions: 19
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Stargazers count: 6.733%
- Average: 9.542%
- Dependent packages count: 9.576%
- Dependent repos count: 10.802%
- Forks count: 11.058%
Dependencies
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
Score: 30.98487733598176