Jari Worsley wrote:
> 
> "Geir Magnusson Jr." wrote:
> >
> > Jari Worsley wrote:
> > >
> >
> > The real answer assumes a little about how things like Velocity work.
> > See below.
> >
> > > Or am i missing something.
> >
> > The question is how do you access it?  And for this game, you can't use
> > scriptlets in JSP :)
> >
> 
> Ok. I haven't looked at Velocity at all.

You should if you build web apps, or do code generation or things like
that.  We found out yesterday that a commercial UML tool is using
Velocity for it's code generation.  Our baby's growing up - we are so
proud :)

> 
> > In velocity, we never allow 'new' from a template to prevent trouble. It
> > also keeps the designer from having to know about the Java API - tools
> > are provided.  Some of those tools could be JDK classes, but people tend
> > to make them easier to use by wrapping them.
> >
> > The controller servlet places all allowed tools in the 'context' that
> > the template can access, something like
> >
> > context.put("mathtool", new MathTool() );
> >
> > and then in the template, you can use it.
> >
> > $mathtool.mul( $a, $b )
> >
> > and if the MathTool was generously written, then mul() would handle
> > different types of input for $a and $b, and deal with them if possible.
> >
> > This moves the onus of understanding the Java API's back onto the
> > programmer, and not the designer.
> 
> This seems to me that you're advocating logic being part of the
> template? Doesn't this "break" the whole MVC idea where you try and do
> all processing before hitting the page, then just use some template
> mechanism (JSP etc) to display data in the page?

Yes and no. To a real purist, the answer is indeed yes for many of the
possible tools - there are some wacky ones (IMHO) right now in the seed
set that do things like process control.

However, there are real needs for simple logic, such as row coloring and
layout - that's why Velocity is contrained to simple integer math in the
template language itself.  For example, you have a large list of items,
say stock tickers, and you want to lay them out in a table of 7 columns,
one per cell.  In velocity, modulo some edge-effect details :

#set($count = 1)

<table>
  <tr>
   #foreach( $stock in $stocklist )
      <td>$stock</td>

      #if ( ($count % 7) == 0 )
        </tr><tr>
      #end

      #set($count = $count + 1)
    #end
  </tr>
</table>

The point here is to illustrate how simple math and logic has a place in
the designers 'toolkit'.

I don't know how to avoid having logic - I don't think a tool that spits
out a complete table is appropos, as then the generation of markup moved
out of View into M or C (the land of the Java programmer anyway).

> 
> To me
> $sometool.apicall($arg1, $arg2) is just as much a programming call as
> a JSP style <%= request.getAttribute("foo") %>.

1) In Velocity, you would just do

$request.getAttribute("foo")

2) In JSP, you wouldn't have to resort to scriptlets, right?  You could
easily access via a bean access tag or a custom tag...

And there is nothing wrong with access of data through what looks like
method calls - I don't think thats that problem, becuase the tools that
are made available to the Velocity template designer are controlled by
the servlet - its a deliberate decision.    You can't create arbitrary
objects like

<% Foo foo = new Foo(); %>

The point here is not to engage again in a JSP vs templating discussion
(although I am always happy to do that) - just to try and get the idea
across that you can consider 'context tools' (as we call them - they are
placed in the context...) to be like JSP taglibs - fundamentally a set
of objects deliberately made accessable to the template/page designer. 
The difference is that they require no special API support - any Java
class with public methods can be accessed from velocity - that's why
collecting little tools like this is valuable - they can be used outside
of the domain of the web, and many outside of the domain of templating
tools - they are just classes.
  
> 
> I guess I should look at what you are trying to do in Velocity before
> commenting further really.

Give it a look.  If you have any questions, feel free to contact me
directly, or bring them to the Velocity community via the mail lists. 
We are always happy to help.

geir

-- 
Geir Magnusson Jr.                           [EMAIL PROTECTED]
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Reply via email to