Reputation: 451
I'd like to pass specific values to various custom tags, like thus:
<cfif someerror>
<cfset mytag.bordercolor = "red">
</cfif>
<cf_input id="mytag">
Is this possible in some way?
Upvotes: 0
Views: 287
Reputation: 1046
I'm not sure I follow your question, so I'll toss out some possibly relevant information.
The syntax that you typed will work.
In the input.cfm file, you would reference Attributes.id. The value of which would be "mytag".
I would suggest using cfparam to set up a default value.
<cfparam name="Attributes.id" type="string" default="tag">
for example.
If you want to pass in the "mytag" structure instead of the string "mytag" then you would use the following sytax:
<cf_input id="#mytag#">
That would allow you to get the color using Attributes.mytag.bordercolor.
In which case, your cfparam would be more like:
<cfparam name="Attributes.id" default="#StructNew()#">
If you are wanting to pass in the string, but still get the color from the variables scope of the page then it would be something like this:
<cfif StructKeyExists(Caller,Attributes.id) AND StructKeyExists(Caller[Attributes.id],"bordercolor")>
<cfset Variables.bordercolor = Caller[Attributes.id].bordercolor>
</cfif>
This is because the variables scope of the calling page is available within the custom tag as "Caller" scope. I would advise being careful in using this, however, as you are breaking encapsulation. If you are reaching out to variable names that are specifically passed in that is probably OK, but it wouldn't generally be a good idea to get non-specified variables from Caller scope.
Upvotes: 0
Reputation: 13548
According to the documentation, what you have should work EXCEPT that you forgot to enclose the variable name between hashtags #
. Try this instead:
<cf_input id="#mytag#">
There are some important things to keep in mind on how the variables get passed to custom tags. From the documentation - Passing variables to custom tags and UDFs
Passing variables to CFML tags and UDFs
When you pass a variable to a CFML custom tag as an attribute, or to a user-defined function as an argument, the following rules determine whether the custom tag or function receives its own private copy of the variable or only gets a reference to the calling page's variable:
- Simple variables and arrays are passed as copies of the data. If your argument is an expression that contains multiple simple variables, the result of the expression evaluation is copied to the function or tag.
- Structures, queries, and cfobject objects are passed as references to the object.
If the tag or function gets a copy of the calling page's data, changes to the variable in the custom tag or function do not change the value of the variable on the calling page. If the variable is passed by reference, changes to the variable in the custom tag or function also change the value of the variable in the calling page.
To pass a variable to a custom tag, you must enclose the variable name in number signs. To pass a variable to a function, do not enclose the variable name in number signs.
Upvotes: 2