Skip to main content
Participant
October 12, 2018
Question

Recursive evaluation of string?

  • October 12, 2018
  • 1 reply
  • 624 views

I have HTML fragments with embedded CF tags stored in a field in my database. When I read the field from the database, I want to have the CF tags, etc. evaluated and store that in a string. 

Let's say Text contains "This is my #Content#" and Content contains "substituted content".   I want both to be evaluated (recursively) so that the substitutions take place correctly and I end up with "This is my substituted content".

I have tried this, but all I get is Final containing "This is my #Content#".

    <cfsavecontent variable="Final">

        <cfoutput>

        #Text#

        </cfoutput>

    </cfsavecontent>

Is there any way to get this recursive substitution to happen?

Thanks!

    This topic has been closed for replies.

    1 reply

    EddieLotter
    Inspiring
    October 12, 2018

    Use the evaluate() function.

    #evaluate(Text)#

    pete_freitag
    Participating Frequently
    October 12, 2018

    While the evaluate function may work in this case, I have to point out that from a security standpoint it is very dangerous to use evaluate because it could lead to remote code execution vulnerabilities.

    If possible simply do something like this:

    <cfset finalResult = replaceNoCase(text, "##content##", content, "ALL")>

    That replaces all occurrences of #content# with the value of the variable content. You would have to do that for each variable used. But if possible that is much more secure and much more performant than evaluate.

    If you need something more complicated, I'd look into a templating language eg: ColdBox Templating Language

    BKBK
    Community Expert
    Community Expert
    October 14, 2018

    pete_freitag  wrote

    If possible simply do something like this:

    <cfset finalResult = replaceNoCase(text, "##content##", content, "ALL")>

    Perhaps

        <cfset finalResult = replaceNoCase(text, "[[{{content}}]]", content, "ALL")>

    to avoid complications, as # is a special symbol in ColdFusion.