Camunda BPM supports template engines which are implemented as script engines compatible withJSR-223. As a result, templates can be used everywhere where scripts can be used.

In community distributions of Camunda BPM, the following template engine is provided out of thebox:

Additionally, the following template engine is supported as enterprise extension:

  • XSLT

    Install a Template Engine

Install a Template Engine for an Embedded Process Engine

A template engine must be installed in the same way as a script engine. This means that the templateengine must be added to the process engine classpath.

When using an embedded process engine, the template engine libraries must be added to theapplication deployment. When using the process engine in a maven war project, the template enginedependencies must be added as dependencies to the maven pom.xml file:

Please import the Camunda BOM to ensure correct versions for every Camunda project.

  1. <dependencies>
  2. <!-- freemarker -->
  3. <dependency>
  4. <groupId>org.camunda.template-engines</groupId>
  5. <artifactId>camunda-template-engines-freemarker</artifactId>
  6. </dependency>
  7. <!-- apache velocity -->
  8. <dependency>
  9. <groupId>org.camunda.template-engines</groupId>
  10. <artifactId>camunda-template-engines-velocity</artifactId>
  11. </dependency>
  12. </dependencies>

Install a Template Engine for a Shared Process Engine

When using a shared process engine, the template engine must be added to the shared process engineclasspath. The procedure for this depends on the application server. In Apache Tomcat, thelibraries have to be added to the shared lib/ folder.

FreeMarker is pre-installed in the Camunda pre-packaged distribution.

Use a Template Engine

If the template engine library is in the classpath, you can use templates everywhere in the BPMNprocess where you can use scripts, for example as a script task or inputOutput mapping.The FreeMarker template engine is part of the Camunda BPM distribution.

Inside the template, all process variables of the BPMN element scope are available. Thetemplate can also be loaded from an external resource as described in the script sourcesection.

The following example shows a FreeMarker template, of which the result is saved in the process variabletext.

  1. <scriptTask id="templateScript" scriptFormat="freemarker" camunda:resultVariable="text">
  2. <script>
  3. Dear ${customer},
  4. thank you for working with Camunda BPM ${version}.
  5. Greetings,
  6. Camunda Developers
  7. </script>
  8. </scriptTask>

In an inputOutput mapping it can be very useful to use an external template to generate thepayload of a camunda:connector.

  1. <bpmn2:serviceTask id="soapTask" name="Send SOAP request">
  2. <bpmn2:extensionElements>
  3. <camunda:connector>
  4. <camunda:connectorId>soap-http-connector</camunda:connectorId>
  5. <camunda:inputOutput>
  6. <camunda:inputParameter name="soapEnvelope">
  7. <camunda:script scriptFormat="freemarker" resource="soapEnvelope.ftl" />
  8. </camunda:inputParameter>
  9. <!-- ... remaining connector config omitted -->
  10. </camunda:inputOutput>
  11. </camunda:connector>
  12. </bpmn2:extensionElements>
  13. </bpmn2:serviceTask>

Use XSLT as Template Engine

Enterprise Feature

Please note that this feature is only included in the enterprise edition of the Camunda BPM platform, it is not available in the community edition.

Check the Camunda enterprise homepage for more information or get your free trial version.

Install the XSLT Template Engine

The XSLT Template Engine can be downloaded from the Enterprise Edition Download page.

Instructions on how to install the template engine can be found inside the downloaded distribution.

Use XSLT Template Engine with an embedded process engine

When using an embedded process engine, the XSLT template engine library must be added to theapplication deployment. When using the process engine in a maven war project, the template enginedependency must be added as dependencies to the maven pom.xml file:

Please import the Camunda BOM to ensure correct versions for every Camunda project.

  1. <dependencies>
  2. <!-- XSLT -->
  3. <dependency>
  4. <groupId>org.camunda.bpm.extension.xslt</groupId>
  5. <artifactId>camunda-bpm-xslt</artifactId>
  6. </dependency>
  7. </dependencies>

Use XSLT Templates

The following is an example of a BPMN ScriptTask used to execute an XSLT Template:

  1. <bpmn2:scriptTask id="ScriptTask_1" name="convert input"
  2. scriptFormat="xslt"
  3. camunda:resource="org/camunda/bpm/example/xsltexample/example.xsl"
  4. camunda:resultVariable="xmlOutput">
  5. <bpmn2:extensionElements>
  6. <camunda:inputOutput>
  7. <camunda:inputParameter name="camunda_source">${customers}</camunda:inputParameter>
  8. </camunda:inputOutput>
  9. </bpmn2:extensionElements>
  10. </bpmn2:scriptTask>

As shown in the example above, the XSL source file can be referenced using the camunda:resourceattribute. It may be loaded from the classpath or the deployment (database) in the same way asdescribed for script tasks.

The result of the transformation can be mapped to a variable using the camunda:resultVariableattribute.

Finally, the input of the transformation must be mapped using the special variable camunda_sourceusing a <camunda:inputParameter … /> mapping.

A full example of the XSLT Template Engine in Camunda BPM can be found in theexamples repository..

原文: https://docs.camunda.org/manual/7.9/user-guide/process-engine/templating/