Answers.Body: <p>You can just make link to a twig template to output a csv quite simply...just like any other template really, except it won't extend you layout etc. and will simply contain something like:</p>
  
  <pre><code>{%for result in results %}
      {{var1}},{{var2}},{{var3}} etc...
  {% endfor %}
  </code></pre>
  
  <p>Depending on what sort of csv you're after and what you're going to do with the data, you may want to consider escaping the data as you output it:</p>
  
  <p><a href="http://twig.sensiolabs.org/doc/filters/escape.html" rel="nofollow noreferrer">http://twig.sensiolabs.org/doc/filters/escape.html</a></p>
  
  <p>In terms of setting it up as a  link, your csv template could just accept the same search parameters passed through and essentially repeat the search and output the csv results as above.</p>
  
  <p>I.e. in your original results page you could build the link to the CSV as </p>
  
  <pre><code>&lt;a href="/utility/csvmaker?q={{q}}"&gt;Get Results As CSV&lt;/a&gt;
  </code></pre>
  
  <p>And then in the csvmaker</p>
  
  <pre><code>{% set results = craft.entries.search(q).order('score') %}
  ...etc as above
  </code></pre>
  
  <p>But a lot depends on how you've built your search in the first place really.</p>
  
  <p>If you want it to trigger an actual file download, then I've found that's pretty easy to do that on the front end with some JS, e.g. I use this:</p>
  
  <p><a href="https://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser/20343999">https://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser/20343999</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-31T23:46:51.467Z
Answers.Text: Thanks for the answer!  I am working on implementing your solution and have just run across a few things, sorry but I'm fairly new to Craft...  For the route you suggest in escaping, how do you extend Craft to use the csv custom escaper?  and for the force download, that method did not work for me.  I included the script as listed and there was no change in behavior when the link was clicked.
Answers.UserDisplayName: user5824
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-01T01:37:47.217Z
Answers.Text: @ElisaC Well you can either use one of the standard twig escape filters, or you'd have to write your own twig filter in a plugin which is broadly described here: http://twig.sensiolabs.org/doc/advanced.html ... it's not too hard to write a filter in practise (look for a plugin with an existing one to copy), but that's an SE question in it's own right if you need it.
  
  The force download thing I would attempt after just getting it to work as a standard link - it shouldn't be too hard to adapt that JS but will be easier to help if you have something concrete working otherwise I think.
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-28T06:07:46.853Z
Answers.LastActivityDate: 2016-10-28T06:07:46.853Z
Answers.LastEditDate: 2017-05-23T12:38:53.957Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>I have set up a custom search of entries on a page in my Craft site which includes filters to filter the results.  I would like to be able to export these results (filtered or otherwise) as a CSV through a link on the page:
  <a href="http://www.joycefdn.org/grants" rel="nofollow">http://www.joycefdn.org/grants</a></p>
  
  <p>Is there a way to do this?</p>
  
Tags: <entries><search><frontend>
Title: How do I export search results as a csv?
ThreadText: # How do I export search results as a csv?
  
  (Asked by: username_0 on 2016-10-28)
  
  I have set up a custom search of entries on a page in my Craft site which includes filters to filter the results. I would like to be able to export these results (filtered or otherwise) as a CSV through a link on the page:
  <http://www.joycefdn.org/grants>
  
  Is there a way to do this?
  
  **Tags:** entries, search, frontend
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-10-28. Score: 5 | Accepted answer
  
  You can just make link to a twig template to output a csv quite simply...just like any other template really, except it won't extend you layout etc. and will simply contain something like:
  
  ```
  {%for result in results %}
      {{var1}},{{var2}},{{var3}} etc...
  {% endfor %}
  
  ```
  
  Depending on what sort of csv you're after and what you're going to do with the data, you may want to consider escaping the data as you output it:
  
  <http://twig.sensiolabs.org/doc/filters/escape.html>
  
  In terms of setting it up as a link, your csv template could just accept the same search parameters passed through and essentially repeat the search and output the csv results as above.
  
  I.e. in your original results page you could build the link to the CSV as
  
  ```
  <a href="/utility/csvmaker?q={{q}}">Get Results As CSV</a>
  
  ```
  
  And then in the csvmaker
  
  ```
  {% set results = craft.entries.search(q).order('score') %}
  ...etc as above
  
  ```
  
  But a lot depends on how you've built your search in the first place really.
  
  If you want it to trigger an actual file download, then I've found that's pretty easy to do that on the front end with some JS, e.g. I use this:
  
  <https://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser/20343999>
  
  > **Comments:**
  > **username_0**: Thanks for the answer!  I am working on implementing your solution and have just run across a few things, sorry but I'm fairly new to Craft...  For the route you suggest in escaping, how do you extend Craft to use the csv custom escaper?  and for the force download, that method did not work for me.  I included the script as listed and there was no change in behavior when the link was clicked.  
  > **username_1**: @ElisaC Well you can either use one of the standard twig escape filters, or you'd have to write your own twig filter in a plugin which is broadly described here: http://twig.sensiolabs.org/doc/advanced.html ... it's not too hard to write a filter in practise (look for a plugin with an existing one to copy), but that's an SE question in it's own right if you need it.  The force download thing I would attempt after just getting it to work as a standard link - it shouldn't be too hard to adapt that JS but will be easier to help if you have something concrete working otherwise I think.  
  
  ---
  
  