Does play 2.0 template engine support the simple calculation in the html page.
Let us say, I create a sum.scala.html page:
@(a:String, b: String)
<html>
<head></head>
<body>
<h1> answer is getSum(@a,@b) </h1>
</body>
</html>
Is there any way that we could "getSum of a and b" via some function? or Does any play 2.0 expert know any good idea about calculation in play 2.0 template engine? Thanks
Have you tried @(a.toInt + b.toInt)
?
You can just pass the value to template, can't you ?
@(a:String, b: String, c: String)
<h1> answer for @a + @b is @c </h1>
You can also call function from Yourcontroller
in the template:
@Yourcontroller.getSum(a,b);
In /app/controllers/Yourcontroller.java
ad the function (simplest sample):
public static Integer getSum(String a, String b){
Integer c = Integer.valueOf(a) + Integer.valueOf(b);
return c;
}
If you want to refer to the result multiple times, you can use defining
:
@defining(a.toInt + b.toInt) { sum =>
<h1>The sum is @sum</h1>
The sum of @a + @b is @sum
}
For reuse of your code in scala html template. You can make a template function in template for this.
@sum(a:Long,b:Long) = {
@(a + b)
}
And call this in your template as normal functions. Like
@sum(2,3)