## Set value of attribute to sum of other attributes?

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.

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}
``````

Thanks! I will do it that way.

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.

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:{@[email protected]_slug,[email protected]_slug}}
y = {y}
``````

The last line could also look like this:

``````{if x>0:{@[email protected]_slug}{@[email protected]_slug}}
``````

The original forum post explains it well.

i am a little bit confused by this

This topic is now closed. Topics are closed after 60 days of inactivity.

Quest
Squiffy
ActiveLit

Documentation
Blog
GitHub

Forums