[SOLVED] Template variable has multiple values

Hey, I’m trying to generate anchor id’s and href’s dynamically. However, the same variable has multiple values.

In the typoscript file I have

anchorTop = ${Math.floor(Math.random() * 10000) + 1}
anchorBottom = ${Math.floor(Math.random() * 10000) + 1}

and the template:

<div id="{anchorTop}"></div>
<div class="container">
  <a href="#{anchorTop}" class="arrow-up-link"></a>
</div>
...
<div id="{anchorBottom}"></div>
<div class="container">
  <a href="#{anchorBottom}" class="arrow-up-link"></a>
</div>

This would end up translating into something like

<div id="3422"></div>
<div class="container">
  <a href="#642" class="arrow-up-link"></a>
</div>
...
<div id="5842"></div>
<div class="container">
  <a href="#1234" class="arrow-up-link"></a>
</div>

Instead of

<div id="3422"></div>
<div class="container">
  <a href="#3422" class="arrow-up-link"></a>
</div>
...
<div id="1234"></div>
<div class="container">
  <a href="#1234" class="arrow-up-link"></a>
</div>

Is there way to ensure anchorTop and anchorBottom have a consistent value?

How about something like node identifiers (if nodes are involved)?

You mean to set the two variables as properties on the node and then generate it? The reason why i didnt was because its not something that i want exposed or be editable

I’m not really getting why it does the calculations twice in the template

Hey,

so as you seemingly don’t have any specific requirements for the ids I thought you could just use the node identifiers, as I assume those divs each represent a node. Node identifiers are unique, so nothing to worry about.

Something like id="anchor-{node.identifier}"
(and same for the href).

For performance reasons we lazily evaluate EEL expressions when used in the template. That is in your case of course a drawback and I just tried several variants but there is no good way to work around this.

1 Like

Alright, this solution is perfect. Thanks for the help.