diff --git a/doc/reference/templates.md b/doc/reference/templates.md index a2f400c3..f0af6699 100644 --- a/doc/reference/templates.md +++ b/doc/reference/templates.md @@ -290,10 +290,11 @@ If an expression evaluates to a falsy value, it will not be set at all: It is sometimes convenient to format an attribute with string interpolation. In that case, the `t-attf-` directive can be used. It is useful when we need to mix -literal and dynamic elements, such as css classes. +literal and dynamic elements, such as css classes. The dynamic elements can be +specified with either `{{...}}` or `#{...}`: ```xml -
+ ``` diff --git a/src/compiler/code_generator.ts b/src/compiler/code_generator.ts index 766b4d06..f29d6ce9 100644 --- a/src/compiler/code_generator.ts +++ b/src/compiler/code_generator.ts @@ -1,4 +1,10 @@ -import { compileExpr, compileExprToArray, interpolate, INTERP_REGEXP } from "./inline_expressions"; +import { + compileExpr, + compileExprToArray, + interpolate, + INTERP_REGEXP, + replaceDynamicParts, +} from "./inline_expressions"; import { AST, ASTComment, @@ -606,11 +612,8 @@ export class CodeGenerator { this.target.hasRef = true; const isDynamic = INTERP_REGEXP.test(ast.ref); if (isDynamic) { - const str = ast.ref.replace( - INTERP_REGEXP, - (expr) => "${" + this.captureExpression(expr.slice(2, -2), true) + "}" - ); - const idx = block!.insertData(`(el) => refs[\`${str}\`] = el`, "ref"); + const str = replaceDynamicParts(ast.ref, (expr) => this.captureExpression(expr, true)); + const idx = block!.insertData(`(el) => refs[${str}] = el`, "ref"); attrs["block-ref"] = String(idx); } else { let name = ast.ref; diff --git a/src/compiler/inline_expressions.ts b/src/compiler/inline_expressions.ts index e4df91ac..cca8c2f4 100644 --- a/src/compiler/inline_expressions.ts +++ b/src/compiler/inline_expressions.ts @@ -355,15 +355,20 @@ export function compileExpr(expr: string): string { .join(""); } -export const INTERP_REGEXP = /\{\{.*?\}\}/g; -const INTERP_GROUP_REGEXP = /\{\{.*?\}\}/g; +export const INTERP_REGEXP = /\{\{.*?\}\}|\#\{.*?\}/g; -export function interpolate(s: string): string { +export function replaceDynamicParts(s: string, replacer: (s: string) => string) { let matches = s.match(INTERP_REGEXP); if (matches && matches[0].length === s.length) { - return `(${compileExpr(s.slice(2, -2))})`; + return `(${replacer(s.slice(2, matches[0][0] === "{" ? -2 : -1))})`; } - let r = s.replace(INTERP_GROUP_REGEXP, (s) => "${" + compileExpr(s.slice(2, -2)) + "}"); + let r = s.replace( + INTERP_REGEXP, + (s) => "${" + replacer(s.slice(2, s[0] === "{" ? -2 : -1)) + "}" + ); return "`" + r + "`"; } +export function interpolate(s: string): string { + return replaceDynamicParts(s, compileExpr); +} diff --git a/tests/compiler/__snapshots__/attributes.test.ts.snap b/tests/compiler/__snapshots__/attributes.test.ts.snap index 381886e8..941acce0 100644 --- a/tests/compiler/__snapshots__/attributes.test.ts.snap +++ b/tests/compiler/__snapshots__/attributes.test.ts.snap @@ -387,6 +387,20 @@ exports[`attributes static attributes with dashes 1`] = ` }" `; +exports[`attributes string interpolation, alternate syntax 1`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; + + let block1 = createBlock(\`