1

I'm working on two templates which use the same snippet for outputting data. One is a HTML template which displays a table, the other forces the download of a CSV.

With some conditionals I've set up my snippet to replace all table tags with either nothing, spaces or commas for the CSV delimiters, but the CSV output still has all the line breaks which I'd ideally like to keep in place so that I can still actually read the template when I need to edit it.

The template that downloads the CSV looks like this:

<?php
    header('Content-type: text/csv');
    header("Content-Disposition: attachment; filename=file-{current_time format='%d-%m-%Y'}.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
?>
{sn_filter_urls}

Inside the snippet is

{if segment_2=='html-template'}<table>
  <thead>
    <tr>
      <th>Line type</th>
      <th>Campaign</th>
      <th>Labels</th>
      <th>Campaign Daily Budget</th>
      <th>Campaign Type</th>
      <th>Networks</th>
      <th>Languages</th>
      <th>Bid Strategy Type</th>
      <th>Enhanced CPC</th>
      <th>Ad Rotation</th>
      <th>Delivery Method</th>
      <th>Ad Group</th>
      <th>Max CPC</th>
      <th>Final URL</th>
      <th>ID</th>
      <th>Location</th>
      <th>Criterion type</th>
      <th>Keyword</th>
      <th>Description</th>
      <th>Headline 1</th>
      <th>Headline 2</th>
      <th>Path 1</th>
      <th>Path 2</th>
      <th>Comment</th>
      <th>Link text</th>
      <th>Feed name</th>
      <th>Header</th>
      <th>Snippet Values</th>
    </tr>
  </thead>
  <tbody>
  {/if}
  {if segment_2=='csv-template'}Campaign,Labels,Campaign Daily Budget,Campaign Type,Networks,Languages,Bid Strategy Type,Enhanced CPC,Ad Rotation,Delivery Method,Ad Group,Max CPC,Final URL,ID,Location,Criterion type,Keyword,Description,Headline 1,Headline 2,Path 1,Path 2,Comment,Link text,Feed name,Header,Snippet Values{/if}
    {exp:channel:entries}
        {embed='_embeds/.filter_url_activity_title'}
    {/exp:channel:entries}
  {if segment_2=='filter-urls'}</tbody>
</table>{/if}

And inside the embedded template:

{exp:channel:entries}
  {exp:super_search:results}
    {if segment_2=='html-template'}<tr>{/if}
      {if segment_2=='html-template'}<td>{/if}DATA{if segment_2=='html-template'}</td>{if:else},{/if}
      {exp:loopee forint="1" to="17" by="1"}{if segment_2=='filter-urls'}<td></td>{if:else},{/if}{/exp:loopee}
      … other table markup …
    {if segment_2=='html-template'}</tr>{/if}
    {sn_sitelinks}
  {/exp:super_search:results}
{/exp:channel:entries}

There's another snippet in there which also has similar table markup but the problem with the line breaks exists in the short example above. Wherever I'm using loopee to output markup or , the , all come out on one line which is what I want, but where I've put line breaks in my template so a new td starts on a new line, that's showing up in the CSV output.

Is there a way I can have the contents of sn_filter_urls run through a process to remove the line breaks before it's then downloaded?

2

You can use a wonderful EE utility called Streeng to do this (and lots of other things).

Streeng performs a broad variety of manipulations on the tag outputs, including stripping out some or all of the <html> tags within it, and performing substitutions. You could use these features to achieve what you are describing (and remove the conditional statements used in your code at same time).

With Streeng installed, to get a version of the tag output with <html> tags removed and the line feeds replaced by single spaces you would need to enclose the tag as follows:

{exp:streeng allowed="none" find="NEWLINE" replace=" "} {sn_filter_urls} {/exp:streeng}

HTH

Extra!

In response to your comment about it not working, I did this test:

Create template partial {sn_test_text} containing:

Here is some stuff
Here is a new line 
Here is the last line.

Then created a template with this code in:

{exp:streeng 
  allowed="none" 
  find="NEWLINE" 
  replace=" "}
    {sn_test_text}
{/exp:streeng}

And then when I run the template I get this output

Here is some stuff Here is a new line Here is the last line.
| improve this answer | |
  • Doesn't seem to work when wrapped around a snippet. – Tyssen Oct 11 '17 at 0:08
  • hmm. interesting, works fine here - what output do you get? Have appended test results to answer above. – JCOGS Design Oct 11 '17 at 11:52
  • The snippet contains some embeds which is where I guess it's falling down. – Tyssen Oct 11 '17 at 22:26
  • You could try adding parse="inward" to the streeng tag - or post what you actually have here and I could have a look – JCOGS Design Oct 12 '17 at 8:34
  • I've added more code to the original question but I'm thinking I'm just going to have to go with two separate templates rather than trying to work with a single snippet for both. – Tyssen Oct 16 '17 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.