Validus … talkin bout <code> n sh!t

8Feb/120

Mach II: event.getArg() and event.isArgDefined()

Mach II being an event driven framework you can imagine what the Event Object is like. It's as described in the doco, the heart of Mach-II request life-cycle. In a nut shell, it pulls all the form & url variables form requests and throws it into the event object.

This will look for an event argument called title.
Either set by the URL or the Form.

URL:
http://localhost/index.cfm?event=home&title=Hello

Form:
<input type="hidden" name="title" value="Hello" />

Event args can also be defined in the mach ii config xml as well.

Mach-II.xml:
<event -arg name="title" value="Hello" />

Only recently I discovered that you can set a default value for getArgs(), previously I was writing heaps of excess code!

Ye ole days of coding:

<cfset title = '' />
<cfif event.isArgDefined('title')>
  <cfset title = event.getArg('title') />
</cfif>

Instead you can just do it all in one line, using the second param of getArg() as a default value.

<cfset title = event.getArg('title', 'No title set') />

You may have noticed in my first code example, I used another event object method called isArgDefined() - this is pretty much the same as doing StructKeyExists(event, 'mykey') I don't see any real reason why not to use either or, but preferable keep using event.isArgDefined() as this checks within the Event object scope.

No related posts.

Filed under: mach ii Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.