Set value of attribute to sum of other attributes?

loopernow
24 Jun 2018, 22:12

Hello again,

I was wondering if I can do math operations using attributes as the things added/subtracted/multiplied/divided together. Also, what would the syntax be inside and outside of if/then clauses?

For example, this is what I tried that didn't work:

@set pod_slug=0
@set printer_slug=0
@set y = 7
{if x>0:{@y=pod_slug+printer_slug}} (it's this line that doesn't work--I'm trying to add the values of pod_slug and printer_slug together and give that sum to y as its value )
y = {y}


K.V.
24 Jun 2018, 22:55

I can only make it work with JS (and I'm cheating and setting x to 1 in my test code):

@set pod_slug=0
@set printer_slug=0
@set y = 7
@set x = 1
    if (squiffy.get("x")>0){
      squiffy.set("y", squiffy.get("pod_slug") + squiffy.get("printer_slug"));
    };
{y}

loopernow
25 Jun 2018, 13:38

Thanks! I will do it that way.


Richard Headkid
25 Jun 2018, 19:27

You're welcome!

I tried to make it work the other way, but I was utterly defeated. I don't know if the values are converted to strings using the other method or not, but it sure seemed like that's what was happening.


loopernow
13 Jul 2018, 03:25

Hello! After looking more closely at the forum post that explained the new functionality that was put into Squiffy 5.1.2 (here), I was able to figure out how to do it the alternate way outlined there. It's a little bit weird-looking, and a little bit fussy about syntax too, but fairly easy to understand, and less verbose.

(It's mentioned there that @set is always processed at the beginning of a section, even if you've written it into the middle or end of same. Something to remember.)

Below, after setting initial attribute values, y is set to the sum of pod_slug and printer_slug, inside an if statement.

@set pod_slug=0
@set printer_slug=4
@set y = 7
@set x = 1
{if x>0:{@y=@pod_slug,y+=@printer_slug}}
y = {y}

The last line could also look like this:

{if x>0:{@y=@pod_slug}{@y+=@printer_slug}}

The original forum post explains it well.


ahmed22
28 Jul 2018, 08:35

i am a little bit confused by this