Rcurse is a super simple template engine written in Ruby.
Install it with gem install rcurse
,
add gem "rcurse"
to your Gemfile
and run bundle install
Require it with require 'rcurse'
.
Call Rcurse::render_file("file.rcurse", "file.html")
to render file.rcurse
to file.html
.
You can also render a template from a string: Rcurse::render("rcurse template")
.
You can use Rcurse from command line: rcurse <input file> <output file>
For example:
rcurse index.rcurse index.html
Wrap your Ruby code in {%
and %}
to evaluate it. You can use it to set variables or do other stuff. This doesn't output anything to the template, if you want to output the result of your code, use {%=
instead of {%
, like {%= "trololo".upcase %}
.
Using variables:
{% name = "Hodor" %}
<h1>Hello, {%= name %}!</h1>
Renders as:
<h1>Hello, Hodor!</h1>
Context contains a binding for template variables, and a path of current template if Rcurse is rendering a file.
Use context.eval
to evaluate Ruby code.
context.eval "a = 1"
See context.path
for the path of currently rendered template.
Helpers are like functions you can call in your templates. Use them by inserting {{helper-name arguments}}
in source files.
include filename
- include another file, like so:{{include header.rcurse}}
Use Rcurse::Helper.new(name) { |args, context| do_something }
to create a helper, your block can accept a single array of arguments and a context.
my_helper = Rcurse::Helper.new("my_helper") do |arguments, context|
# do something with arguments and return a string containing the new content
end