CBWIRE
v4.x
v4.x
  • Introduction
  • How It Works
  • Getting Started
  • Configuration
  • Releases
    • What's New With 4.1
    • What's New With 4.0
    • What's New With 3.2
    • What's New With 3.1
    • What's New With 3.0
    • What's New With 2.2
    • What's New With 2.1
    • What's New With 2.0
  • Resources
  • Upgrading from CBWIRE 3.x
  • Upgrading from CBWIRE 2.x
  • CBWIRE CLI
  • The Essentials
    • Components
    • Templates
    • Data Properties
    • Computed Properties
    • Data Binding
    • Actions
    • Events
    • Nesting Components
    • Lifecycle Methods
    • JavaScript
    • Testing
  • Features
    • Single-file Components
    • Alpine.js
    • Lazy Loading
    • Form Validation
    • File Uploads
    • Query String
    • Redirecting
    • WireBox
  • Template Directives
    • wire:click
    • wire:confirm
    • wire:dirty
    • wire:ignore
    • wire:init
    • wire:key
    • wire:loading
    • wire:model
    • wire:navigate
    • wire:offline
    • wire:poll
    • wire:stream
    • wire:submit
    • wire:transition
  • Advanced
    • Troubleshooting
Powered by GitBook
On this page
  • Overview
  • Implicit Rendering
  • Explicit Rendering
  • Using Data Properties
  • Using Computed Properties
  • Using Helper Methods

Was this helpful?

Export as PDF
  1. The Essentials

Templates

PreviousComponentsNextData Properties

Last updated 7 months ago

Was this helpful?

Overview

Templates are your ' HTML and consist of valid HTML/CFML tags. This includes <cfif>, <cfloop>, etc.

<cfoutput>
    <!--- HTML template goes here --->
    <div>
        <button wire:click="doSomething">Click here</button>
        <!-- Any valid CFML here -->
        <cfif datePart( 'h', now() ) lt 12>
            <p>Good morning!</p>
        <cfelse>
            <p>Good afternoon!</p>
        </cfif>
    </div>
</cfoutput>

Your templates must have a single outer element for CBWIRE to bind to your component and update the DOM properly. This can be any valid HTML element. Below, we are using a div element.

Below is a template with one outer element ( good ) and another with two outer elements ( bad ).

<cfoutput>
    <!--- GOOD: single outer element --->
    <div>
        <p>My awesome component</p>
    </div>
</cfoutput>

<cfoutput>
    <!--- BAD: 2 outer elements --->
    <div>
        My awesome component    
    </div>
    <div>
        <p>This won't work.</p>
    </div>
</cfoutput>

Implicit Rendering

By default, CBWIRE will look in the ./wires folder for a .cfm file with the same name as your component.

./wires/Counter.cfc <--- CFC file
./wires/Counter.cfm <--- Template file based on CFC name

Explicit Rendering

To override the default implicit rendering, we can tell CBWIRE where our component template is by defining a onRender() method on our component and using the template() method.

// ./wires/MyTemplate.cfc
component extends="cbwire.models.Component" {
    function onRender() {
        return template( "someFolder.MyTemplate" ); // renders ./somefolder/MyTemplate.cfm    
    }
}

We can pass parameters when calling template().

// ./wires/MyTemplate.cfc
component extends="cbwire.models.Component" {
    function onRender() {
        return template( "someFolder.MyTemplate", { "someVar": "value" } );
    }
}

In this case, someVar becomes available to our template.

<!--- ./someFolder/MyTemplate.cfm --->
<cfoutput>
    <div>
        Somevar: #someVar#
    </div>
</cfoutput>

You can also return your template inline like so:

// ./wires/MyTemplate.cfc
component extends="cbwire.models.Component" {
    function onRender() {
        return "<div>My component</div>";
    }
}

Using Data Properties

// ./wires/Greeter.cfc
component extends="cbwire.models.Component" {
    data = {
        "greeting": "Hello from CBWIRE"
    };
}
<!--- ./wires/greeter.cfm --->
<cfoutput>
    <div>
        Greeting: #greeting#
    </div>
</cfoutput>

Using Computed Properties

function greeting() computed {}
// ./wires/Greeter.cfc
component extends="cbwire.models.Component" {
    function greeting() computed {
        return "Hello from CBWIRE";
    }
}

You access computed properties in your template by invoking the method.

<!--- ./wires/greeter.cfm --->
<cfoutput>
    <div>
        Greeting: #greeting()#
    </div>
</cfoutput>

Using Helper Methods

You can access any global helper methods defined in your ColdBox application and any modules you have installed.

For example, suppose you've installed the cbi8n module ( an internalization module for ColdBox ). You can access its global helper methods in your template, such as the $r() method for displaying text in various languages.

<cfoutput>
    <div>
        #$r( "greeting" )#
    </div>
</cfoutput>

You can change this default location in the settings.

You can access your from your template by calling #propertyName#.

You define on your component by using the computed annotation.

Computed properties are cached are are executed when first called. See .

Visit to find ColdBox modules for your application.

components
configuration
Computed Properties
ForgeBox
data properties
computed properties