views:

14

answers:

1

I have about 10 standard blocks of dynamically generated HTML that get included on a large percentage of pages on my site (informational sidebars).

I can use #include statements or define them as a macros, both would work.

What's better? I'm new to Velocity and migrating a site from JSP.

Why use one vs the other? Why have #include when a no-arg macro() does the same? Are they handled any differently from a caching/memory perspective?

+2  A: 

If they are static blocks (no VTL code in them), then #include-ing them as separate resources will certainly perform much better. Even if they are dynamic (have VTL code), then you can use #parse instead of #include and still get better performance than a macro. I wouldn't even be surprised to learn that #define VTL blocks are faster than macros, though i haven't tested that. Macros are great and very useful, but people do sometimes misuse or abuse them.

The downside of #parse and #include is that you have to maintain more separate files, but that's the price for superior speed. #define is great for code blocks that don't need args and/or need to be passed around as a reference, but you should only use #define'd blocks directly in the template that defines them or things can get confusing.

Macros are great in that they take params and can be defined globally, and globally is where they perform best and make most sense. I generally try to avoid local macros when i have a reasonable alternative. And when i successfully avoid having any local macros, i turn support for them off to get a small speed boost.

Nathan Bubna
Many thanks for the excellent explanation, it really helps clarify the difference in my mind.
David Parks