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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

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 · 339 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 Thomas, 1 week ago
2 54 12 hours ago
Support · by Anna, 3 days ago
2 60 15 hours ago
Support · by Justin Young, 16 hours ago
1 30 15 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 57 5 days ago