setCustomMuraScopeKey - Mura Docs v7.0

setCustomMuraScopeKey

As you can see, the Mura Scope is a great way to access Mura CMS variables, data, and objects. There may be times when you want to add your own variables, data, and/or objects to the Mura Scope as well, and as you'll see, it's very easy to do just that.

In the Mura Events section, the various event/request lifecycles and contextual events that occur within Mura are covered. At any point within the lifecycle, or contextual event, you can choose to register your own custom Mura Scope key. Once it has been registered, the key will continue to be available for the remainder of the subsequent lifecycle and/or contextual event.

Function Syntax

m.setCustomMuraScopeKey(
name
, value
)

Parameters

Parameter Type Req/Opt Default Description
name string Req   This is the name of the custom Mura Scope key.
value any Req   This is the "value" returned when referencing the Mura Scope key.

Examples

To register your custom Mura Scope key, you first select the point in the lifecycle, or contextual event, you wish to create your custom key, and then choose your desired event handler to place your custom logic (e.g., the Site eventHandler.cfc, Theme eventHandler.cfc, or any other registered event handler). [NOTE: add link to event handler section here]

Setting a Custom Key to a Simple String

The example below is using the onSiteRequestStart event to register a simple string to be stored in the Mura Scope.

public function onSiteRequestStart(m) {
m.setCustomMuraScopeKey('myCustomKey', 'This is my custom Mura Scope Key value!');
}

Accessing the Custom Key

<cfoutput>
  <p>#m.myCustomKey#</p>
</cfoutput>

The above example should output the following:

<p>This is my custom Mura Scope Key value!</p>

Setting a Custom Key to an Object

First, we'll need an example object. Let's assume you have the following code in a file labeled exampleObject.cfc.

component {
  public function sayHello() {
return 'Hello from exampleObject.cfc!';
  }
}

Next, we'll select an event to add our logic to, and set a custom key that references the example object.

public function onBeforeUserSave(m) {
  var myObject = new exampleObject();
  m.setCustomMuraScopeKey('myObject', myObject);
}

Because we've specified the onBeforeUserSave event in the example above, our new Mura Scope Key will be available in the onAfterUserSave event, if needed.

Accessing the Custom Key's Object

<cfoutput>
#m.myObject.sayHello()#
</cfoutput>

The above example should output the following:

Hello from exampleObject.cfc!