{"id":1712,"date":"2010-07-21T14:27:35","date_gmt":"2010-07-21T19:27:35","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=1712"},"modified":"2010-08-25T11:05:22","modified_gmt":"2010-08-25T16:05:22","slug":"velocity-template","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=1712","title":{"rendered":"Velocity Template"},"content":{"rendered":"<span id=\"Configuration_Properties\"><h2>Configuration Properties<\/h2><\/span>\n<span id=\"General_Properties\"><h3>General Properties<\/h3><\/span>\n<pre lang=\"bash\">\r\ninput.encoding\r\noutput.encoding\r\nparser.pool.size\r\nruntime.interpolate.literals\r\nruntime.introspector.uberspect\r\n<\/pre>\n<span id=\"Logging_Properties\"><h3>Logging Properties<\/h3><\/span>\n<pre lang=\"bash\">\r\n# Log file location. Default to velocity.log in the execution path\r\nruntime.log\r\n# Which log system to use. Defaults to Avalon LogKit\r\nruntime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem\r\n# Output stack trace\r\nruntime.log.xxx.stacktrace=true | false\r\n# Long invalid references in template. Default to true. Should be turned off in prod.\r\nruntime.log.invalid.references= true | false\r\n<\/pre>\n<span id=\"Resource_Loader_Properties\"><h3>Resource Loader Properties<\/h3><\/span>\n<pre lang=\"bash\">\r\nresource.loader=file, class, jar\r\n\r\n# File Resource Loader\r\nfile.resource.loader.class= org.apache.velocity.runtime.resource.loader.FileResourceLoader\r\nfile.resource.loader.path=.\/src\/templates\/ch2\r\nfile.resource.loader.cache=true\r\nfile.resource.loader.modificationCheckInterval=2\r\n\r\n# Classpath Resource Loader\r\nclass.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader\r\n\r\n# JAR Resource Loader\r\njar.resource.loader.class = org.apache.velocity.runtime.resource.loader.JarResourceLoader\r\njar.resource.loader.path = jar:file:\/tmp\/ResourceLoader2.jar\r\n\r\n# DataSourceResourceLoader\r\n<\/pre>\n<span id=\"Directive_Properties\"><h3>Directive Properties<\/h3><\/span>\n<pre lang=\"bash\">\r\ndirective.include.out-put.errormsg.start\r\ndirective.include.output.errormsg.end\r\n\r\n## Maximum depth of parse (default to 10)\r\ndirective.parse.maxdepth\r\n\r\n## default to velocityCount\r\ndirective.foreach.counter.name\r\n## default to 1\r\ndirective.foreach.counter.initial.value\r\n<\/pre>\n<span id=\"Macro_Properties\"><h3>Macro Properties<\/h3><\/span>\n<pre lang=\"bash\">\r\nvelocimacro.library=macroLibrary.vm\r\nvelocimacro.permissions.allow.inline=true | false\r\nvelocimacro.permissions.allow.inline .to.replace.global=true | false\r\n## If true, changes to variables inside macro are visible only inside macro\r\n## Default to false\r\nvelocimacro.context.localscope=true | false\r\n## Default to false\r\nvelocimacro.library.autoreload=true | false\r\n## Determines whether an inline macro is visible outside the defining template\r\nvelocimacro.permissions.allow.inline.local.scope=true | false\r\n<\/pre>\n<span id=\"Velocity_Template_Language_VTL\"><h2>Velocity Template Language (VTL)<\/h2><\/span>\n<p>* Comments<\/p>\n<pre lang=\"bash\">\r\n## Single line comment\r\n#* Multiple\r\nline comments *#\r\n<\/pre>\n<p>* Escape char<\/p>\n<pre lang=\"bash\">\r\n## Escape character is \\\r\n\\$myName = $myName\r\n<\/pre>\n<p>* Variables<\/p>\n<pre lang=\"bash\">\r\n## Variable is prefixed with a dollar sign\r\n$myName\r\n\r\n## Formal reference of variables\r\n${myName}\r\n\r\n## Quiet reference which will not be displayed if not set\r\n$!undefinedVar\r\n\r\n## Access object method\r\n$myDate.getDate()\r\n$myDate.getMonth()\r\n\r\n## Access Java bean variables\r\n$myDate.Date\r\n$myDate.Month\r\n\r\n## Access Map variables\r\n$myMap.firstName\r\n\r\n## Create variables in a template\r\n#set($msg = \"Hello world!\")\r\nI have a message: $msg\r\n\r\n#set($str = \"A string\")\r\n#set($num = 123)\r\n#set($bool = true)\r\n\r\n<\/pre>\n<p>* Performing arithmetic<br \/>\n&#8211; Supports + &#8211; * \/ %<br \/>\n* Logical operators<br \/>\nAND: &#038;&#038;<br \/>\nOR: ||<br \/>\nNOT: !<\/p>\n<span id=\"Directives\"><h3>Directives<\/h3><\/span>\n<p>* Include<\/p>\n<pre lang=\"bash\">\r\n## include template as is, no parsing\r\n#include(\"footer.vm\")\r\n\r\n## included template needs to be parsed\r\n#parse(\"footer.vm\")\r\n<\/pre>\n<p>* If\/else<br \/>\nComparison operators: ==, !=, <, >, <=, >= <\/p>\n<pre lang=\"bash\">\r\n#set($myBool = true)\r\n#if($myBool)\r\nIt is true\r\n#else\r\nIt is false\r\n#end\r\n\r\n#if ($myName == \"John\")\r\n    Hello John!\r\n#elseif ($myName == \"Jane\")\r\n    Hello Jane!\r\n#else\r\n    Hello world!\r\n#end\r\n<\/pre>\n<p>* Access collections with #foreach<\/p>\n<pre lang=\"bash\">\r\n## Define Velocity array\r\n#set($myArray = [\"one\", \"two\", \"three\"])\r\n#foreach($item in $myArray)\r\n    List Item: $item\r\n#end\r\n\r\n## Use range\r\n#foreach($num in [1..9])\r\n    List number: $num\r\n#end\r\n<\/pre>\n<p>* Macros<\/p>\n<pre lang=\"bash\">\r\n#macro(myMacro $param1 $param2)\r\n     \\$param1: $param1\r\n     \\$param2: $param2\r\n    #foreach($num in [1..9])\r\n        List number: $num\r\n    #end\r\n#end\r\nCall macro\r\n#myMacro(\"John\", \"Jane\")\r\n<\/pre>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"http:\/\/velocity.apache.org\/engine\/releases\/velocity-1.5\/user-guide.html\">http:\/\/velocity.apache.org\/engine\/releases\/velocity-1.5\/user-guide.html<\/a><br \/>\n* <a href=\"http:\/\/velocity.apache.org\/engine\/releases\/velocity-1.5\/vtl-reference-guide.html\">VTL Reference Guide<\/a><br \/>\n* Pro Jakarta Velocity: From Professional to Expert by Rob Harrop Apress \u00a9 2004<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Configuration Properties General Properties input.encoding output.encoding parser.pool.size runtime.interpolate.literals runtime.introspector.uberspect Logging Properties # Log file location. Default to velocity.log in the execution path runtime.log # Which log system to use. Defaults to Avalon LogKit runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem # Output stack trace runtime.log.xxx.stacktrace=true | &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=1712\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14],"tags":[],"class_list":["post-1712","post","type-post","status-publish","format-standard","hentry","category-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-rC","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1712"}],"version-history":[{"count":4,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1712\/revisions"}],"predecessor-version":[{"id":1714,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1712\/revisions\/1714"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}