9 Replies Latest reply on Apr 1, 2009 7:06 AM by Newsgroup_User

    Variable within a variable?

    drmaves
      I have a template that displays a variable that contains a string like this:

      <cfoutput>
      #content#
      </cfoutput>

      The variable 'content' is stored in a database table and contains text with varables within it, like this:

      My content is here with a variable with in it like this #keyword#.

      The text for the 'content' variable is supplied by the end user.

      The goal here is to have merge variables that the end user can put into their text, like first_name, last_name or keyword.

      The variable 'keyword' is a variable that is set to a URL variable at the beginning of the template.

      How can I get the 'keyword' variable to display its value?
      As it is now it just displays '#keyword#' and doesn't resolve the variable to the value.

      I know there must be a simple solution here but I just can's see it.

      Any help would be greatly appreciated.




      '



        • 1. Re: Variable within a variable?
          Stephan112
          if I understand you right you are looking for the function evaluate
          • 2. Re: Variable within a variable?
            Dan Bracuk Level 5
            The function evaluate won't work. After you extract your code from your db, you have to write it to a file and then cfinclude that file.

            • 3. Re: Variable within a variable?
              Level 7
              drmaves wrote:
              > I have a template that displays a variable that contains a string like this:
              >
              > <cfoutput>
              > #content#
              > </cfoutput>
              >
              > The variable 'content' is stored in a database table and contains text with
              > varables within it, like this:
              >
              > My content is here with a variable with in it like this #keyword#.

              Pseudocode, not tested:

              do{
              r = reFindNoCase("##[a-z0-9]*##", content, 1, true);
              found = r.pos[1] gt 0;

              if( found ) {
              varName = Mid(content, r.pos[1], r.len[1]);
              replaceNoCase(content, "##" & varName & "##", variables[varName],
              "ALL");
              }

              }while(found)

              This assumes that all your variables are in Variables (and not in
              Session, Application, etc).

              --
              Mack
              • 4. Re: Variable within a variable?
                drmaves Level 1
                Mack,

                Sorry for my ignorance but is this javascript or cfscript?

                How would I apply this given my example?

                Roger
                • 5. Re: Variable within a variable?
                  Dan Bracuk Level 5
                  It's cfscript that changes this #keyword# to variables[keyword]. It won't help you.
                  • 6. Re: Variable within a variable?
                    drmaves Level 1
                    Dan,

                    It seems like there must be some way to do this inline without writing a separate file and including it.
                    Is this something you've been through before and ended up with this as the only solution?

                    Roger
                    • 7. Variable within a variable?
                      drmaves Level 1
                      Okay, I found a way to do this at least for one variable.

                      I changed the #keyword# to {keyword} and then used the following funciton to replace it:

                      #Replace(content,'{keyword}', keyword)#

                      It worked!

                      Now the question is, "What is the most elegant way to handle multiple variables within the content?"

                      Looks like ReplaceList() should work.
                      • 8. Re: Variable within a variable?
                        Level 7
                        > I changed the #keyword# to {keyword} and then used the following funciton to
                        > replace it:
                        >
                        > #Replace(content,'{keyword}', keyword)#
                        >
                        > It worked!
                        >
                        > Now the question is, "What is the most elegant way to handle multiple
                        > variables within the content?"

                        The most elegant way of doing it is to write it to a file and using
                        <cfinclude>, like Dan said.

                        Doing that means you don't have to horse around with tokens like {keyowrd},
                        or running really slow string function on each token you've got. You're
                        already got the CF compiler on hand to do all that sort of stuff with you.
                        The one conceit the compiler has is that you need to give it a *file*.

                        Save the file. Include the file.

                        --
                        Adam
                        • 9. Re: Variable within a variable?
                          Level 7
                          Adam Cameron wrote:
                          >
                          > Save the file. Include the file.
                          >

                          For simple evaluation of variables you can use the combination of
                          evaluate() and de() 'delay evaluate' functions.

                          drmaves wrote:
                          > I have a template that displays a variable that contains a string
                          like this:
                          >
                          > <cfoutput>
                          > #content#
                          > </cfoutput>

                          This can be done with: <cfoutput>#evaluate(de(content))#</cfoutput>

                          But to do anything that has CFML in the data you have to save the file
                          and include the file.