Synopsis
Where:
DescriptionWith this you can create a new variable, or replace an existing variable. Note that only top-level variables can be created/replaced (i.e. you can't create/replace For more information about variables, read this: Template Author's Guide/Miscellaneous/Defining variables in the template Note: A frequent mistake is trying to use Example: variable Template <#assign seq = ["foo", "bar", "baz"]> Example: Increments the numerical value stored in variable As a convenience feature, you can do more assignments with one Template <#assign seq = ["foo", "bar", "baz"] x++ > If you know what namespaces are: Template <#import "/mylib.ftl" as my> <#assign bgColor="red" in my> An extreme usage of Template <#macro myMacro>foo</#macro> <#assign x> <#list 1..3 as n> ${n} <@myMacro /> </#list> </#assign> Number of words: ${x?word_list?size} ${x} will print: Output Number of words: 6 1 foo 2 foo 3 foo Please note that you should not to use this to insert variables into strings: Template <#assign x>Hello ${user}!</#assign> <#-- BAD PRACTICE! --> You should simply write: Template <#assign x="Hello ${user}!"> |