How to refer to helper object's method?

1,087 views
Skip to first unread message

Vadym

unread,
Aug 18, 2013, 2:37:04 AM8/18/13
to mustac...@googlegroups.com
Hi,

I'm evaluating mustache.java integrated with SpringMVC for migrating off of a legacy Java stack, where there are plenty of view helper classes for each view. In an attempt to reduce migration scope, I put all those helper objects into a map along with model data, i.e:

map.put("myHelper", myHelper);
map.put("myVar", myVar);

, then tried referring those helper methods passing them variable in two ways:

{{#myHelper.myMethod}}
    {{myVar}}
{{/myHelper.myMethod}}

, and 

{{#myHelper}}
    {{#myMethod}}
        {{myVar}}
    {{/myMethod}}
{{/myHelper}}

In both cases it failed to find myMethod reference. Is reference to an Object's method supported at all? Also, how would I make sure myVar is pulled from correct scope?

Thanks!
Vadym

Sam Pullara

unread,
Aug 19, 2013, 1:16:35 AM8/19/13
to mustac...@googlegroups.com
That should work fine. Can you share what one of those objects looks like?

Sam
--
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.

Vadym

unread,
Aug 19, 2013, 4:05:30 PM8/19/13
to mustac...@googlegroups.com



Hi Sam,

Thx for the prompt reply. This is what the helper class looks like:

public class MyHelper {

public MyHelper() {}


public String myMethod(String myVar) {

return myVar + "updated";

}

}


There is nothing fancy. Nevertheless, it fails to find the method. Am I passing variable correctly in the Mustache syntax? Are there any sample unit tests (passing variable to a method), which I could look at?

Thanks,
Vadym

Sam Pullara

unread,
Aug 19, 2013, 4:08:41 PM8/19/13
to mustac...@googlegroups.com
Right now references in the mustache need to be either fields or methods that don't take a value. Can you describe what you think should have happened?

Thanks,
Sam

Vadym

unread,
Aug 19, 2013, 8:44:31 PM8/19/13
to mustac...@googlegroups.com
Sam,

I'm trying to migrate implementation of some common pieces of a web page. For instance, today, when we include js/css resources in our template we make a call similar to

urlUtilities.resolve("/js/homePage.js", isAkamized)

I thought I could call the same urlUtilities helper with parameters in the template, because JS/CSS resource inclusions seem to belong to the view/template rather than the data model. Can you suggest an alternative? How does Twitter handle JS/CSS inclusions?

-V

Sam Pullara

unread,
Aug 19, 2013, 10:04:50 PM8/19/13
to mustac...@googlegroups.com
The best bet for using mustache is to have this kind of layering:

template -> view code -> data model

At Twitter (and at Bagcheck where I first built this impl), each template is backed by related view code that does things like this. I have a boring article on this:


Sam

Vadym

unread,
Aug 20, 2013, 1:51:46 PM8/20/13
to mustac...@googlegroups.com
Sam,

I've read this article (before posting the question;) and I do agree with you on keeping view logic in dedicated view code away from templates. However, my assumption was mustache.java implementation of Mustache spec supports passing variables to methods just the way JavaScript implementation does. Here is an excerpt from mustache.js tutorial:

Template:

{{#wrapped}}
  {{name}} is awesome.
{{/wrapped}}

Hash:

{
  "name": "Willy",
  "wrapped": function() {
    return function(text) {
      return "<b>" + render(text) + "</b>"
    }
  }
}

Output:

<b>Willy is awesome.</b>
How hard would it be to implement the same support in mustache.java? Can I help with the implementation? Please, let me know

Thanks,
Vadym

Sam Pullara

unread,
Aug 20, 2013, 1:56:27 PM8/20/13
to mustac...@googlegroups.com
Ah, yes you can do that. Very similar to the way you do it in Javascript. Rather than offering a render method though, you just have to pick between returning a Function or a TemplateFunction, the former processes the rendered text, the latter processes template text. Here is a link to the unit tests:


I've considered adding the direct use of methods like you were trying to do, but didn't decide on a good way to distinguish the two forms. Maybe an annotation?

Sam

Vadym

unread,
Aug 21, 2013, 4:43:21 PM8/21/13
to mustac...@googlegroups.com
I guess writing Function and TemplateFunction definitions is doable (although, quite cumbersome) for new functionality. However, in case of having to use existing set of libraries, it will be a lot of overhead to wrap each and every method in Function/TemplateFunction to make those methods available. Method annotations seem to be quite elegant way to differentiate between the two forms. 

Do you plan on adding the direct use of methods anytime soon? If not, what classes/hooks should I look at to add that support myself?

-V

Sam

unread,
Aug 22, 2013, 2:17:13 AM8/22/13
to mustac...@googlegroups.com
I'll look into it. However, I would say that I think that the only valid use for functions like that are i18n and caching :) Otherwise you're basically using mustache to call functions which means you're encoding business logic in the template. But since I like to enable every use case, I think I can figure out a way to do this...

Sam

Vadym Ustymenko

unread,
Aug 22, 2013, 10:52:27 AM8/22/13
to mustac...@googlegroups.com
Please, post an update when you have any progress. We are trying to move to a strict separation between view & model. However, due to sheer number of the API, the transition might be delayed if there is no backup - ability to call existing APIs

Thanks,
Vadym

Sam

unread,
Sep 14, 2013, 5:31:48 PM9/14/13
to mustac...@googlegroups.com
Hi Vadym,

Not sure if this is exactly what you want, but here is an example where methods are being used for functions in mustache.java:


Sam
Reply all
Reply to author
Forward
0 new messages