Template processor
Core v2.2.0
The processor provides powerful variable interpolation and expression
evaluation within text content. It processes
${...
} expressions using the configured
scripting language (JavaScript, Groovy, or BeanShell), enabling dynamic content generation,
string building, and simple computations.
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="username" value="john_doe"/>
<def var="apiKey" value="abc123"/>
<!-- Automatic template interpolation in attributes -->
<http url="https://api.example.com/user/${username}?key=${apiKey}"/>
<!-- Explicit template processor (same result) -->
<def var="url">
<template>https://api.example.com/user/${username}?key=${apiKey}</template>
</def>
<http url="${url}"/>
</config>
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="firstName" value="John"/>
<def var="lastName" value="Doe"/>
<def var="age" value="30"/>
<def var="greeting">
<template>
Hello, ${firstName} ${lastName}!
You are ${age} years old.
Next year you will be ${parseInt(age) + 1}.
</template>
</def>
</config>
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<loop item="product" index="i">
<xpath expression="//product">...</xpath>
<def var="name"><xpath expression=".//name/text()">${product}</xpath></def>
<def var="price"><xpath expression=".//price/text()">${product}</xpath></def>
<def var="html">
<template>
<![CDATA[
<div class="product" id="product-${i}">
<h3>${name}</h3>
<p class="price">$${price}</p>
<a href="/product/${i}">View Details</a>
</div>
]]>
</template>
</def>
<file path="products.html" action="append">${html}</file>
</loop>
</config>
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="userId" value="123"/>
<def var="userName" value="John Doe"/>
<def var="userEmail" value="john@example.com"/>
<def var="jsonOutput">
<template>
<![CDATA[
{
"id": ${userId},
"name": "${userName}",
"email": "${userEmail}",
"timestamp": "${_date}",
"source": "WebHarvest"
}
]]>
</template>
</def>
<file path="user-${userId}.json" action="write">${jsonOutput}</file>
</config>
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<!-- CSV header -->
<file path="products.csv" action="write">ID,Name,Price,Stock</file>
<!-- CSV rows with template -->
<loop item="product">
<xpath expression="//product">...</xpath>
<def var="id"><xpath expression="@id">${product}</xpath></def>
<def var="name"><xpath expression="name/text()">${product}</xpath></def>
<def var="price"><xpath expression="price/text()">${product}</xpath></def>
<def var="stock"><xpath expression="stock/text()">${product}</xpath></def>
<file path="products.csv" action="append">
<template>${id},"${name}",${price},${stock}</template>
</file>
</loop>
</config>
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="customerName" value="Jane Smith"/>
<def var="orderTotal" value="299.99"/>
<def var="orderId" value="ORD-98765"/>
<def var="emailBody">
<template>
<![CDATA[
Dear ${customerName},
Thank you for your order ${orderId}.
Order Details:
- Total: $${orderTotal}
- Date: ${_date}
- Estimated delivery: 3-5 business days
Track your order: https://example.com/track/${orderId}
Best regards,
The Team
]]>
</template>
</def>
</config>
<def var="products">
<template>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<products>
]]>
</template>
</def>
<loop item="product">
<xpath expression="//product">...</xpath>
<def var="name"><xpath expression="name/text()">${product}</xpath></def>
<def var="price"><xpath expression="price/text()">${product}</xpath></def>
<def var="products" value="${products}">
<template>
<![CDATA[
<product>
<name>${name}</name>
<price>${price}</price>
</product>
]]>
</template>
</def>
</loop>
<def var="products" value="${products}</products>"/>
<file path="products.xml" action="write">${products}</file>
when template contains XML/HTML tags$${
} to output literal ${
} in textlanguage
attribute