Hi gang. I've been struggling with a syntax probl...
# cfml-general
n
Hi gang. I've been struggling with a syntax problem related to verifying a value in a struct that has dynamic naming. Before I give up on this approach, I wanted to get see if anyone has feedback. Here is the code where I'm checking for a variable "hidemenu" within a struct that has a variable in the name (ie RoleID, which can change): <cfif structFind(application.sitesettings.RoleID_#RoleID#,"hidemenu") EQ "on"> This seems like it should be simple, and I don't want to pose frivolous questions here, but it hasn't been for me after quite a bit of trial and error. Any suggestions?
n
@Adam Cameron Thanks. I've been through the CF documentation quite a bit already, but it looks like the syntax examples in that link are worth reviewing.
a
It's the associative array stuff. It lets you define a key reference dynamically. You can't do that with dot notation.
d
Struct keys are just strings, can be calculated dynamically. Look at constructs like this:
Copy code
someStruct["foo_#somevar#_#someothervar#_bar"]
n
@Dave Merrill Thank you. I have the Struct set up correctly (e.g. application.adminsettings.roleID_#RoleID#). I can see that in my dump. But, when I try to check for a key/value within that Struct using StructFind or StructKeyExists, I keep getting errors. If there is a way to do this, it probably has to do with the associative array syntax that I haven't cracked.
m
Bracket notation is your best friend:
<cfif structFind(application.sitesettings["RoleID_#RoleID#"],"hidemenu") EQ "on">
n
@Myka Forrest Thanks, I think you may be on to it. Trying that now.
m
It's the same thing everyone else has been saying...
🤣 1
n
@Myka Forrest this has been a brutal learning experience, but I think this is the solution:
<cfif structFind(application.adminsettings.AdminAccessRole["AdminAccessRoleID_#AdminAccessRoleID#"],"HIDELEFTMENU") EQ "on"> <cfoutput>HELLO WORLD<br></cfoutput> </cfif> --------------------------------- getting the dots, brackets and quote marks in the exact right places was difficult
I have not seen a lot of good documentation on the use of bracket notation. The challenging part here (for me) was that the variable was in the name of the struct, not in the name of the key (which seems like a simpler scenario)
This appears to be working so many, many thanks after a long night
m
The name of the struct is a struct itself, so the same concept can be applied.
n
haha, still getting my head around that - "the name of a struct is a struct itself" still sounds like a zen koan to me, but working on it.
m
This is tangential, but you likely don't need to wrap your "HELLO WORLD" in
<cfoutput>
tags; you're not evaluating any variables within them.
z
the struct is most likely in a struct called
local
or
variables
n
@Myka Forrest I get that, but this was an odd situation where there was no display unless I put the text inside cfoutput. It may have something to do with the way that Fusebox methodology works (a bit old fashioned)
@zackster Yes, I have a bunch of structs within application variables for various settings. In this case, the application variables were grouped in a roleid that could change (ie similar to: application.adminsettings.roleid_#roleid#)
d
Personally, I don't like looking for stuff in the variables or locals scope, so I'd create a statically named struct as a container, and structure your data in there. Re needing cfoutput, if some higher level code has set enablecfoutputonly=true, you get no output without cfoutput.