Module: Inquirex::Actions::Template
- Defined in:
- lib/inquirex/actions/template.rb
Overview
Renders {field} placeholders against collected answers.
Placeholders are dot-notation keys resolved via Answers#to_flat_h ("{email}", "{business{business.count}"). The syntax is deliberately inert — no code execution — so flow definitions stored in a database can be rendered server-side safely and a visual editor can preview templates client-side. It matches the {field} placeholder convention used by inquirex-llm prompts.
Rendering modes:
- render_text — values inserted verbatim
- render_html — every interpolated value is HTML-escaped
The built-in {answers_summary} placeholder expands to all collected answers: "key: value" lines in text mode, a simple table in HTML mode. Unknown fields render as empty strings; array values join with ", ".
Constant Summary collapse
- PLACEHOLDER =
Matches one {field} placeholder; capture 1 is the dot-notation key.
/\{\{\s*([\w.]+)\s*\}\}/- SUMMARY_KEY =
Reserved placeholder key that expands to a summary of all collected answers.
"answers_summary"
Class Method Summary collapse
-
.format_value(value) ⇒ String
Formats a single answer value for interpolation.
-
.render(string, answers, html:) ⇒ String
Shared implementation behind render_text and render_html.
- .render_html(string, answers) ⇒ String
- .render_text(string, answers) ⇒ String
-
.summary_html(flat) ⇒ String
Expands {answers_summary} in HTML mode.
-
.summary_text(flat) ⇒ String
Expands {answers_summary} in text mode.
Class Method Details
.format_value(value) ⇒ String
Formats a single answer value for interpolation.
71 72 73 |
# File 'lib/inquirex/actions/template.rb', line 71 def format_value(value) value.is_a?(Array) ? value.join(", ") : value.to_s end |
.render(string, answers, html:) ⇒ String
Shared implementation behind render_text and render_html.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/inquirex/actions/template.rb', line 56 def render(string, answers, html:) flat = answers.to_flat_h string.gsub(PLACEHOLDER) do key = Regexp.last_match(1) next(html ? summary_html(flat) : summary_text(flat)) if key == SUMMARY_KEY value = format_value(flat[key]) html ? CGI.escapeHTML(value) : value end end |
.render_html(string, answers) ⇒ String
46 47 48 |
# File 'lib/inquirex/actions/template.rb', line 46 def render_html(string, answers) render(string, answers, html: true) end |
.render_text(string, answers) ⇒ String
34 35 36 |
# File 'lib/inquirex/actions/template.rb', line 34 def render_text(string, answers) render(string, answers, html: false) end |
.summary_html(flat) ⇒ String
Expands {answers_summary} in HTML mode.
87 88 89 90 91 92 |
# File 'lib/inquirex/actions/template.rb', line 87 def summary_html(flat) rows = flat.map do |key, value| "<tr><th>#{CGI.escapeHTML(key)}</th><td>#{CGI.escapeHTML(format_value(value))}</td></tr>" end %(<table class="inquirex-answers">#{rows.join}</table>) end |
.summary_text(flat) ⇒ String
Expands {answers_summary} in text mode.
79 80 81 |
# File 'lib/inquirex/actions/template.rb', line 79 def summary_text(flat) flat.map { |key, value| "#{key}: #{format_value(value)}" }.join("\n") end |