Methods with parameter in Scala

109 views
Skip to first unread message

Mike Murray

unread,
Jan 30, 2014, 10:31:23 PM1/30/14
to mustac...@googlegroups.com
Hi,

I'm using Finatra which uses Mustache.java in Scala. I'm having trouble getting it to work with template functions that take a parameter. I'm not sure if it's a bug or if I'm misusing the library.

My view class that is fed to mustache.execute looks something like this:

class MyView {
  def paramTest(s: String): String = "You passed in: %s".format(s)
}

And I try to use the paramTest method in my template: {{#paramTest}}hello{{/paramTest}}
It *does not* work, nothing is printed out.

However, if I try a function without any parameters:

class MyView {
  def paramTest(): String = "You passed in: hardcoded"
}

And then use it like: {{paramTest}}
It *does* work as expected.

If this is a bug I will open an issue and create a test case, otherwise I would appreciate some pointers around what I'm doing wrong.

Also, in case it is relevant, Finatra sets the object handler to TwitterObjectHandler:
  mustacheFactory.setObjectHandler(new TwitterObjectHandler)

Thanks!

Sam Pullara

unread,
Jan 30, 2014, 10:46:04 PM1/30/14
to mustachejava
We don't support using methods with parameters directly. You will need to return a Function (Guava) object from the method if you want the content to be passed. Implement TemplateFunction if you want the return value to be reinterpreted as a template. 

Sam

---
Sent from Boxer | http://getboxer.com
--
You received this message because you are subscribed to the Google Groups "mustache.java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

onkar singh

unread,
Jun 16, 2015, 5:25:38 PM6/16/15
to mustac...@googlegroups.com
Hello Sam,

Does the guava function approach work ?  Because i am trying to make the function call happen, however it never gets executed.   I have omitted 'test' method because that has been taken from MustacheTest.java.  My output is always  "d"


public class CSSController {
   
public static class CSS {
         
public Function<String, String> c() {
             
return new Function<String, String>() {

                 
public String apply(String property) {
                       
return "test";
                   
}
         
};
   
}
 
}

 
@Test
 
public void testMethodVariable() {
 CSS css
= new CSS();
 test
("bar", "{{#c}}d{{/c}}", css);
 
}

Sam

unread,
Jun 30, 2015, 5:05:05 PM6/30/15
to mustac...@googlegroups.com, onkar....@gmail.com
Are you using 0.8 or 0.9? In 0.9 we have switched to  the Java 8 Function interface and dropped the dependency on guava.

Sam
Reply all
Reply to author
Forward
0 new messages