2

Is it possible in Scriban to use the value from the variable, and then increment it and then use that value inside the sc_placeholder

If it is not possible using the basic default Scriban, maybe someone knows some extension that I can use with this.

This is what I mean by this:

{{- index = 0 -}}
{{if i_item.has_children}}
{{for i_child in i_item.children}}
<div class="section anchor-header" id="{{i_child.Anchor.raw}}">
<div class="container">
    <div class="row">
        <div class="col">
            <h2 class="anchor-header-title">
                {{i_child.Name}}
            </h2>
            <button type="button" class="anchor-header-button anchor-link">
                <svg class="icon icon-arrow" aria-hidden="true">
                    <use xlink:href="#icon-arrow"></use>
                </svg>
                TOP
            </button>
        </div>
    </div>
</div>
</div>
{{index++}}
{{sc_placeholder 'section-{{index}}' }}
{{end}}

{{end}}

With this code I am getting an error: Error while parsing unary expression: Expecting <expression> instead of "CodeExit" in: <operator> <expression>

1 Answer 1

3

I have tried this and not managed to get the ++ increment variable to work, but a simple index = index + 1 does work. Change your template to this:

{{- index = 0 -}}
{{~ if i_item.has_children ~}}
{{~ for i_child in i_item.children ~}}
<div class="section anchor-header" id="{{i_child.Anchor.raw}}">
<div class="container">
    <div class="row">
        <div class="col">
            <h2 class="anchor-header-title">
                {{i_child.Name}}
            </h2>
            <button type="button" class="anchor-header-button anchor-link">
                <svg class="icon icon-arrow" aria-hidden="true">
                    <use xlink:href="#icon-arrow"></use>
                </svg>
                TOP
            </button>
        </div>
    </div>
</div>
</div>
{{index = index + 1}}
{{sc_placeholder 'section-' + index}}
{{~ end ~}}
{{~ end ~}}

You'll notice too that I've added whitespace control around your loop and if statement, it helps generate cleaner markup in the rendered version.

Here is a .netfiddle to prove the template works https://dotnetfiddle.net/B9vgj4

Update

It looks like the ++ operator was added in a newer version of Scriban - not sure which version, but if you try the latest (5.4.1 at the time of writing) - your original code works fine. So looks like the version with SXA does not support that.

2
  • Thank you for the answer. The problem I am still having is that I get this placeholder section-{{index}} instead of section-1, section-2 and etc
    – Samke11
    Commented Mar 29, 2022 at 9:42
  • Try using string concatenation instead of an interpolated string. I have updated the answer to reflect.
    – Richard Seal
    Commented Mar 29, 2022 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.