Create a static file that expands variables using the ERB class

I would like to use the ERB class to expand variables like the Rails html.erb file and save it as a static file.

The situation is, for example, each user has his or her favorite stylesheet settings in the preferences settings, but once set, the stylesheet itself is a static file dedicated to that user until there is a change. It is more efficient to read it as.

For example, write a stylesheet that contains these variables.

css:_style.css.erb


body {
  background-color: <%= bg_color %>;
}
.header {
  color: <%= title_color %>;
}
.text {
  color: <%= text_color %>
}

The preference settings read by ActiveRecord are If you have an object variable called @preference with @ preference.bg_color, @ preference.title_color, @ preference.text_color

style_erb = ERB.new(File.read("_style.css.erb"))
style_res = style_erb.result_with_hash(@preference.attributes)
File.write("style_#{@preference.user_id}.css", style_res)

You can get a static file with the variables expanded as follows.

At first, I thought it would simply expand the instance variable @preference, but I couldn't do that, probably because I was reading the template from a file, so I used the method I passed as hash.

Recommended Posts

Create a static file that expands variables using the ERB class
How to create a jar file or war file using the jar command
Easy way to create a mapping class when using the API
Create a jar file with the command
Reading a file using Java's Scanner class
How to create a class that inherits class information
Create a login authentication screen using the session function
Test a class that takes a Context and reads / writes a file
Create a MOB using the Minecraft Java Mythicmobs plugin | Preparation 1
About the File :: Stat class
[Java] Create a temporary file
Create a fortune using Ruby
Create a large number of records with one command using the Ruby on Rails seeds.rb file
[Swift5] How to create a .gitignore file and the code that should be written by default
Create a calendar from the Calendar class by specifying the year and month
[Android] Create a calendar using GridView
Create a Jetty project using Eclipse
Create a tomcat project using Eclipse
Upload a file using Java HttpURLConnection
Create a Java project using Eclipse
Create a filtering function using acts-as-taggable-on
A murmur about the utility class
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Ruby] Processing with multiple colons. A class that utilizes the module namespace.
Create a Tokyo subway map from the CSV file of station data.jp
[Java] Create a jar file with both compressed and uncompressed with the jar command
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
Sample program that returns the hash value of a file in Java
A story that was embarrassing to give anison file to the production environment
[Java] Implement a function that uses a class implemented in the Builder pattern