githubEdit

codeTemplating Language

Create dynamic templates with BoxLang's powerful templating language for HTML generation, views, and content rendering

BoxLang includes a powerful templating language designed for creating dynamic HTML, views, and content rendering. Template files use the .bxm extension and provide a natural way to mix static content with dynamic BoxLang code.

📄 Template File Structure

Template files (.bxm) are designed for content generation and HTML output. Unlike script files (.bxs) or class files (.bx), templates default to outputting content directly, making them ideal for views, email templates, and dynamic HTML generation.

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to BoxLang</title>
</head>
<body>
    <bx:output>
        <h1>Hello, #name#!</h1>
        <p>Today is #dateFormat( now(), "full" )#</p>
    </bx:output>
</body>
</html>

🔤 Output Interpolation

BoxLang templates use the # (hash/pound) character for output interpolation. Any expression wrapped in # symbols will be evaluated and output to the page.

circle-info

Important: For interpolation to work in templates, expressions must be surrounded by a <bx:output> component or within an outputting component context (like <bx:loop query="...">). Component attribute values are automatically evaluated and support interpolation without requiring <bx:output>.

Basic Interpolation

Escaping Hash Symbols

To output a literal # character without evaluation, use double hashes ##:

Complex Expressions

You can use any BoxLang expression within interpolation:

🏷️ Template Components

BoxLang provides a rich set of template components using the bx: prefix. All template components follow XML-like syntax with opening and closing tags or self-closing syntax.

Common Template Components

bx:output

The bx:output component enables output of dynamic content and expressions. Within bx:output, the # interpolation is automatically enabled.

Basic Usage:

Attributes:

  • query - Loop over a query object and output each row

  • group - Group query output by a column name

  • groupCaseSensitive - Whether group matching is case-sensitive (default: false)

  • startRow - Start outputting from this row number

  • maxRows - Maximum number of rows to output

  • encodefor - Automatically encode output ("html", "javascript", "url", "xml")

Query Loop Example:

Automatic Encoding Example:

Grouped Output Example:

bx:set

Define variables in templates using bx:set:

bx:script

Execute BoxLang script code within a template:

bx:if, bx:elseif, bx:else

Conditional rendering in templates:

bx:loop

Iterate over arrays, queries, structures, and ranges:

bx:include

Include other template files:

Additional Template Components

BoxLang provides many more template components for various purposes:

bx:param

Validate and set default values for variables:

bx:try / bx:catch / bx:finally

Exception handling in templates:

bx:throw

Throw custom exceptions:

bx:abort

Stop template execution:

bx:dump

Debug output for variables:

bx:exit

Exit from a specific execution context:

bx:savecontent

Capture output into a variable:

bx:silent

Suppress output:

🔄 Mixing Scripts and Templates

BoxLang allows seamless mixing of script and template syntax within the same file.

Scripts in Templates

Use <bx:script> to embed BoxLang script code within a template:

Templates in Scripts

In script files (.bxs), you can embed template code using template island notation with triple backticks (```):

🎯 Component Execution

BoxLang templates can execute any BoxLang component using the bx: prefix followed by the component name. The runtime automatically resolves and executes the component.

Built-in Components

BoxLang includes many built-in components for common tasks:

Custom Components

You can create your own custom components to extend the templating language with reusable functionality. Custom components are BoxLang templates or scripts that can be invoked like built-in components.

Creating a Custom Component

Create a component file (e.g., greeting.bxm):

Using Custom Components

Call your custom component using the bx:_ prefix:

Or using the bx:component tag:

Component Discovery

BoxLang searches for custom components in several locations:

  1. Relative to the current template

  2. Application-defined component paths (via Application.bx)

  3. Global component directories

Configuring Component Paths

In your Application.bx, you can specify custom component directories:

{% hint style="info" %} For comprehensive documentation on creating and using custom components, including advanced patterns, component scopes, and best practices, see the Components Framework Documentation. {% endhint %}

💡 Practical Examples

Dynamic HTML Page

Data Table with Query

Email Template

Form Processing

🔒 Security Considerations

When outputting user-generated content or data from external sources, always encode the output to prevent XSS attacks:

📚 Component Reference

For a complete list of available template components, see:

🎓 Best Practices

  1. Use templates for views: Template files (.bxm) are ideal for HTML generation and views

  2. Keep logic in scripts: Use <bx:script> or separate script files for complex business logic

  3. Encode output: Always encode user-generated content using encodeForHTML() and related functions

  4. Organize includes: Store reusable template fragments in a common directory structure

  5. Comment your templates: Use <!--- BoxLang comments ---> for documentation

  6. Use meaningful variable names: Make your templates readable and maintainable

Last updated

Was this helpful?