Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Math operations in silverstripe


Go to End


5 Posts   3664 Views

Avatar
markkkkkk

Community Member, 3 Posts

22 May 2015 at 10:15pm

How do you perform mathematical operations (i.e. addition, multiplication) in the .ss file?

works fine:
<% if $foo > $bar %><% end_if %>

I wanna perform this:
<% if $foo > $bar*0.5 %><% end_if %>

Avatar
Pyromanik

Community Member, 419 Posts

22 May 2015 at 10:28pm

Edited: 23/05/2015 7:43am

You don't.

Putting application logic into the view like this is bad form. You should make an aptly named getter on your model (or controller, whichever is more applicable).

Avatar
Willr

Forum Moderator, 5523 Posts

22 May 2015 at 10:28pm

At this stage, you have to keep operations like that out of your template. You should create a method on your object which contains the logic and call that.

<% if HasGreaterFoo %>
..

function HasGreaterFoo() {
return ($this->Foo > $this->Bar;
}

Make sure you have that on the correct scope. If you don't want object orientated methods you could pass in arguments $Top.HasGreaterArg($Foo, $Bar) and put that on your top level controller class.

Avatar
markkkkkk

Community Member, 3 Posts

25 May 2015 at 2:38pm

Thanks for the replies.. but I got another problem with the solutions.
What I have is this in my phpfile,

class Foo extends DataObject
{
...'foogreater'=>'float',...
}

class Bar extends DataObject
{
...'barlesser'=>'float,...
}

how do I connect/perform the
function HasGreaterFoo(){
return ($this->foogreater > $this->barlesser*0.5);
}

Thanks guys. appreciate it. -_-

Avatar
Pyromanik

Community Member, 419 Posts

26 May 2015 at 9:46am

Edited: 26/05/2015 9:48am

It might be a good idea for you to go through the tutorials if you're not familiar with MVC webapps.
If you are, every object that extends ViewableData (so all DataObjects & Controllers) doubles as a ViewModel.