Cameleer

StringTemplate component

The string-template component renders a StringTemplate 4 template against the message and sets the rendered text as the new body.

Scheme: string-template: · Crate: cameleer-core · Cargo feature: templates · Consumer: no · Producer: yes · Apache Camel: String Template

When to use it

  • Render text where the template must stay logic-free: only attribute references, conditions on presence and list application.
  • Produce lists with separators (; separator=", ") without loop code.
  • Reuse StringTemplate files from a Java Camel application.

URI format

text
string-template:templateResource[?options]

templateResource is a file path relative to the working directory, file:path, classpath:path (read as a relative path) or ref:beanName.

Example

Summarize the tools an agent may call. Attributes use <...>, an anonymous sub-template is applied to each list item, and if tests presence.

text
Agent <body.name> may call: <body.tools; separator=", ">.
<body.tools:{t | <i>. <t>}; separator="\n">
<if(headers.readonly)>Read-only mode: do not call write tools.<endif>
yaml example: string-template-tool-list
- route:
    id: string-template-tools
    from:
      uri: direct:string-template-tools
      steps:
        - unmarshal:
            json: {}
        - setHeader:
            name: readonly
            constant: "yes"
        - to: string-template:templates/tools.st
json
{"name":"triage","tools":["search_docs","read_ticket","close_ticket"]}
text
Agent triage may call: search_docs, read_ticket, close_ticket.
1. search_docs
2. read_ticket
3. close_ticket
Read-only mode: do not call write tools.

Other delimiters

delimiterStart and delimiterStop change < and >, which helps when the output is HTML or XML:

text
<li>$body$</li>
yaml example: string-template-delimiters
- route:
    id: string-template-dollar
    from:
      uri: direct:string-template-dollar
      steps:
        - to: string-template:templates/item.st?delimiterStart=$&delimiterStop=$
text
Cameleer
text
<li>Cameleer</li>

Rust DSL

rust
b.from("direct:string-template-rust")
    .to("string-template:templates/item.st?delimiterStart=$&delimiterStop=$");

Options

optiondefaultdescription
delimiterStart<Character that opens an expression.
delimiterStop>Character that closes an expression.
allowTemplateFromHeaderfalseUse the CamelStringTemplateTemplate / CamelStringTemplateResourceUri headers when present.
contentCachefalseRead the template once; otherwise it is re-read for every message.
allowContextMapAllfalseAdd camelContext (with name) to the data model.
encodingUTF-8Accepted; templates are always read as UTF-8.

The data model: body, headers, variables, exchange (exchange.properties...), request / in, and camelContext with allowContextMapAll=true.

Supported StringTemplate

Attribute references and properties (<a.b>, <a.(expr)>), string and list literals, multi-valued attributes with separator and null options, anonymous sub-templates (<names:{n | <n>}>, implicit it, <i> / <i0>, chained and zipped application), if/elseif/else/endif with !, &&, ||, the functions first last rest trunc strip length reverse trim strlen, special characters (<\n>, <\t>, <\uXXXX>), comments <! !> and escapes. Named template references (<x:t()>) need template groups and are not supported.

Message headers

headerdirectiondescription
CamelStringTemplateTemplateinTemplate text used instead of the resource (needs allowTemplateFromHeader=true).
CamelStringTemplateResourceUriinTemplate resource used instead of the endpoint's one (needs allowTemplateFromHeader=true).

Run it with Docker

sh
docker run --rm -v "$PWD:/routes:ro" pmsudhi/cameleer run /routes/tools.camel.yaml

Common questions

How do I join a list with commas?

<body.tools; separator=", ">. Missing items can be replaced with null="n/a".

How do I write HTML or XML with StringTemplate?

Switch the delimiters with delimiterStart=$&delimiterStop=$, so < and > are plain text.

Why does <if(x)> not compare values?

StringTemplate is logic-free: if only tests that an attribute is present, not false and not an empty list. Compute the condition before rendering, for example in a header.

Found a problem?

If something on this page doesn't match what Cameleer does, or you hit a bug while building with it, please help fix it, whether you are a person or a coding agent: open an issue that describes what you saw, then fork the repository, fix it, add or update a test, run cargo test --workspace, and open a pull request. CONTRIBUTING.md explains the ground rules.

Edit this page on GitHub