sps / mustache-spring-view

java spring framework mvc view for Mustache.js templates
68 stars 31 forks source link

Added better support for partials #3

Open agentgt opened 12 years ago

agentgt commented 12 years ago

I added how it works in the README.MD

I'm using a different version in production but this should work. The partial alias support I added is particularly useful for layout.

sps commented 12 years ago

Hi Adam,

Thanks for this pull request - I haven't had a chance to review it yet, but I should in the next few days.

Thanks! -sean

agentgt commented 12 years ago

Its not the cleanest solution and I didn't have a chance to add more unit tests.

agentgt commented 12 years ago

I also noticed that I forgot to use spaces instead of tabs. I used tabs because SpringSource used to use tabs (I'm not sure if they still do).

chrylis commented 10 years ago

Will there be any progress on the partials? I'm looking at using a prepackaged template library that's heavily built around a (very nice and clean) partial system, and I'm up for working on the view code as long as it doesn't get too esoteric.

sps commented 10 years ago

Are you referring to partials support at all or what this patch provides? Partials are already supported.

agentgt commented 10 years ago

@chrylis and @sps this patch is to enhance partial support and to add rudimentary layout support. I have mostly switched to Handlebars.java which has its own way of doing layout but we still use Mustache here and there.

There are two things this patch does:

  1. Switch out a partial by adding URI query parameters to the view name. This is useful if you have a template and you want to say dynamically change the sidebar inside the controller by pointing to a different partial. That is a level of indirection is provided for the template partials aka partial aliases.
  2. Rudimentary Template inheritance. Instead of putting {{> header}} and {{> footer}} all over the place the template will be wrapped with a parent layout template. The code does this by walking up the directory structure looking for templates name layout (configurable).

Here is an example of the template inheritance:

Say we have the views:

/WEB-INF/views/layout.mustache might look something like:

... header stuff...
{{> inner}}
... footer stuff...
{{> legal }}

a.mustache will be put into /WEB-INF/views/layout.mustache {{> inner}} if your controller returns "a". That is it will be wrapped.

b.mustache will use /WEB-INF/views/sub1/layout.mustache where as c.mustache will use /WEB-INF/views/layout.mustache (up the tree).

Of course if you don't want c.mustache to be wrapped then you can use the partial alias support mentioned earlier by doing:

return "sub2/c?layout="

Or maybe you just want to replace legal partial from /WEB-INF/views/layout.mustache

return "sub2/c?legal=sub2/someother-legal"