Matiss
Matiss

Reputation: 5349

SilverStripe arithmetic in template

I want to do a simple arithmetic operation in a .ss template.

<% loop $Images %>
    <img src="$Link" alt=""/>
    <% $Pos == 4 %>
        and {$TotalItems - 4} more foto's 
        $Break
    <% end_if %>
<% end_loop %>

For example I would want to output

and 10 more foto's

But the best I can get is

and 14 - 4 more foto's

I know I can make a function, which works for now, but can I do arithmetic operations in the template?

Upvotes: 2

Views: 392

Answers (1)

csy_dot_io
csy_dot_io

Reputation: 1199

You could do it like this.

At first you limit the images to the amount you would like to display. After that you loop over the same set with an offset of x (4) and check if there's more. If so, output the amount of remaining images.

<% loop $Images.Limit(4,0) %>
   <img src="$Link" alt=""/>
<% end_loop %>
<% if $Images.Limit(9999,4) %>
  and $Images.Limit(9999,4).Count more foto's 
<% end_if %>

the code is untested but should work.

Edit

I think "real" arithmetic is not possible by default. You'll need to write a custom function to do this.

Upvotes: 4

Related Questions