Template engines power web frameworks, email systems, and code generators by dynamically inserting data into predefined structures. This drill teaches you to parse template syntax, substitute variables, handle loops, and generate dynamic content. You'll learn pattern matching with regular expressions, string manipulation, and data binding—essential skills for building web applications, code generators, and automation tools.
Use String#gsub with regex and block to process patterns
Regex for variables: /{{([\w.]+)}}/
Regex for each loops: /{{#each (\w+)}}(.*?){{/each}}/m
The /m flag enables multiline matching
Capture groups are accessed with $1, $2, etc.
Non-greedy match: .*? matches minimum characters
Split nested path: 'user.name'.split('.') => ['user', 'name']
template = "Hello {{name}}!"
data = {'name' => 'Alice'}
puts render_template(template, data)
Hello Alice!
template = "Items: {{#each items}}{{this}} {{/each}}"
data = {'items' => ['a', 'b', 'c']}
puts render_template(template, data)
Items: a b c
template = "User: {{user.name}}, Age: {{user.age}}"
data = {'user' => {'name' => 'Bob', 'age' => 30}}
puts render_template(template, data)
User: Bob, Age: 30
template = "Hello {{name}}! Your score: {{score}}"
data = {'name' => 'Carol'}
puts render_template(template, data)
Hello Carol! Your score:
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept