<pre><code>{% set text %}
    Here is some
    Text that spans
    multiple lines
{% endset %}

{% set textArray = text | split('\n') | filter %}

{% for line in textArray %}
    &lt;span&gt;{{ line }}&lt;/span&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-08-22T18:23:32.420Z
Brilliant. Exactlay what I needed! Cheers, Trevor. 
Jay
CC BY-SA 4.0
2018-08-22T17:18:50.867Z
2018-08-22T17:18:50.867Z
Trevor Davis
<p>I've made a little macro for this:</p>

<pre><code>{% macro splitText(text) %}
  {% set text = text|nl2br|raw %}
  {% set text = '&lt;span class="split_text"&gt;' ~ text|replace('&lt;br /&gt;', '&lt;/span&gt; &lt;span class="split_text"&gt;') ~ '&lt;/span&gt;' %}
  {{ text|raw }}
{% endmacro %}
</code></pre>

<p>You may need to tweak it a bit, but this is what worked for me.</p>

CC BY-SA 4.0
2018-08-22T18:23:12.353Z
Perfectly good answer. Ended up going with Trevor's below because I liked the control of the forloop. Gave me access to 'loop.index' in there, too. Thanks though, Will!
Jay
CC BY-SA 4.0
2018-08-22T19:27:18.053Z
Makes sense to me. I like that solution better, too :)
Will Browar
CC BY-SA 4.0
2018-08-22T16:48:40.930Z
2018-08-22T16:48:40.930Z
Will Browar
<p>Building on this question here: <a href="https://craftcms.stackexchange.com/questions/2830/create-line-breaks-from-text-field/2834">Create line breaks from text field</a></p>

<p>What approach would be best for breaking a multi line plain text field into multiple single lines to be used as:</p>

<pre><code>&lt;span&gt;{{ textfield.lineOne }}&lt;/span&gt;
&lt;span&gt;{{ textfield.lineTwo }}&lt;/span&gt;
&lt;span&gt;{{ textfield.lineThree }}&lt;/span&gt;
</code></pre>

<p>At the moment, my approach is to use a Matrix for the user to be able to add individual lines, but I'd much rather I was able to present the client with a multiline text field and rework them in the template.</p>

<templating><plaintext>
How to create single lines from a plain text field with line breaks
# How to create single lines from a plain text field with line breaks

(Asked by: username_0 on 2018-08-22)

Building on this question here: Create line breaks from text field
What approach would be best for breaking a multi line plain text field into multiple single lines to be used as:
{{ textfield.lineOne }}
{{ textfield.lineTwo }}
{{ textfield.lineThree }}

At the moment, my approach is to use a Matrix for the user to be able to add individual lines, but I'd much rather I was able to present the client with a multiline text field and rework them in the template.

**Tags:** templating, plaintext


---

## 2 answers

### Answer by username_1 on 2018-08-22. Score: 3 | Accepted answer

{% set text %}
    Here is some
    Text that spans
    multiple lines
{% endset %}

{% set textArray = text | split('\n') | filter %}

{% for line in textArray %}
    {{ line }}
{% endfor %}

> **Comments:**
> **username_0**: Brilliant. Exactlay what I needed! Cheers, Trevor.   

---

### Answer by username_2 on 2018-08-22. Score: 2

I've made a little macro for this:
{% macro splitText(text) %}
  {% set text = text|nl2br|raw %}
  {% set text = '' ~ text|replace('', ' ') ~ '' %}
  {{ text|raw }}
{% endmacro %}

You may need to tweak it a bit, but this is what worked for me.

> **Comments:**
> **username_0**: Perfectly good answer. Ended up going with Trevor's below because I liked the control of the forloop. Gave me access to 'loop.index' in there, too. Thanks though, Will!  
> **username_2**: Makes sense to me. I like that solution better, too :)  

---

