<return>

Function return processor

Core v2.2.0

Overview

The processor is used inside definitions to explicitly return a value and immediately exit the function. If no is used, the function returns the result of its last expression.

Usage Examples

Example 1: Early return from function

example-1.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<function name="findProduct">
  <def var="query"><call-param name="query"/></def>
  
  <loop item="product" maxloops="100">
    <xpath expression="//product">...</xpath>
    
    <def var="name"><xpath expression=".//name/text()">${product}</xpath></def>
    
    <if condition="${name == query}">
      <return>${product}</return>  <!-- Found! Exit function immediately -->
    </if>
  </loop>
  
  <!-- Not found -->
  <return>NOT_FOUND</return>
</function>
</config>

Example 2: Conditional return

example-2.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<function name="validateEmail">
  <def var="email"><call-param name="email"/></def>
  
  <if condition="${empty(email)}">
    <return>false</return>
  </if>
  
  <regexp>
    <regexp-pattern>^[\\w.-]+@[\\w.-]+\\.\\w+$</regexp-pattern>
    <regexp-source>${email}</regexp-source>
  </regexp>
  
  <if condition="${!empty(group0)}">
    <return>true</return>
  </if>
  
  <return>false</return>
</function>
</config>

Important Notes

Related Processors