Skip to content
Grav 2.0 is officially stable. Read the announcement →
Support

Submit form data by email. How to make new lines?

form solved

Solved by p View solution

Started by p 3 years ago · 1 replies · 417 views
3 years ago

I'm writing form data into a file and an email by using the following twig:

TWIG
{%- macro render_field(form, fields, scope) %}
{%- import _self as self %}
{%- for index, field in fields %}
    {%- set show_field = attribute(field, "input@") ?? field.store ?? true %}
    {%- if field.fields %}
        {%- if field.type == "section" %}
            {{- "\r\n" ~ field.title|replace({' *': ''}) ~ "\r\n" ~}}
        {%- endif %}
        {%- set new_scope = field.nest_id ? scope ~ field.name ~ '.' : scope %}
        {{- self.render_field(form, field.fields, new_scope) }}
    {%- else %}
        {%- if show_field %}
            {%- set value = form.value(scope ~ (field.name ?? index)) %}
            {%- if value %}
                {%- if field.label %}{{- field.label|t|e }}: {% endif %}
                {{- string(value is iterable ? value|json_encode : value) ~ "\r\n" }}
            {%- endif %}
        {%- endif %}
    {%- endif %}
{%- endfor %}
{%- endmacro %}

{%- import _self as macro %}
{%- autoescape false %}
{{- macro.render_field(form, form.fields, '') }}
{%- endautoescape %}

It works fine for the file, but in the email there are no new lines, but only spaghetti text. I already tried adding <br> and <br /> but that didn't work. It only led to <br> and <br /> in the email and &lt; / &gt; in the file.

What am I doing wrong?

👍 1
3 years ago Solution

Solved by using abc.html.twig instead of abc.txt.twig.

👍 1

Suggested topics

Topic Participants Replies Views Activity
Support · by BenLaKnet, 5 days ago
1 122 2 days ago
Support · by BenLaKnet, 1 week ago
5 195 2 days ago
Support · by kskt, 2 days ago
0 44 2 days ago
Support · by David Meissner, 3 days ago
0 43 3 days ago
Support · by David Meissner, 4 days ago
3 92 3 days ago