Skip to main content

render

Function render 

Source
pub fn render(
    content: &str,
    config_path: &Path,
    file_access: FileAccess,
) -> Result<Rendered, EjsError>
Expand description

Pre-process EJS tokens in a config file before JSON/YAML parsing.

Handles the patterns emitted by Mountebank and compatible tooling:

  • <% include 'path' %> — inline the referenced file (relative to the config file)
  • <%- stringify('path') %> — inline a file’s rendered contents, escaped for a JSON string
  • <%= process.env.VAR %> — substitute with the env var value (empty string if unset)
  • <%= process.env.VAR || 'default' %> — substitute with env var or the literal default

Any other tag — another <%= expr %>, a <% statement %>, <%- expr %>, <%# comment %>, a <% with no closing %> — fails the load, naming the tag and where it is (issue #1095). Stripping it would load a config that silently differs from the file.

Includes are expanded first, so an included file is templated like the document. Every other tag is then substituted by its own span in one pass, so text a substitution inserts — an env var’s value, a stringified file — is never scanned again as if it were template.

A <%= process.env.VAR %> whose variable is unset renders empty, as Mountebank’s does; each one is listed in Rendered::unset_env so a caller can say so. One with a || 'default' is not. A variable set to a value that is not valid Unicode is listed either way, because the value the author set is ignored.

§Errors

EjsError names the first tag that cannot be rendered and where it is.