Include of a .vtl or .html file

Last Updated: Jul 15, 2020
documentation for the dotCMS Content Management System

You can include any text file on your dotCMS instance (even a file on a different Site) within any content displayed by dotCMS.

Commands

There are two different ways to include files in content, depending on whether or not you want the text in the included file to be interpreted by the Velocity parser.

dotParse

The built-in #dotParse() command can be used to include the entire contents of a Velocity or HTML file.

  • The entire contents of the file will be passed through the Velocity parser so that all Velocity code in the file will be executed.
  • The output of the Velocity code in the file will then be parsed with the HTML parser.

For information on how to prevent HTML parsing of the file contents, or to prevent portions of the file from being parsed by the Velocity parser, please see Preventing Parsing, below.

dotInclude

The built-in #dotInclude() command can be used to include any file in dotCMS.

  • The contents of the file will not be passed through the Velocity parser.
    • Thus any characters in the file which would normally be interpreted by Velocity can remain unescaped in the file.
  • The contents of the file will still be parsed by the HTML parser.
    • For information on how to prevent HTML parsing of the file contents, please see Preventing Parsing, below.
  • By default, only files with certain extensions will be accepted.
    • The following extensions are accepted by default:
      • .css
      • .htm
      • .html
      • .js
      • .json
      • .txt
    • You may change the extensions accepted by adding the VELOCITY_INCLUDE_ALLOWED_EXTENSIONS property to the dotmarketing-config.properties file.
      VELOCITY_INCLUDE_ALLOWED_EXTENSIONS=css,htm,html,js,json,txt
      

Calling Conventions

  • Both commands accept a path to a file in dotCMS.
    • The included file must be located within the same dotCMS instance; both commands will fail if the included file is located outside of dotCMS.
    • To include a file outside the same dotCMS instance, please see Including Text from an External Link, below.
  • Both commands can be called in any Velocity context.
  • For the #dotParse() command only, the extension of the file parsed must be .vtl, .vm or .html.

Syntax

Both #dotParse and #dotInclude may be called in the following contexts.

Note: Although all the examples below use the #dotParse command, all contexts apply equally to the #dotInclude command.

ContextExample
Relative Path (on the same dotCMS Site)#dotParse("/application/vtl/my-file.vtl")
On a specific Site (on the same dotCMS instance)#dotParse("//myhost.com/velocity/mycode.vtl")
By Identifier (please see Note below)#dotParse("/dA/$identifier/{fileName}")
Note: Including Files by Identifier

Including a file in this way allows you to include files from other types of contents such as dotAssets. It also ensures that the file will still be found and included properly even if the file is moved or renamed at a later date, so this method may make sense if you know the file name and location may be changed in the future.

Both of these commands will automatically include an edit link to the parsed file when you are in Edit mode. The edit link can be disabled by adding #set($dontShowIcon=true) before you call either command:

#set($dontShowIcon=true)
#dotParse("/application/vtl/my-file.vtl")

Preventing Parsing

#dotParse automatically parses the entire included file using both the Velocity and HTML parsers, and #dotInclude automatically parses the included file using the HTML parser. You must take additional steps if you wish to prevent parsing of any file by the HTML parser, or to prevent #dotParse from parsing a portion of the file with the Velocity parser.

Prevent HTML Parsing

If you display the contents of a file on a Page, the file will automatically be interpreted by your browser's HTML parser. Therefore, if you wish to display the contents of the file without HTML parsing, you must escape the special HTML characters in the contents of the file.

Individual HTML characters may be escaped using either HTML & notation or the dotCMS EscapeTool Velocity viewtool.

You may escape all of the HTML characters in imported text at one time using the $esc.html method of the EscapeTool, but be aware that HTML escaped in this way may not contain line breaks and other formatting necessary to display the escaped HTML in an easily readable format on the page.

Prevent Velocity Parsing

If you wish to prevent a portion of the included text from being parsed by Velocity, you can do one of the following:

1. Break out the portion of the file you don't want parsed into a separate file.

You can then include the parseable and unparseable files separately (using #dotParse and #dotInclude, respectively).

2. Add Velocity “do not parse” notation within the included file.

Within the included file, enclose the unparseable portion of the file in the Velocity notation which instructs the Velocity parser to ignore that block of text.

#[[
    ...do not parse this text...
]]#

Note that when you use either of these methods, all included text will still be parsed by the HTML parser (so any “<", ">“, or “&” chars will be interpreted). Please see Prevent HTML Parsing above for more information.

Including Text from an External Link

Both #dotParse and #dotInclude are designed to only allow you to include the contents of files on the same dotCMS instance. However if you wish to include the contents of a file or page from another site, you may use the ImportTool Velocity Viewtool provided with dotCMS. For example, the following command imports the entire contents of the Apache Velocity 1.7 user's guide:

$import.read("http://velocity.apache.org/engine/1.7/user-guide.html")

For more information on the $import.read() method, please see the ImportTool documentation.

On this page

×

We Dig Feedback

Selected excerpt:

×