Generate correctly indented source code with the power of Ruby
Welcome to the Ruby implementation of Twofold.
Most template engines like Slim or Haml are focused towards HTML. ERB allows you to generate any text, but control structures are cumbersome.
We want to be able to read the target language and also the control language.
With the power of Ruby it's all very easy.
- indentation stacking
- control every generated line and every whitespace
- some unit tests
Unlike most template languages every line is Ruby unless it starts with on of:
|
- followed by output text, breaks the output line\
- followed by output text without breaking the output line=
- followed by ruby code
Output text can contain #{interpolated}
ruby code.
Whitespaces BEFORE these symbols are ignored. But whitespaces AFTER the symbols are used as output indentations. These indentations stack.
Let's look at an example:
def self.greet_method(name, greet)
|def greet_#{name}(name)
| puts("#{greet} \#{name}")
|end
return # be careful what you return.
end
|class Greeters
methods.each do |m|
= greet_method(m[:name], m[:greet])
end
|end
If we provide the following locals:
locals = {
methods: [
{ name: "hello", greet: "Hello World of " },
{ name: "tag", greet: "Guten Tag " },
]
}
Twofold will produce:
class Greeters
def greet_world(name)
puts("Hello World of #{name}")
end
def greet_tag(name)
puts("Guten Tag #{name}")
end
end
Notice how all the methods and their content are indented into the class, even though there is no indentation in the template?
This is because we call greet_method
with the two space indentation.
Add this line to your application's Gemfile:
gem 'twofold'
You simply create a Tilt template (Twofold::Template
)
require 'twofold'
Twofold::Template.new('template.twofold').render(scope)
After checking out the repo, run bundle install
to install dependencies.
Then, run rake spec
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/hicknhack-software/twofold.temple. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
Twofold is released under the terms of the MIT License.
Everyone interacting in the twofold project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.