Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-04-24 10:44:15

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,736
Website GitHub

Export a CSV without markup and trace log

Stupid question, and I’m sure I’ve solved this before but can’t find the answer. If I want to create a page template that puts a form at the top to allow someone to select articles to export (section, date range, modified since, etc), and then a shiny Go button that will submit the form and spit out a CSV file of the chosen records, how do I get the CSV file to be served with the mime header but NOT have any of the trace info when the site is running in debugging/testing modes (which I do from time to time). That knackers up the CSV file.

<txp:header name="Content-Disposition" value='attachment;filename=products-<txp:date format="yyyy-MM-dd" />.csv' />
<txp:header name="Content-Type" value="text/csv" />
<article::custom with attributes read from form submission>
   <txp:article_field 1 />,
   <txp:article_field 2 />,
   ...
</article::custom>

I’ve tried adding <txp:txp_die status="200"/> to the end but that just serves an OK page.
I’ve tried adding <txp:php>exit;</txp:php> to the end but that just kills all output.
I’ve tried adding <txp:php>ob_flush();</txp:php> to the end but it makes no difference.

Is there a better way to output CSVs e.g. using ?f=? Or should I be using PHP instead of Txp tags (e.g. fputcsv()) and use parse() inside PHP blocks to output the dynamic tag content? I’d rather do it in tags if I can because it makes it more readable for others to tweak the output.

I’m obviously missing something simple. Any ideas please?


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#2 2025-04-29 22:29:14

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,736
Website GitHub

Re: Export a CSV without markup and trace log

I’m convincd that ?f=csv_export is the way forward, because nothing else (e.g. messing with ob_end_clean() etc) works.

If I type ?f=csv_export in the URL directly and hit enter (with my CSV-generation code in that Form, of course) it spits out the rows without all the top-and-tail additional page HTML furniture and stacktrace, etc (at least in Testing mode: in Debugging mode, the script times out, so there must be something funky going on, but I’m ignoring that!)

All good.

However, this doesn’t work:

<form id="export_query" action="<site::url/>?f=csv_export">
<label for="export_type">Product type</label>
<select multiple id="export_type" name="export_type[]">
<option value="">All</option>
<section::list break="option" />
</select>
<input type="submit" value="Go!">
</form>

When I hit the submit button it hits the home page and has just ? in the URL.

Do I need to preserve ‘f’ as a context somehow? Do I need to trap it in the index page code explicitly and divert to the form?


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2025-04-29 22:42:37

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,736
Website GitHub

Re: Export a CSV without markup and trace log

Bloke wrote #339640:

Do I need to preserve ‘f’ as a context somehow?

Duh, it was being overwritten by the form submission values, hence the single ? when I didn’t select anything.

The solution is to remove the action and add this to the form:

<input type="hidden" name="f" value="csv_export">

The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 Yesterday 08:34:44

etc
Developer
Registered: 2010-11-11
Posts: 5,376
Website GitHub

Re: Export a CSV without markup and trace log

Possibly, setting global $production_status = 'live' would work too?

Offline

#5 Yesterday 09:15:59

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,736
Website GitHub

Re: Export a CSV without markup and trace log

etc wrote #339644:

Possibly, setting global $production_status = 'live' would work too?

That’s a good idea. I’ll try that, thank you.

Can’t fathom why it times out in debugging mode. It seems to dump the remaining html page content above and below each row of the result set. No warnings. No errors. Just a row of CSV data surrounded by blocks of html markup. Repeat.

Weird.

In Testing mode it drops the csv table perfectly in one block and tacks on the stack trace (which I don’t want). In Live mode, I just get the csv table, as I want.

There’s got to be a way to silence the markup or blat the buffer and start fresh. With one of the scripts that doesn’t output the same mime type every time (so I can’t use the ?f= trick), I’ve had to hack the page template with a check as the first line to see if a URL var is set and, if so, just call <txp:article>. Otherwise output the regular page markup to display the form controls etc. That’s annoying and I’d much rather do it in the regular page flow and simply tell Txp to ditch the markup in the buffer so far and send a new header when I need it.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#6 Yesterday 09:57:49

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,889
Website GitHub

Re: Export a CSV without markup and trace log

A wild guess: gocom has a plugin rah_debug that does the opposite. Might this line in reverse help?


TXP Builders – finely-crafted code, design and txp

Offline

#7 Yesterday 10:02:02

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,736
Website GitHub

Re: Export a CSV without markup and trace log

jakob wrote #339647:

Might this line in reverse help?

Huh. Didn’t know trace had a quiet mode. That seems like a good ticket too. I’ll play around. Thank you.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB