set variable value to .cfm output?

Discussion in 'Programming' started by Purple Martin, Apr 9, 2008.

  1. #1
    Is there a way to set the value of a String variable to the HTML output of an existing .cfm file?

    For example...

    say I have a .cfm file which contains the following:
    <cfset animal="monkey">
    <p>OMG! It's a #animal#!</p>
    Code (markup):
    and in a second file I want to set a variable so that the variable value is a string which is the HTML output of the first file i.e. the value will be:
    <p>OMG! It's a monkey!</p>
    Code (markup):

     
    Purple Martin, Apr 9, 2008 IP
  2. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #2
    Yes you can.

    Here are your options.

    One:
    Pass the variable in the url,
    
    
    <!--- Here is your first page called firstpage.cfm--->
    
    <cfset animal="monkey">
    
    <a href="secondpage.cfm?animal=<cfoutput>#animal#</cfoutput>">Secondpage</a>
    
    
    Code (markup):


    Option TWO:
    You can set a session variable if you are using session managment:
    which makes all variables you set in the session scope available to every page in your application.

    
    
    <cfset session.animal = 'Monkey'>
    
    <!---then to output the var you would do--->
    
    <cfoutput>#session.animal#</cfoutput>
    
    Code (markup):

    You can also pass variables to other pages with a form but like the passing vars in the url the user of your site has to click on a link or the submit button in this case inorder to pass the vars.

    Good luck
     
    unitedlocalbands, Apr 10, 2008 IP
  3. Purple Martin

    Purple Martin Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but it seems I didn't explain clearly what I need. The rendered output of a .cfm file, i.e. what would get sent to a browser, is what I want to use as the value of the variable in another file. Hope that makes sense now.
     
    Purple Martin, Apr 10, 2008 IP
  4. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Perhaps I didn't make myself clear.:cool:

    Only a couple of options when it comes to setting and using variables.

    1)Set a session variable
    2)Set the variable in a form to pass to new page
    3)Set a client Variable (works like a session variable)
    4)Set the Variable in the url to pass to new page
    5)When the page loads you can set a <cfparam> which will define a variable for that page.

    I'm not sure what I can suggest to you.

    Maybe you can post the code you are working with.

    Maybe I still don't understand what you would like to do?

    Or maybe the answer to your question is no, you cant pass variables they way you are describing. Sorry I cant help you further.

    On a second thought, heres a question for you.

    Why do you need to set a variable to the rendered output of a cfm page.

    Why not just set the variable to the var string that you want the new variable to be equal to?
     
    unitedlocalbands, Apr 10, 2008 IP
  5. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Sounds like UnitedLocalBands is on the money Purple Martin. The only other thing I would suggest is

    1) <cfhttp url="YourFirstCFMPage.cfm?WithURLVariables=ToWhatYouWant" timeout=15 method="get"></cfhttp>
    <cfhttp url="http://www.msn.com" timeout="15" method="get"></cfhttp>
    <cfset Session.YourVariable= cfhttp.FileContent>
    2) In your second page, have:
    <cfoutput>#Session.output#</cfoutput>
     
    datropics, Apr 12, 2008 IP
  6. dshuck

    dshuck Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I will try another swag at this...

    If you just want to store the content of an included cfm file, as is done with all the MVC frameworks as content is generated before being delivered to the final view, you could do something like this...

    in template A)
    <cfinclude template="templateB.cfm" />
    Code (markup):
    Then, the content of templateB.cfm could be something like this....
    
    <cfset animal="monkey">
    <cfsavecontent variable="SomeCoolVariable">
    <p>OMG! It's a #animal#!</p>
    </cfsavecontent>
    
    Code (markup):
    Then, later in the processing of template A)
    
    <cfoutput>#SomeCoolVariable#</cfoutput>
    
    Code (markup):
    Is that what you are trying to do?
     
    dshuck, Apr 14, 2008 IP
  7. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #7
    As you can see Purple Martin, there are many ways to tackle this. Hope you find one that fits your fancy
     
    datropics, Apr 14, 2008 IP