Writing Templates

To use ERB, we need to learn two things. First, we need to learn how to write the content of our ERB templates and how to use flow control, conditionals, and other Ruby constructs within them. And second, we need to learn how to actually execute these templates from within our Ruby code, including how to pass data into them.

ERB files are, in essence, plain text files. You don’t need to use any special invocations or formatting; any plain text file is, by definition, a valid ERB file. If we had a file containing the following text:

 
Hello, world

it would, when compiled as ERB, produce the output Hello, world. This is the key advantage of using a templating language like ERB: boilerplate content—such as the address in our ...

Get Text Processing with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.