• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Variable within a variable?

Explorer ,
Mar 30, 2009 Mar 30, 2009

Copy link to clipboard

Copied

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.




'



TOPICS
Advanced techniques

Views

800

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 31, 2009 Mar 31, 2009

Copy link to clipboard

Copied

if I understand you right you are looking for the function evaluate

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 31, 2009 Mar 31, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 31, 2009 Mar 31, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 31, 2009 Mar 31, 2009

Copy link to clipboard

Copied

Mack,

Sorry for my ignorance but is this javascript or cfscript?

How would I apply this given my example?

Roger

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 31, 2009 Mar 31, 2009

Copy link to clipboard

Copied

It's cfscript that changes this #keyword# to variables[keyword]. It won't help you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 31, 2009 Mar 31, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 31, 2009 Mar 31, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 31, 2009 Mar 31, 2009

Copy link to clipboard

Copied

> 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2009 Apr 01, 2009

Copy link to clipboard

Copied

LATEST
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation