Re: discover macro name
"Jonathan Revusky" <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Here is code for a method that you could expose to your FTL to get
the current macro name.
import freemarker.core.Environment;
import freemarker.core.Macro;
public class GetMacroName extends TemplateMethodModel {
public TemplateModel exec(List args) throws TemplateModelException {
Environment env = Environment.getCurrentEnvironment();
Macro.Context mc = env.getCurrentMacroContext();
if (mc == null) {
throw...
}
String macroName = mc.getMacro().getName();
return new SimpleScalar(macroName);
}
}
There is one problem however. Some of the API in there is package
private. You'd either have to put the GetMacroName() in the
freemarker.core package or make a couple of things in there public and
rebuild.
JR
On Sun, Mar 23, 2008 at 10:59 AM, Ioannis Cherouvim <[email protected]> wrote:
> Hi
>
> OK here are the details.
>
> I use freemarker to render html in my Java web applications. Usually I
> will have macros to render some html snippets such as search form,
> search result, login form etc because I reuse them in many projects and
> they feature the exact same html. The graphic designers (the people who
> initially do the static html layout) want to be able to see where these
> snippets start and end, in the final rendered product (when running on
> "debug" mode), in order to maintain/debug html rendering issues.
>
> I've got around 30 of those macros.
>
> So, instead of doing the following:
> <#macro renderSearchForm>
> <!-- renderSearchForm starts -->
> <p>bla bla bla...</p>
> <!-- renderSearchForm ends -->
> </#macro>
>
> <#macro renderLoginForm>
> <!-- renderLoginForm starts -->
> <p>bla bla bla...</p>
> <!-- renderLoginForm ends -->
> </#macro>
>
>
> I'd like to be able to do something like:
> <#macro renderSearchForm>
> <!-- ${currentMacroname} starts -->
> <p>bla bla bla...</p>
> <!-- ${currentMacroname} ends -->
> </#macro>
>
> So I don't have to maintain too much stuff, especially in case of macro
> name refactorings.
>
> This will eventually lead me to a more generic macro such as:
> <#macro renderSearchForm>
> <@commentWrap currentMacroname>
> <p>bla bla bla...</p>
> </@commentWrap>
> </#macro>
>
> Which will be a macro wrapping the nested html (using <#nested /> ) with
> the appropriate html comments.
>
> thanks,
> Ioannis
>
>
>
> Daniel Dekany wrote:
> > Sunday, March 23, 2008, 7:30:32 AM, Ioannis Cherouvim wrote:
> >
> >> Hello
> >>
> >> I'd like to know if it is possible to dynamically discover the currently
> >> executing macro name. For example:
> >> <#macro foo>
> >> ...
> >> </#macro>
> >>
> >> What do I have to do in order for the macro above to output the word
> >> "foo"? That is the name of the macro.
> >
> > I don't know, but, out of curiosity, what is that needed for?
> >
> >> thanks,
> >> Ioannis
> >
>
>
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/