<p>An alternative to using a plugin to write an actual text file to the local filesystem is to create a <a href="https://craftcms.com/docs/routing#dynamic-routes" rel="nofollow noreferrer">dynamic route</a> that looks like your entry URL format, except with <code>.txt</code> at the end (e.g. <code>news/{slug}.txt</code>), and point that route to a template called <code>text.html</code> or the like.</p>

<p>Inside the <code>text.html</code> template, you'll need to pull the entry using the <code>slug</code> token:</p>

<pre><code>{%- spaceless -%}

    {# Get the entry #}
    {% set entry = craft.entries.section('news').slug(slug).first() %}

    {% if not entry %}
        {% exit 404 %}
    {% endif %}

    {# Set some headers #}
    {% header "Content-Type: text/plain; charset=utf-8" %}
    {% header "Access-Control-Allow-Origin: *" %}

    {# Print the table #}
    {% for row in entry.tableField %}
        ...
    {% endfor %}

{%- endspaceless -%}
</code></pre>

<p>The idea is that with the above, you'll be able to just append <code>.txt</code> to your existing entry URLs to serve a text version of each entry – I have no idea if JW Player can be configured to use such a setup, but there it is.</p>

<p>Caveat: This solution will have a slightly larger overhead than simply serving an actual text file, since you'll be invoking Craft and hitting the database for each request.</p>

CC BY-SA 3.0
2016-11-28T07:51:46.380Z
2016-11-28T07:51:46.380Z
Mats Mikkel Rummelhoff
<p>You can write a <a href="https://pluginfactory.io/" rel="nofollow noreferrer">plugin</a> that listens to the <a href="https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry" rel="nofollow noreferrer">onSaveEntry</a> event.</p>

<p>When that event fires, you can grab any of the data you need for the text file from any of the custom fields and your plugin can write it out to the filesystem at whatever place it needs for JW Player to consume.</p>

CC BY-SA 3.0
2016-11-27T21:29:25.320Z
2016-11-27T21:29:25.320Z
Brad Bell
<p>I'm looking to create a <code>.txt</code> file with table field data taken from an entry on the fly. All entries would have their own designated files. It's for video chapters with JW Player. They ask for a <code>.vtt</code> file but with testing a <code>.txt</code> seems to work.</p>

<p>I've had a good search but can't seem to find much. I guess the worst case scenario is uploading them via FTP, but preferably not! Thanks.</p>

<files>
Creating an .txt file for every entry dynamically
# Creating an .txt file for every entry dynamically

(Asked by: username_0 on 2016-11-27)

I'm looking to create a `.txt` file with table field data taken from an entry on the fly. All entries would have their own designated files. It's for video chapters with JW Player. They ask for a `.vtt` file but with testing a `.txt` seems to work.

I've had a good search but can't seem to find much. I guess the worst case scenario is uploading them via FTP, but preferably not! Thanks.

**Tags:** files


---

## 2 answers

### Answer by username_1 on 2016-11-28. Score: 2 | Accepted answer

An alternative to using a plugin to write an actual text file to the local filesystem is to create a dynamic route that looks like your entry URL format, except with .txt at the end (e.g. news/{slug}.txt), and point that route to a template called text.html or the like.
Inside the text.html template, you'll need to pull the entry using the slug token:
{%- spaceless -%}

    {# Get the entry #}
    {% set entry = craft.entries.section('news').slug(slug).first() %}

    {% if not entry %}
        {% exit 404 %}
    {% endif %}

    {# Set some headers #}
    {% header "Content-Type: text/plain; charset=utf-8" %}
    {% header "Access-Control-Allow-Origin: *" %}

    {# Print the table #}
    {% for row in entry.tableField %}
        ...
    {% endfor %}

{%- endspaceless -%}

The idea is that with the above, you'll be able to just append .txt to your existing entry URLs to serve a text version of each entry – I have no idea if JW Player can be configured to use such a setup, but there it is.
Caveat: This solution will have a slightly larger overhead than simply serving an actual text file, since you'll be invoking Craft and hitting the database for each request.


---

### Answer by username_2 on 2016-11-27. Score: 3

You can write a [plugin](https://pluginfactory.io/) that listens to the [onSaveEntry](https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry) event.

When that event fires, you can grab any of the data you need for the text file from any of the custom fields and your plugin can write it out to the filesystem at whatever place it needs for JW Player to consume.


---

