Lots of out.println(..) in java code are hard to write,
maintain and read. A hash section is a simple way to output HTML/text from within
java code without having to use out.println() statements.
Syntax
Hash sections can appear anywhere in a java code section.
#..text/html here (can be any number of lines)..#
Tag-Escape
This is only a concern inside a code section (which is the only place a hash section can exist). Any
other # is always just a literal # (so you can use these without worry in CSS sections etc., as long as
those sections are not within a code section).
To prevent the tag from being recognized, this tag can be escaped by prefixing the start
tag with a backslash: \#
The parser is aware of valid java statements and java code. Therefore any # appearing inside java strings is ignored.
For example:
[[
String foo = "abc#123";
]]
The # in abc#123 does not start a hash section. (so java
code works with no modifications inside a code section).
The following is a valid hash section (and note, not valid java
code so there is no overlap between a hash section and valid java code).
[[
String foo = "abc#123";
#
Hello World !
#
]]
Since literal # characters are very
common in HTML, the parser makes a special exception to the tag-escape
rule. You don't have to know this rule, just use normal escapes when needed,
but if you want to save some typing, see more details here
Suppose we have:
#
... bgcolor="#ffffff"
#
One would expect that the # after
bgcolor will erroneously end the hash section. (I've
used bgcolor as a specific attribute but this discussion is valid
for all foo="#bar" attributes). So we normally say:
1. # ... bgcolor="\#ffffff" #
But since some_attribute="#value" is such a common case,
the parser also allows one to say:
All of the above (cases 1, 2, and 3) will work the same and result in
bgcolor="#ffffff".
The hash shown in red is not tag-escaped yet
magically does not (prematurely) end the hash section.
Also, note that in the above cases, the value of the bgcolor (or whatever)
attribute is quoted by single or double quotes. If you have the habit of
not quoting attribute values, then you always have to escape the
# contained in a hash section.
We cannot say this:
#
... bgcolor=#ffffff //Note: (no single or double quotes around #ffffff)
#
We have to say:
#
... bgcolor=\#ffffff
#
If all this gets hard to keep in mind, then just remember to escape any #
within a hash section and you will always be OK.
Example
A simple hash section.
[[
// Note the use of an escaped \#
// to get a literal # in the output.
boolean great = true;
if (great) {
# You are \#1 #
}
]]
You are #1
Example
A code section that prints HTML (using a hash section) from
within a Java for-loop.
[import
import java.util.*;
]
[[
List list = Arrays.asList(new String[] {"123", "456"});
out.print("