mustache-spring-view icon indicating copy to clipboard operation
mustache-spring-view copied to clipboard

Added better support for partials

Open agentgt opened this issue 13 years ago • 6 comments

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.

agentgt avatar Mar 27 '12 17:03 agentgt

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

sps avatar Mar 28 '12 03:03 sps

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

agentgt avatar Mar 28 '12 16:03 agentgt

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

agentgt avatar Mar 28 '12 16:03 agentgt

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.

chrylis avatar Jan 07 '14 06:01 chrylis

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

sps avatar Jan 09 '14 04:01 sps

@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
  • /WEB-INF/views/legal.mustache
  • /WEB-INF/views/a.mustache
  • /WEB-INF/views/sub1/b.mustache
  • /WEB-INF/views/sub1/layout.mustache
  • /WEB-INF/views/sub2/c.mustache

/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"

agentgt avatar Mar 29 '14 15:03 agentgt