Skip to content

CBWire Application Helper : wireGenericView()#263

Open
mrigsby wants to merge 1 commit intocoldbox-modules:developmentfrom
mrigsby:generic-wire-view
Open

CBWire Application Helper : wireGenericView()#263
mrigsby wants to merge 1 commit intocoldbox-modules:developmentfrom
mrigsby:generic-wire-view

Conversation

@mrigsby
Copy link
Contributor

@mrigsby mrigsby commented Mar 21, 2026

CBWire Application Helper : wireGenericView()

Have you ever found that you end up with a plethora of ColdBox views that contain nothing more than this:

<cfoutput>
    <h1>My Page/Wire Title</h1>
    #wire( "myCoolWire" )#
</cfoutput>

If you have, you know how it can clutter up your views folder with very generic ColdBox views. The wireGenericView() application helper bypasses the need to create these very generic views and sets the view in your handler for you to a generic view from the CBWire module! When used you do not need to call event.setView() in your handler.

Method Signature:

/**
 * A helper function to set up the view arguments and call the generic wire view.
 * Eliminates the need to manually create a basic view that would usually only contains a
 * title (optional) and the wire() method call with the appropriate arguments.
 *
 * @wireName string | The name of the wire component to render. Required.
 * @wireParams struct | The parameters to pass to the wire component. Optional, defaults to an empty struct.
 * @viewArgs struct | Additional arguments to pass to the view. Optional, defaults to an empty struct.
 * @viewArgs.title string | An optional title to display above the wire component in the view. { "title" : "My Cool Page Title" }
 * @viewArgs.titleTag string | An optional HTML tag to wrap the title in. Defaults to h1 if title is provided without a titleTag. { "titleTag" : "h2" }
 *
 * @return void | Sets the events view to the generic wire view with the appropriate arguments.
 */
function wireGenericView( required string wireName, struct wireParams = {}, struct viewArgs={} );

Usage Examples:

Simple use in your handler:

// Handler Method
function viewUsers( event, rc, prc ){
    wireGenericView( "users.viewAll" );
}

Pass params to the wire() function:

// Handler Method
function viewUsers( event, rc, prc ){
    wireGenericView( "users.viewAll", { "inactive" : false } );
}

This will the pass the wireParams argument struct into the wire() methods params argument for you.

Set a custom title in the view & pass wire params:

// Handler Method
function viewUsers( event, rc, prc ){
    wireGenericView( 
    	"users.viewAll",
    	{ "inactive" : false },
    	{ "title" : "All Active User Accounts" }
    );
}

Set custom title in the view using a title tag other other the default H1 and pass wire params:

// Handler Method
function viewUsers( event, rc, prc ){
    wireGenericView( 
    	"users.viewAll",
    	{ "inactive" : false }, 
    	{ "title" : "All Active User Accounts", "titleTag" : "h5" } 
    );
}

It is a small addition and does not suite every situation. However, when it does suit the situation it can help to improve the organization and manageability of your views folder.

@grantcopley grantcopley added this to the v5.1 milestone Mar 23, 2026
@grantcopley grantcopley added the enhancement New feature or request label Mar 23, 2026
@grantcopley
Copy link
Collaborator

grantcopley commented Mar 23, 2026

@mrigsby Awesome idea. Setting as enhancement and targeting for 5.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants