A platform for community discussion. Free, open, simple.
FEATURE: Use Liquid for workflow template rendering (#40602)
Joffrey JAFFEUX237ed2ae3e3a
diff --git a/Gemfile b/Gemfileindex b064700fbcb..2c04756f3cb 100644--- a/Gemfile+++ b/Gemfile@@ -74,6 +74,7 @@ gem "email_reply_trimmer"gem "image_optim"gem "multi_json"gem "mustache"+gem "liquid", "5.12.0"gem "nokogiri"gem "loofah"gem "css_parser", require: falsediff --git a/Gemfile.lock b/Gemfile.lockindex 3612464497f..c7f26ff5cb7 100644--- a/Gemfile.lock+++ b/Gemfile.lock@@ -319,6 +319,9 @@ GEMlibv8-node (24.12.0.1-x86_64-linux)libv8-node (24.12.0.1-x86_64-linux-musl)lint_roller (1.1.0)+ liquid (5.12.0)+ bigdecimal+ strscan (>= 3.1.1)listen (3.10.0)loggerrb-fsevent (~> 0.10, >= 0.10.3)@@ -867,6 +870,7 @@ DEPENDENCIESjsonjson_schemerlandlock+ liquid (= 5.12.0)listenlogragelogstash-event@@ -1101,6 +1105,7 @@ CHECKSUMSlibv8-node (24.12.0.1-x86_64-linux) sha256=e1236be4765edc109bd6ecef2989cf781bdcbac6f6e2cb3236ec9568540a9cfalibv8-node (24.12.0.1-x86_64-linux-musl) sha256=7563175534c40893815bfd28e8fe27e12ff1656700c7208f53de06d14001712blint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87+ liquid (5.12.0) sha256=5a3c2c2430cd925d21c53e4ed9abea52cd0a9da53b541422f81dee79aca2a673listen (3.10.0) sha256=c6e182db62143aeccc2e1960033bebe7445309c7272061979bb098d03760c9d2literate_randomizer (0.4.0) sha256=05073c9b383983b1ed7e26c40b963468e91bc86e663b3eeff3a4af91b84217b1logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex 5673782aa08..632ce76cf59 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -201,7 +201,7 @@ en:"action:limit": "Keep only the first or last N items""action:log": "Record key/value data to the execution log without affecting the workflow""action:markdown_table": "Build a markdown table from a list of items"- "action:template": "Render text with a Mustache template"+ "action:template": "Render text with a template""action:respond_to_webhook": "Send a custom HTTP response to the webhook caller""trigger:topic_admin_button": "Adds a button to the topic admin menu that triggers this workflow""trigger:error": "Fires when another workflow fails"@@ -724,8 +724,8 @@ en:mode_run_once_for_each_item: "Run once for each item"mode_run_once_for_all_items: "Run once for all items"template: "Template"- template_description: "In all-items mode, use <code>{{#items}}...{{/items}}</code> to loop over items. Inside the loop each item is available, and <code>item_index</code> is the current loop index starting at 1. In each-item mode, the current item is available at the top level. For example, if you have topic as input, you can access the title with <code>{{topic.title}}</code>. <code>items_count</code>, <code>vars</code>, <code>workflow</code>, <code>execution</code>, and <code>site_settings</code> are also accessible. Workflow expressions are not evaluated inside this field. If you need to modify your items, use the Set fields node before the template node."- template_placeholder: "Hello name"+ template_description: "In all-items mode, use loop syntax <code>{% for item in items %}...{% endfor %}</code>. Inside the loop, prefix item fields with <code>item.</code>, for example <code>{{ item.topic.title }}</code>. The loop index is available as <code>item.item_index</code>. In each-item mode, use the same <code>item.</code> prefix for the current item. <code>items_count</code>, <code>vars</code>, <code>workflow</code>, <code>execution</code>, and <code>site_settings</code> are also accessible. Workflow expressions are not evaluated inside this field. If you need to modify your items, use the Set fields node before the template node."+ template_placeholder: "Hello item.name"form:page_type: "Page type"page_type_page: "Next form page"diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.ymlindex 72d3a73464f..9101e81264d 100644--- a/plugins/discourse-workflows/config/locales/server.en.yml+++ b/plugins/discourse-workflows/config/locales/server.en.yml@@ -70,7 +70,7 @@ en:json_blank: "JSON string is blank."json_must_be_object: "JSON must be an object."template:- invalid_template: "Invalid Mustache template"+ invalid_template: "Invalid template"http_request:url_required: "URL is required."unsupported_scheme: "Only HTTP and HTTPS URLs are supported."diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rbindex 3bca28f54ce..7a66b011cc0 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb@@ -1,17 +1,17 @@# frozen_string_literal: true-require "mustache"+require "liquid"module DiscourseWorkflowsmodule Nodesmodule Templateclass V1 < NodeType- DEFAULT_TEMPLATE = <<~MUSTACHE+ DEFAULT_TEMPLATE = <<~LIQUIDItems:- {{#items}}- - {{item_index}}: {{name}}- {{/items}}- MUSTACHE+ {% for item in items -%}+ - {{ item.item_index }}: {{ item.name }}+ {% endfor -%}+ LIQUIDRUN_ONCE_FOR_ALL_ITEMS = "runOnceForAllItems"RUN_ONCE_FOR_EACH_ITEM = "runOnceForEachItem"@@ -120,8 +120,8 @@ module DiscourseWorkflowsenddef render_template(template, context)- Mustache.render(template, context)- rescue Mustache::Parser::SyntaxError => e+ Liquid::Template.parse(template).render!(context)+ rescue Liquid::Error => eraise_node_error!(I18n.t("discourse_workflows.errors.template.invalid_template"),description: e.message,@@ -140,11 +140,9 @@ module DiscourseWorkflowsenddef item_template_context(base_context, input_item, item_index)- input_item- .fetch("json") { {} }- .deep_stringify_keys- .merge(base_context)- .merge("item" => input_item.deep_stringify_keys, "item_index" => item_index + 1)+ item = template_item(input_item, item_index)++ item.merge(base_context).merge("item" => item, "item_index" => item_index + 1)enddef site_settings_context@@ -164,13 +162,17 @@ module DiscourseWorkflowsdef input_items(exec_ctx)exec_ctx.input_items.map.with_index do |input_item, item_index|- input_item- .fetch("json") { {} }- .deep_stringify_keys- .merge("item" => input_item.deep_stringify_keys, "item_index" => item_index + 1)+ template_item(input_item, item_index)endend+ def template_item(input_item, item_index)+ input_item+ .fetch("json") { {} }+ .deep_stringify_keys+ .merge("item" => input_item.deep_stringify_keys, "item_index" => item_index + 1)+ end+def paired_items(exec_ctx)exec_ctx.input_items.map { |item| exec_ctx.paired_item_for(item) }enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rbindex e10f1e91d72..3a8b6514e66 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb@@ -43,7 +43,7 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 doit "renders a single output from all input items" doresult =execute_template(- "{{#items}}{{name}} {{/items}}",+ "{% for item in items %}{{ item.name }} {% endfor %}",input_items: [{ "json" => { "name" => "Alice" } }, { "json" => { "name" => "Bob" } }],)@@ -55,7 +55,7 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 doresult =execute_template(- "{{name}} {{item_index}}/{{items_count}} {{site_settings.title}}",+ "{{ item.name }} {{ item.item_index }}/{{ items_count }} {{ site_settings.title }}",mode: "runOnceForEachItem",input_items: [{ "json" => { "name" => "Alice", "item_index" => 99 } },@@ -93,10 +93,10 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 doresult =execute_template(- "{{#items}}{{name}} {{/items}}" \- "count={{items_count}} var={{vars.project}} " \- "workflow={{workflow.name}} execution={{execution.id}} " \- "{{#items}}item={{item.json.name}} index={{item_index}} {{/items}}",+ "{% for item in items %}{{ item.name }} {% endfor %}" \+ "count={{ items_count }} var={{ vars.project }} " \+ "workflow={{ workflow.name }} execution={{ execution.id }} " \+ "{% for item in items %}item={{ item.item.json.name }} index={{ item.item_index }} {% endfor %}",input_items: input_items,vars: {"project" => "Workflows",@@ -113,24 +113,24 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 doendit "exposes site settings and filters private settings" do- SiteSetting.title = "Mustache Forum"+ SiteSetting.title = "Liquid Forum"result =execute_template(- "title={{site_settings.title}} " \- "secret={{site_settings.discourse_connect_secret}} " \- "hidden={{site_settings.vapid_public_key}}",+ "title={{ site_settings.title }} " \+ "secret={{ site_settings.discourse_connect_secret }} " \+ "hidden={{ site_settings.vapid_public_key }}",)expect(result.first["json"]).to eq(- "template" => "title=Mustache Forum secret=[FILTERED] hidden=[FILTERED]",+ "template" => "title=Liquid Forum secret=[FILTERED] hidden=[FILTERED]",)endit "emits only the template field" doresult =execute_template(- "{{#items}}{{name}}{{/items}}",+ "{% for item in items %}{{ item.name }}{% endfor %}",input_items: [{ "json" => { "name" => "Alice", "extra" => "ignored" } }],)@@ -140,7 +140,7 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 doit "links the output item to every source item" doresult =execute_template(- "{{#items}}{{name}}{{/items}}",+ "{% for item in items %}{{ item.name }}{% endfor %}",input_items: [{ "json" => { "name" => "Alice" } }, { "json" => { "name" => "Bob" } }],)@@ -148,19 +148,19 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 doendit "renders missing variables as blank strings" do- result = execute_template("Hello {{missing}}")+ result = execute_template("Hello {{ missing }}")expect(result.first["json"]).to eq("template" => "Hello ")end- it "uses default Mustache escaping rules" do+ it "uses default Liquid output rules" doresult =execute_template(- "{{#items}}{{value}} {{{value}}}{{/items}}",+ "{% for item in items %}{{ item.value }}{% endfor %}",input_items: [{ "json" => { "value" => "<b>bold</b>" } }],)- expect(result.first["json"]).to eq("template" => "<b>bold</b> <b>bold</b>")+ expect(result.first["json"]).to eq("template" => "<b>bold</b>")endit "defaults to a template showing item loop syntax" do@@ -176,10 +176,10 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 doTEXTend- it "raises a node error for invalid Mustache syntax" do- expect { execute_template("{{#items}}") }.to raise_error(+ it "raises a node error for invalid Liquid syntax" do+ expect { execute_template("{% for item in items %}") }.to raise_error(DiscourseWorkflows::NodeError,- /Invalid Mustache template/,+ /Invalid template/,)end