0

I'm wondering can Velocity make what I want :)

For example I have

<html>
   <meta> 
      <title>My title</title>
   </meta>
   <body>
   <div id="content">
   <!-- here is my dynamic content -->
   </div>       
   <div id="right">static content</div> 
  </body></html>

Now. I have 4 actions in my Spring based application

create, update, login, home

Every action have it own template. For create is a big form, for update small form, for login, login form, for home lates news.

There are very much diffrent each from other. Now I want to dynamicly swap content in my Can I create such template-container (i mean the header part and right div) with dynamic part ?

I don't want to get the actions response to variable and pass it to template. I want to have a simple template for example create.jsp or create.vm or create.html and I want that my app automaticly will take the template of the action and render it in my static template-container.

I hope it is clear

Fixus
  • 4,631
  • 10
  • 38
  • 67

2 Answers2

1

Why not use Tiles instead? It's designed for this scenario.

TrueDub
  • 5,000
  • 1
  • 27
  • 33
  • (+1) That is what tiles is for – Ralph Mar 17 '12 at 10:55
  • Not rly. I don`t like tiles because every action need to be described in xml. I mean if I have 20 actions with 20 diffrent templates I need to go to my tiles.xml and write that update extends defaultTemplate, and I need to put-attribute for id="content". It isn`t effective. Or maybe I`m wrong ? – Fixus Mar 17 '12 at 11:01
  • Tiles can extend other tiles, not just defaultTemplate, so you can build things up. Honestly, I think it'd be simpler to do using Tiles (or Facelets if you're using JSF, or even Sitemesh) rather than Velocity, but it's up to you. – TrueDub Mar 17 '12 at 12:53
  • @TrueDub I know that I can extend. But I need to do it manualy for every view. Its very inefective. Or am I wrong ? – Fixus Mar 17 '12 at 18:16
  • Take a look to Sitemesh. It needs less configuration so you can setup it and forget if you have 2 or 20 actions. You dont need to add new configuration for new actions if the template is the same. – jddsantaella Mar 17 '12 at 20:50
0

I've used Tiles 2 with JSP in several projects and I like it. However, with Velocity I prefer macros for simple templating system.

main.vm:

#macro(main)
<html>
    <meta>
        <title>My title</title>
    </meta>
    <body>
        <div id="content">
            $bodyContent
        </div>
    </body>
</html>
#end

hello.vm:

#@main
Hello, World!
#end

Spring configuration:

<bean id="velocityConfigurer" 
    ...
    <property name="velocimacro.library" value="main.vm" />
</bean>

Maybe this is not so pretty, but it has the advantage that every view is the one who decides which layout applies, more like JSF does.

sinuhepop
  • 20,010
  • 17
  • 72
  • 107