Handlebar Helper Function: Set

Performs local variable assignment within its block scope. It injects one or more variables into the current context for use only within the block. Variables assigned with set are local to the block and don’t persist outside the {{#set}}...{{/set}} block.

ParameterTypeDescription
varNameanyVariable name and value pairs such as name="John" and age=30. Variable names must be valid identifiers. Set at least one variable.

The function returns the rendered content of the block with all the specified variables available in the context. The variables are available only within the block scope and don’t persist outside of it.

The helper creates a new context by cloning the existing one, adding the provided key-value pairs, and then rendering the inner block with the updated context. This method preserves the parent context. All parent variables remain accessible directly, unlike the with helper, which replaces the context.