echo_foreach_split
The `echo_foreach_split` directive iteratively executes a series of commands for each item in a split input string.
Description
The echo_foreach_split directive is part of the NGINX Echo module, which allows the execution of commands based on a split input string across multiple iterations. When this directive is called, the input string is divided into separate elements based on a specified delimiter, and for each element, the subsequent commands defined after echo_foreach_split are executed in sequence. This is particularly useful for performing repetitive tasks where the specifics of the task may vary based on the contents of the split string.
The directive takes at least two arguments: the first argument is the input string to be split, and the second argument is the delimiter that separates the elements within that string. After the string has been split, the commands that follow this directive can utilize the current element dynamically, enabling workflow automation and streamlined processing of multiple items.
One common use case for echo_foreach_split is to read a comma-separated list of values, then iterate over the list, executing designated commands for each value. This approach can help with logging, generating responses, or processing inputs richer than standard single-value directives allow.
Config Example
location /process_items {
echo_foreach_split "item1,item2,item3" ",";
echo "Current item: $echo_it";
}Ensure the input string is not empty; otherwise, no iterations will occur.
Be cautious with the delimiter choice to avoid unintended splits. For special characters, escaping might be necessary.
Commands defined after echo_foreach_split must be valid and handle the current item appropriately.