loop inside savecontent

234 views
Skip to first unread message

ADK

unread,
Sep 22, 2014, 5:34:04 PM9/22/14
to ra...@googlegroups.com
In cfscript I am trying to loop over a query inside of a savecontent block (basically generating an HTML table inside of a cfc method for output).

In the tag based version of cfsavecontent this works easily with either a cfoutput query or a cfloop query, however for the life of me I cannot get anything but the last query row to output inside the savecontent block? I've tried various versions of nested cfoutput's and writeoutput's etc. but cannot seem to come up with the right combination to make this work (or perhaps it's impossible with cfscript?)

Igal @ getRailo.org

unread,
Sep 22, 2014, 5:36:38 PM9/22/14
to ra...@googlegroups.com
should be something like:

savecontent variable="output" {

  loop query=someQuery {

    echo(someQuery.column1);
  }
}

echo(output);




On 9/22/2014 2:34 PM, ADK wrote:
In cfscript I am trying to loop over a query inside of a savecontent block (basically generating an HTML table inside of a cfc method for output).

In the tag based version of cfsavecontent this works easily with either a cfoutput query or a cfloop query, however for the life of me I cannot get anything but the last query row to output inside the savecontent block? I've tried various versions of nested cfoutput's and writeoutput's etc. but cannot seem to come up with the right combination to make this work (or perhaps it's impossible with cfscript?)
--
Did you find this reply useful? Help the Railo community and add it to the Railo Server wiki at https://github.com/getrailo/railo/wiki
---
You received this message because you are subscribed to the Google Groups "Railo" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/railo/cdc6c47f-dcee-4405-a9b4-a97182d8453f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Igal Sapir
Railo Core Developer
http://getRailo.org/

ADK

unread,
Sep 22, 2014, 5:44:52 PM9/22/14
to ra...@googlegroups.com
Thanks Igal - that's how I have it. That works if just the query. However I am trying to generate a table, so inside my loop would require tr's and the query column values outputted inside td's. THis is where it's breaking down.

Mark Drew

unread,
Sep 22, 2014, 5:47:34 PM9/22/14
to ra...@googlegroups.com
Post your code or gtfo. 

Mark Drew

ADK

unread,
Sep 22, 2014, 6:00:25 PM9/22/14
to ra...@googlegroups.com
savecontent variable="table" {
    echo('
    <table class="table table-striped table-super-condensed">
        <thead>
            <tr>
                <th>Date</th>
                <th>PO Number</th>
                <th>Ship Method</th>
                <th>Terms</th>
                <th>Buyer</th>
                <th class="text-right">Total</th>
                <th class="text-right">Status</th>
            </tr>
        </thead>
        <tbody>
        loop query="qPOs" {
            echo(
            <tr>
                <td>#DateFormat(qPOs.EnteredOn,"yyyy-mm-dd")#</td>
                <td>#qPOs.PONO#</td>
                <td>#qPOs.ShipMethod#</td>
                <td>#qPOs.Terms#</td>
                <td>#qPOs.EnteredBy#</td>
                <td>#DollarFormat(qPOs.Total)#</td>
                <td>#qPOs.isClosed#</td>
            </tr>
        )}
        </tbody>
    </table>           
    ');

Igal @ getRailo.org

unread,
Sep 22, 2014, 6:01:48 PM9/22/14
to ra...@googlegroups.com
you can call echo() inside a loop, but you can not do a loop inside echo().

For more options, visit https://groups.google.com/d/optout.

ADK

unread,
Sep 22, 2014, 6:08:18 PM9/22/14
to ra...@googlegroups.com
Thanks Igal. So is there a way in cfscript to achieve this tag based solution:

<cfsavecontent variable="table">

<table class="table table-striped table-super-condensed">
    <thead>
        <tr>
            <th>Date</th>
            <th>PO Number</th>
            <th>Ship Method</th>
            <th>Terms</th>
            <th>Buyer</th>
            <th class="text-right">Total</th>
            <th class="text-right">Status</th>
        </tr>
    </thead>
    <tbody>
    <cfoutput>
    <cfloop query="qPOs">

        <tr>
            <td>#DateFormat(qPOs.EnteredOn,"yyyy-mm-dd")#</td>
            <td>#qPOs.PONO#</td>
            <td>#qPOs.ShipMethod#</td>
            <td>#qPOs.Terms#</td>
            <td>#qPOs.EnteredBy#</td>
            <td>#DollarFormat(qPOs.Total)#</td>
            <td>#qPOs.isClosed#</td>
        </tr>
    </cfloop>
    </cfoutput>
    </tbody>
</table>
</cfsavecontent>

Igal @ getRailo.org

unread,
Sep 22, 2014, 6:10:54 PM9/22/14
to ra...@googlegroups.com
yes, split your echo() calls



savecontent variable="table" {
    echo('
    <table class="table table-striped table-super-condensed">
        <thead>
            <tr>
                <th>Date</th>
                <th>PO Number</th>
                <th>Ship Method</th>
                <th>Terms</th>
                <th>Buyer</th>
                <th class="text-right">Total</th>
                <th class="text-right">Status</th>
            </tr>
        </thead>
        <tbody>
    ');    // close echo() call

        loop query="qPOs" {
            echo(
            <tr>
                <td>#DateFormat(qPOs.
EnteredOn,"yyyy-mm-dd")#</td>
                <td>#qPOs.PONO#</td>
                <td>#qPOs.ShipMethod#</td>
                <td>#qPOs.Terms#</td>
                <td>#qPOs.EnteredBy#</td>
                <td>#DollarFormat(qPOs.Total)#</td>
                <td>#qPOs.isClosed#</td>
            </tr>
        );    // close echo() call
        }    // close loop

    echo('
        </tbody>
    </table>           
    ');
}

For more options, visit https://groups.google.com/d/optout.

ADK

unread,
Sep 22, 2014, 6:16:59 PM9/22/14
to ra...@googlegroups.com
Thanks Igal - that was it. Well almost: the contents of the echo inside the loop need to be surrounded in single quotes too, but then it works.

Adam Cameron

unread,
Sep 23, 2014, 2:14:46 AM9/23/14
to ra...@googlegroups.com


On Monday, 22 September 2014 23:08:18 UTC+1, ADK wrote:
Thanks Igal. So is there a way in cfscript to achieve this tag based solution:


Why are you trying to reimplement code that is an absolute sitter for being tags, in CFScript?

-- 
Adam 

ADK

unread,
Sep 23, 2014, 3:22:01 AM9/23/14
to ra...@googlegroups.com
short answer: it's a method in a script-based cfc that renders data in various formats based on certain logic. Of course, this specific situation could be pulled out to a view cfm page, but this turned into an exercise in "why does this work so simply in tags, but such a PITA in script?" etc.

I suppose I'll chalk this up to another situation like cfquery that just works better as a tag...

Adam Cameron

unread,
Sep 23, 2014, 12:31:33 PM9/23/14
to ra...@googlegroups.com


On Tuesday, 23 September 2014 08:22:01 UTC+1, ADK wrote:
short answer: it's a method in a script-based cfc that renders data in various formats based on certain logic. Of course, this specific situation could be pulled out to a view cfm page, but this turned into an exercise in "why does this work so simply in tags, but such a PITA in script?" etc.

Because tag-centric code kinda will work better than trying to use script to achieve the same thing. Apples/oranges.

That said, in your case it was more than you just did it wrong than it being particularly "worse". You just needed your CFML constructs outside your string. Once you've done that, the code isn't that different form the tag version.

But it would still be better than being left as tags (and included- / called-into your script-based CFC). Your situation is demonstrating the code probably wasn't that well organised to start with, I'm afraid.
 
I suppose I'll chalk this up to another situation like cfquery that just works better as a tag...

Yup. Tags are great in their place. It's just "their place" is nowhere near as ubiquitous as CFML devs seem to think it is.

-- 
Adam
Reply all
Reply to author
Forward
0 new messages