Snippet Macro
Last modified by Andreea Popescu on 2026/02/26 15:43
| Extracts a portion of code delimited by a token. |
| Type | Doc (Velocity Macro) |
| Category | |
| Developed by | |
| Rating | |
| License | GNU Lesser General Public License 2.1 |
Table of contents
Description
Extracts a portion of code delimited by a token. This macro mimicks the behavior of the Confluence Snippet macro.
Usage
#snippet(lang, id, url)Parameters definition
| Name | Optional | Allowed values | Default value | Description |
|---|---|---|---|---|
| lang | no | String | none | Language to be used when wrapping the content in the {code} macro |
| id | no | String | none | The token id. Tokens are of the form // START SNIPPET: / // END SNIPPET: |
| url | no | String | none | The URL from where to get the content to display |
Example
#snippet("java", "boy", "http://svn.codehaus.org/picocontainer/java/1.x/picocontainer/trunk/container/src/test/org/picocontainer/doc/tutorial/simple/Boy.java")Result

Installation
This a Velocity macro. Since it's not bundled by default you'll have to copy/paste the following code to use it:
#macro(snippet $lang $id $url)
#set($codestart = "{CODE:$lang}")
$codestart.toLowerCase()
#set ($text = $xwiki.getURLContent($url))
#set ($delimiter = "// START SNIPPET: $id")
#set ($pos1 = $xwiki.add($text.indexOf($delimiter), $delimiter.length()))
#set ($pos2 = $text.indexOf("// END SNIPPET: $id"))
$text.substring($pos1, $pos2)
#set($codeend = "{CODE}")
$codeend.toLowerCase()
#end