Static format string (scripty)

I would like to create a formatted string with a static format:

In Zig, it would be:

print("Hello ({})", .{name});

My first attempt was this:

<p :text="'Hello ({})'.fmt($ctx.name)"></p>

But that wasn’t started with a $, so obviously it didn’t work. Second attempt:

<p :text="$'Hello ({})'.fmt($ctx.name)"></p>

Doesn’t work (expected a variable name).

I had to fallback to my original version (which is kind of ok for this case, but would get very unwieldy for format strings with more placeholders):

<p :text="$ctx.name.prefix('Hello (').suffix(')')"></p>

Is there a way to do this? Maybe call String.fmt('Hello ({})', $ctx.name) directly?

1 Like

I am looking for a way to do this too :slight_smile:

To avoid an XY problem, what I’m trying to achieve is changing the color used in an inline style e.g., style="border-color: var(--{});"because each element in a loop is supposed to get a different color and I don’t want to write a different class just for this.

I also want to add different classes e.g., class="base-class {}" .

It sounds like this can only work if I define these “base strings” somewhere else e.g., ziggy.zine , then bind them in ctx and format with the data coming in from the loop?

I think this Appropriate way to build a string in scripty - #3 by richardlehane answers the question.

What I’m doing right now is to add the “base string” with the classes I always want as a variable in `page.custom` and then work with it `$page.custom.get(‘my-base-string’).suffix(‘more-stuff’)`.