Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

templates: Add {{#list_each}} helper.

Anders Kaseorg160785b155a6
diff --git a/tools/lib/template_parser.py b/tools/lib/template_parser.py
index 87306f5835d..4c046891bce 100644
--- a/tools/lib/template_parser.py
+++ b/tools/lib/template_parser.py
@@ -434,7 +434,7 @@ def validate(
# We are not completely rigorous about having a sensible
# order of if/elif/elif/else, but we catch obviously
# mismatching else tags.
- if start_tag not in ("if", "else", "unless"):
+ if start_tag not in ("if", "else", "unless", "list_each"):
return f"Unexpected else/elif tag encountered after {start_tag} tag."
elif start_tag != end_tag:
return f"Mismatched tags: ({start_tag} != {end_tag})"
diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.py
index e12eead5d13..815fd2fab75 100644
--- a/tools/linter_lib/custom_check.py
+++ b/tools/linter_lib/custom_check.py
@@ -707,7 +707,7 @@ html_rules: list["Rule"] = [
{
"pattern": "style ?=",
"description": "Avoid using the `style=` attribute; we prefer styling in CSS files",
- "exclude_pattern": r""".*style ?=["'](display: ?none|background: {{|color: {{|background-color: {{).*""",
+ "exclude_pattern": r""".*style ?=["'](display: ?none|background: {{|color: {{|background-color: {{).*|style=["'](narrow|long|short)["']""",
"exclude": {
# 5xx page doesn't have external CSS
"web/html/5xx.html",
diff --git a/web/src/templates.ts b/web/src/templates.ts
index c58b8fec8c9..bdf66e206a6 100644
--- a/web/src/templates.ts
+++ b/web/src/templates.ts
@@ -1,8 +1,11 @@
import Handlebars from "handlebars/runtime.js";
+import assert from "minimalistic-assert";
+import {z} from "zod/mini";
import * as common from "./common.ts";
import {default_html_elements, intl} from "./i18n.ts";
import {postprocess_content} from "./postprocess_content.ts";
+import {user_settings} from "./user_settings.ts";
// Below, we register Zulip-specific extensions to the Handlebars API.
//
@@ -173,3 +176,48 @@ Handlebars.registerHelper("popover_hotkey_hints", (...args) => {
`<span class="popover-menu-hotkey-hints">${hotkey_hints}</span>`,
);
});
+
+const list_format_options_schema = z.object({
+ style: z.optional(z.enum(["narrow", "long", "short"])),
+ type: z.optional(z.enum(["conjunction", "disjunction", "unit"])),
+});
+
+Handlebars.registerHelper(
+ "list_each",
+ function (this: unknown, context: unknown, options: Handlebars.HelperOptions) {
+ const {fn, inverse} = options;
+ const items_html: string[] = [];
+ let empty = false;
+ assert("each" in Handlebars.helpers);
+ const ret: unknown = Handlebars.helpers["each"].call(this, context, {
+ ...options,
+ fn(item_context: unknown, item_options?: Handlebars.RuntimeOptions) {
+ items_html.push(fn(item_context, item_options));
+ return "";
+ },
+ inverse(item_context: unknown, item_options?: Handlebars.RuntimeOptions) {
+ empty = true;
+ return inverse(item_context, item_options);
+ },
+ });
+ if (empty) {
+ return ret;
+ }
+ assert.equal(ret, "");
+ /* istanbul ignore if */
+ if (Intl.ListFormat === undefined) {
+ return items_html.join(", ");
+ }
+ return new Intl.ListFormat(
+ user_settings.default_language,
+ list_format_options_schema.parse(options.hash),
+ )
+ .formatToParts(items_html)
+ .map((part) =>
+ part.type === "element"
+ ? part.value
+ : Handlebars.Utils.escapeExpression(part.value),
+ )
+ .join("");
+ },
+);
diff --git a/web/tests/templates.test.cjs b/web/tests/templates.test.cjs
index ccc2961d9cb..2fa88b65656 100644
--- a/web/tests/templates.test.cjs
+++ b/web/tests/templates.test.cjs
@@ -2,12 +2,17 @@
const assert = require("node:assert/strict");
-const {set_global, with_overrides} = require("./lib/namespace.cjs");
+const {set_global, with_overrides, zrequire} = require("./lib/namespace.cjs");
const {run_test} = require("./lib/test.cjs");
const navigator = {};
set_global("navigator", navigator);
+const {initialize_user_settings} = zrequire("user_settings");
+
+const user_settings = {};
+initialize_user_settings({user_settings});
+
/*
Note that the test runner automatically registers
all of our handlers.
@@ -95,3 +100,28 @@ run_test("popover_hotkey_hints_shift_hotkey", () => {
const expected_html = `<span class="popover-menu-hotkey-hints popover-contains-shift-hotkey" data-hotkey-hints="${args.hotkey_one},${args.hotkey_two}"><span class="popover-menu-hotkey-hint">${args.hotkey_one}</span><span class="popover-menu-hotkey-hint">${args.hotkey_two}</span></span>\n`;
assert.equal(html, expected_html);
});
+
+run_test("list_each", ({override}) => {
+ assert.equal(
+ require("./templates/list_each.hbs")({stuff: [{name: "x"}, {name: "y"}, {name: "z"}]}),
+ `\
+<b>x</b>, <b>y</b>, and <b>z</b>
+<b>x</b> <b>y</b> <b>z</b>
+`,
+ );
+ assert.equal(
+ require("./templates/list_each.hbs")({stuff: {}}),
+ `\
+empty
+
+`,
+ );
+ override(user_settings, "default_language", "zh-Hans");
+ assert.equal(
+ require("./templates/list_each.hbs")({stuff: [{name: "水"}, {name: "粥"}, {name: "粥"}]}),
+ `\
+<b>水</b>、<b>粥</b>和<b>粥</b>
+<b>水</b><b>粥</b><b>粥</b>
+`,
+ );
+});
diff --git a/web/tests/templates/list_each.hbs b/web/tests/templates/list_each.hbs
new file mode 100644
index 00000000000..8655a93a65f
--- /dev/null
+++ b/web/tests/templates/list_each.hbs
@@ -0,0 +1,2 @@
+{{#list_each stuff}}<b>{{name}}</b>{{else}}empty{{/list_each}}
+{{#list_each stuff type="unit" style="narrow" as |item|}}<b>{{item.name}}</b>{{/list_each}}
diff --git a/web/webpack.config.ts b/web/webpack.config.ts
index 99de07ffe29..ad2514066a7 100644
--- a/web/webpack.config.ts
+++ b/web/webpack.config.ts
@@ -180,6 +180,7 @@ const config = (
"numberFormat",
"tooltip_hotkey_hints",
"popover_hotkey_hints",
+ "list_each",
],
precompileOptions: {
knownHelpersOnly: true,