Cameron Braid
2003-11-12 13:18:07 UTC
A little while ago someone mentioned that optional macro parameters is
on the todo list.
I was wondering when this may come about, as I think it will be a great
addition to allow for much more complex macro librares to be built.
Especially when trying to use macros in place of html elements, where
lots of things are optional.
The proposed syntax was something like
<#macro theMacroName paramName? > ... </#macro>
the ? after the parameter name making it optional. Am I correct in
saying that since it is optional, if a undefined variable is passed to
the macro call, it will be allowed :
<@theMacroName undefinedVariable/>
currently this works if the macro is defined with a default value for
the parameter as in
<#macro theMacroName paramName=""> ... </#macro>
I would also like to know if this optional syntax can be applied to the
<#assign>, <#local>, <#global> or anywhere that builtins can be used.. ie
currently <$assign thingy = undefinedValue/> gives an invalid reference
exception. I would like to be able to do
<$assign thingy = model.expression? /> which should succeed with thingy
being undefined if model.expression is undefined
This allows for coding things like
<#assign colour = model.defaultColour?/>
...lotsa code here...
<#if colour?exists>
....
</#if>
...lotsa code here...
<#if colour?exists>
....
</#if>
Whereas without it you would have to write either :
<#if model.defaultColour?exists>
<#assign colour = model.defaultColour/>
</#if>
which isn't all that bad since freemarker doesn't scope variables like
java :)
I guess that another way to do the 'allow null reference' is
paramName?default(undefined)
though this introduces a new keyword, and is quite verbose.
<#macro name param?default(undefined) > ... </#macro>
Keen to know your thoughts,
Cameron
on the todo list.
I was wondering when this may come about, as I think it will be a great
addition to allow for much more complex macro librares to be built.
Especially when trying to use macros in place of html elements, where
lots of things are optional.
The proposed syntax was something like
<#macro theMacroName paramName? > ... </#macro>
the ? after the parameter name making it optional. Am I correct in
saying that since it is optional, if a undefined variable is passed to
the macro call, it will be allowed :
<@theMacroName undefinedVariable/>
currently this works if the macro is defined with a default value for
the parameter as in
<#macro theMacroName paramName=""> ... </#macro>
I would also like to know if this optional syntax can be applied to the
<#assign>, <#local>, <#global> or anywhere that builtins can be used.. ie
currently <$assign thingy = undefinedValue/> gives an invalid reference
exception. I would like to be able to do
<$assign thingy = model.expression? /> which should succeed with thingy
being undefined if model.expression is undefined
This allows for coding things like
<#assign colour = model.defaultColour?/>
...lotsa code here...
<#if colour?exists>
....
</#if>
...lotsa code here...
<#if colour?exists>
....
</#if>
Whereas without it you would have to write either :
<#if model.defaultColour?exists>
<#assign colour = model.defaultColour/>
</#if>
which isn't all that bad since freemarker doesn't scope variables like
java :)
I guess that another way to do the 'allow null reference' is
paramName?default(undefined)
though this introduces a new keyword, and is quite verbose.
<#macro name param?default(undefined) > ... </#macro>
Keen to know your thoughts,
Cameron
--
Any damn fool can write code that a computer can understand...
The trick is to write code that humans can understand.
[Martin Fowler http://www.martinfowler.com/distributedComputing/refactoring.pdf]
Any damn fool can write code that a computer can understand...
The trick is to write code that humans can understand.
[Martin Fowler http://www.martinfowler.com/distributedComputing/refactoring.pdf]