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 %>
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
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 %>
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).
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.
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. -_-
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.