iTranslated by AI
Personal Notes on ERB
Summary
I am leaving this note as part of my search for a template engine that is easy for me to use.
Introduction
Motivation for writing this document
There is a program called TheLocehiliosan/yadm: Yet Another Dotfiles Manager for managing dotfiles. This program has a feature called "templates" that allows for template expansion by receiving the OS, machine name, class (something like a schema you can define yourself), username, distro, etc., from where it was called. See Templates - yadm. Using this reduces the effort of having to go around editing files that aren't easily switchable; while symbolic links allow you to dynamically switch executable files, other files often require manual updates.
Currently, yadm supports a simple awk-based implementation, esh (a template engine made with sh), j2cli (Jinja2 for Python), and envtpl. I am writing this document as an aid to judge whether it's better to use one of those as the main tool or to use ERB. This is primarily for myself, and I expect to refer back to this when I want to dynamically generate configuration files.
Who is this document for?
Mainly myself and people interested in ERB. Also, those interested in methods for dynamically creating configuration files.
How to read this document
Since the main text alone might not make sense, please check the motivation and other sections as needed.
Main Content
Sample
The following is a modified version of a sample, exploring a way of writing that might be useful when using yadm.
In standard yadm, you can obtain the distro name but not the version, although you can get the machine name. Being able to group processes for multiple machine names using a case when clause is tedious with a naive awk implementation.
As mentioned in the yadm documentation, for things like shell scripts or ~/.vimrc that are evaluated upon execution, such a mechanism isn't necessary; you can just handle the branching yourself.
However, when specifying screen scales in files like ~/.Xresource, there will likely be parts you'll want to vary depending on the machine you're using.
I'll write more as I gain more insights. While there are many examples of it being used as heredocs within a program, I hope to find ways to use it effectively for template expansion.
<? xml version="1.0" ?>
<%# This is a comment %>
<% user = ENV["USER"] %>
<% require 'prime' -%>
<erb-example>
<calc><%= 1+1 %></calc>
<var><%= __FILE__ %></var>
<library><%= Prime.each(10).to_a.join(", ") %></library>
<% if user == "yabuki" %> hello yabuki <% else %> hello you <% end %>
<% case user
when "yabuki" then %>
hello yabuki
<% when "yukiharu" %>
hello yukiharu
<% else %>
hello you
<% end %>
<% machine = `hostname`.chomp %>
<%= $?.to_s %>
<%= machine %>
<% case machine
when "Orlanth", "Odayla" %>
you are in Debian/Sid.
<% else %>
you are in Debian/Buster.
<% end %>
</erb-example>
Run as follows:
erb -T - examples.erb
<? xml version="1.0" ?>
<erb-example>
<calc>2</calc>
<var>examples.erb</var>
<library>2, 3, 5, 7</library>
hello yabuki
hello yabuki
pid 3878586 exit 0
Orlanth
you are in Debian/Sid.
</erb-example>
Regarding the issue of blank lines being generated, it's sometimes not a problem in configuration files, so I'll leave it as is until I encounter a real issue. It might be related to the erb -T option and how to use it. It's not ideal that writing comments increases the number of empty lines. Also, since this is automatically generated, some might argue that you should just modify yadm.source instead.
There are still things about ERB I don't fully understand, such as where and how lines starting with % instead of <% are effective, and how to use the -T option. I'll add more info as I figure it out.
Referenced Documents
-
Introduction to Standard Libraries [Part 10] ERB
- I'll read the documents that can be found online in a chain-reaction fashion starting from this article, which Mr. Nishiyama wrote a long time ago.
- ERB
- the erb way
- ERB More
-
class ERB - Documentation for Ruby 2.6.0
- As of 2021-06-19, the Ruby version in Debian Sid is
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-linux-gnu], so I referred to the documentation for the closest version.
- As of 2021-06-19, the Ruby version in Debian Sid is
-
Using Heredocs in Ruby - Qiita
- I didn't quite understand how to read the
<<-(hyphen) notation, so I learned its meaning from this article, which I found by searching for 'ruby here document'. - Literals (Ruby 2.6.0 Reference Manual) Now that I understood the terminology from the above, I also referred to the heredoc section here to cross-check.
- Using
<<~(tilde) might have made the.gsub(/^ /, '')or.gsub(/^ */, "")processing unnecessary.
- I didn't quite understand how to read the
-
- I learned from this gist that this is how to write
case whenin ERB.
- I learned from this gist that this is how to write
Potentially Related Documents
-
k0kubun/mruby-erb: Direct port of ERB from CRuby -> MRuby
- Port from CRuby in mruby.
-
- According to the documentation, mitamae also appears to be able to use ERB.
- see also Using ERB as a template for generating configuration files in Docker containers - Qiita
- This article explains that since mruby alone cannot use ERB, mitamae is used instead. It describes it as a "busybox-like" or "kitchen sink" approach—the "relatively small single binary is convenient" method.
Acknowledgements
To the author of ERB and to those who maintain the documentation related to ERB.
Conclusion
| Item | Date |
|---|---|
| Date written | 2021-06-19 |
| Date modified | 2024-10-23 |
The information above is provided by the author as an aid to gauge the freshness of this article.
For a detailed change history, please refer to GitHub - yabuki/friendly-potato: zenn-contents.
I would appreciate it if you could submit a pull request for any typos in the article. Acceptance will be determined based on the diff and the description in the pull request.
Discussion