Skip to main content
Inspiring
November 19, 2010
Question

Make a variable from a cfscript result?

  • November 19, 2010
  • 2 replies
  • 781 views

I've got this on my page:

<cfscript>

    myxmldoc = XmlParse("D:\inetpub\xmlfile.xml");

    selectedElements = XmlSearch(myxmldoc, "/ContentThatWorks/Content/ArticleComponent/SubHead");

    for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)

        writeoutput(selectedElements.XmlText & "<br>");

</cfscript>

It displays what I want, but I want to be able to use that value in many parts of the page. How can I convert it to a variable for use elsewhere on the page?

    This topic has been closed for replies.

    2 replies

    Inspiring
    November 19, 2010

    If I understand you correctly, you want to put the output rendered by the writeOutput() statement into a variable?

    If so there are a couple of approaches you could consider.

    1) instead of just outputting the value, concatenate it to a variable in the loop, and then output it after the loop, eg:

    s = "";

    for(){

         s &= "stuff to add to string here";

    }

    writeOutput(s);

    2) use <cfsavecontent> instead (which'll need to go around you're entire <cfscript> block, which is less than ideal.

    http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7d57.html

    --

    Adam

    Inspiring
    November 19, 2010

    You are trying to make the stuff inside the loop a variable?  Looks like a simple concatonation to me.

    Squiggy2Author
    Inspiring
    November 19, 2010

    How would that concatenation work, Dan? Sorry, but I've been out of the CF loop for so long I can't really figure out how to apply what you're saying.

    Inspiring
    November 19, 2010

    Concatonation is described in Adam's answer.