Question

I'm doing my first steps in Coldfusion8. I managed to setup a component/service with a cffunction I'm calling.

Inside the function I need to contruct a 2x2 table with errors and corresponding error messages. Error messages are multi-language and stored in a MySQL table.

Problem:

I can't find a way to reference my variables from inside a CFFunction tag. This does not work:

// select from database
<cfparam name="Session.Sprache" default="DE">
    <cfquery datasource="iln" name="texte">
        SELECT *
        FROM languages
        WHERE sprache = "#Session.lang#"
     </cfquery>

<cffunction name="createErrMsgsLog" returntype="object">
    // create and populate 
    <cfset allErrMsgs=ArrayNew(2)>
    <cfset allErrMsgs[1][1]="firma">
    // not defined
    <cfset allErrMsgs[1][2]="#tx_validate_firma#">
    ....
 </cffunction>

Question:
How can I reference my variables aka #tx_validate_firma# correctly inside CFfunction. They are always undefined.

EDIT:
Ok. This seems to work:

Inside application.cfc I'm calling:

<cfinvoke component="services.errorMsg"
     method="createErrMsgsLog"          
     returnvariable="errMsgs">
</cfinvoke>         

Inside errorMsg.cfc I'm doing:

<cfcomponent displayname="errorMsg">    
    <cffunction name="createErrMsgsLog">
        <cfquery datasource="mine" name="texte">
            SELECT *
            FROM sprachen
            WHERE sprache = "#Session.Sprache#"
        </cfquery>
        <cfoutput query="texte">
            // column name = value
            <cfset "#trim(bezeichner)#" = "#trim(textinhalt)#">
        </cfoutput>
        // build array
        <cfset allErrMsgs=ArrayNew(2)>
        <cfset allErrMsgs[1][1] = "firma">
        <cfset allErrMsgs[1][2] = #tx_validate_firma#>
        ...

        <cfset errMsgs = serializeJSON(allErrMsgs)>
        <cfreturn errMsgs>
    </cffunction>
</cfcomponent>  

This just seems like an awful lot of code...

Was it helpful?

Solution

Just a best practices answer here. When referencing a variable you can just reference the name, no need to use ##.

For example <cfset "#trim(bezeichner)#" = "#trim(textinhalt)#">

Can be <cfset "#trim(bezeichner)#" = trim(textinhalt)>

This doesn't solve your undefined problem, but is something you should follow moving forward (don't bother cleaning up old code), but this makes it much more readable imo.

OTHER TIPS

To use a value inside a function, you need to pass the value into the function as an argument value, eg:

<cffunction name="createErrMsgsLog" returntype="object">
    <cfargument name="someName" [etc]>

And then reference it as arguments.someName within the function.

Whilst you can break out of your function's internal scope and access variables from the main line, it's not considered particularly good practice to do so.

Another consideration is perhaps to abstract your functions out into component definitions in a CFC file, rather than just defining them within your "mainline" code. This makes for tidier, more coherent code, and makes it more reusable. That said, there's often justification for having a one-off function defined inline, too. But it's perhaps worth investigating:

http://livedocs.adobe.com/coldfusion/8/htmldocs/buildingComponents_01.html#1266855

Couple of things here to think about. Not so much an answer as some observations:

Please, for the sanity of yourself, those who have to maintain your code, and those who want to help, don't use "select *" in queries. From the comments that I've seen here, tx_validate_firma is returned in the query named texte. If the code explicitly said:

<cfquery datasource="iln" name="texte">
    SELECT tx_validate_firma, etc
    FROM languages
    WHERE sprache = "#Session.lang#"
 </cfquery>

we'd all know where the value came from. Oh, and please look at using cfqueryparam in your where clause.

I'm assuming that this works, though it seems odd:

<cfset allErrMsgs[1][2] = #tx_validate_firma#>

Because the scope of tx_validate_firma isn't explicitly stated, we outsiders are left scratching our heads, as might someone trying to maintain this later.

<cfset allErrMsgs[1][2] = texte.tx_validate_firma>

Note that you don't need to use # in assignments like this. Generally within a ColdFusion tag you won't need them unless you are inside quotation marks. Something like this:

<cfset fullName = "#firstName# #middleInitial# #lastName#">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top