iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📚

Personal Notes on ERB

に公開
2025/06/05

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

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.





GitHubで編集を提案

Discussion