开发者

Velocity: Is it better to use a Velocity Macro or #include(...) statement for a DIV block repeated many times on different pages?

开发者 https://www.devze.com 2023-01-22 06:47 出处:网络
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 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开发者_JAVA百科 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消