ERB Rendering¶
ERB templates are evaluated on demand when to_s is called.
Basics¶
Any <%= expression %> in the content is evaluated as Ruby:
ERB is full Ruby. Anything a template can express, it can run. See Security.
Template Parameters¶
Parameters declared in the YAML front matter are available as local methods inside ERB:
parsed = PM.parse('greeting.md')
parsed.to_s #=> "Hello, World!"
parsed.to_s('name' => 'Alice') #=> "Hello, Alice!"
See Parameters for required vs default values, name resolution, and strict mode.
Registered Directives¶
Custom directives registered with PM.register are also available in ERB:
See Custom Directives for the full guide.
Built-in Directives¶
PM ships three built-in directives:
include-- Parse and render another prompt file in place. See Including Files.insert-- Copy any file in verbatim.read-- Alias ofinsert.
Disabling ERB¶
Set erb: false in the file's YAML metadata:
When ERB is disabled:
to_sreturns the content without template evaluation- Parameters are ignored (missing required parameters are not an error)
- Directives do not execute
metadata.includesis an empty array afterto_s
Global Default¶
Per-file metadata overrides the global setting unless the configuration is locked
with PM.config.lock!. See Configuration.
Pipeline Position¶
ERB rendering is the last stage of the pipeline, executed on demand:
- Strip HTML comments
- Extract YAML metadata
- Shell expansion (at parse time)
- ERB rendering (at
to_stime)
This means shell variables are already expanded before ERB sees the content. If content contains both $VAR and <%= param %>, the shell variable is resolved first, then ERB fills in the parameters. Command output cannot smuggle ERB into stage 4: any <% a command prints is escaped during stage 3.
Errors¶
| Situation | Error |
|---|---|
| Required parameter missing | PM::MissingParameterError |
| Unknown name in strict mode | PM::UnknownParameterError |
| Unknown name called with arguments, strict mode off | NoMethodError |
| ERB that does not compile | SyntaxError |
For a prompt read from a file, including one pulled in with include, the
backtrace of an error raised inside ERB names that file (/path/to/inner.md:2)
instead of (erb):2. Line numbers count from the first line after the front
matter. String-parsed prompts still show (erb).