getgrav / grav-plugin-email

Grav Email Plugin
http://getgrav.org
MIT License
37 stars 29 forks source link

Sending variables or data from Grav template #49

Closed moreplavec closed 1 year ago

moreplavec commented 7 years ago

Nice plugin, but is it possible to send some variables from Grav template? For example, i have simple product request form, but i need to add page path from which page was form sent or some product information, like product name. All data are in the template, but {{ variable }} is not working if used in page header of the MD file.

rhukster commented 7 years ago

Can you give a more concrete example i can test/investigate with?

moreplavec commented 7 years ago

For example i have this form:

    product-form-en:
        fields:
            -
                name: name
                label: Name
                classes: form-control
                placeholder: 'Your name'
                autofocus: 'on'
                autocomplete: 'on'
                type: text
                validate: { required: true }
            -
                name: email
                label: Email
                classes: form-control
                placeholder: 'Your email'
                type: email
                validate: { required: true }
            -
                name: phone
                label: Phone
                classes: form-control
                placeholder: 'Your phone'
                type: text
            -
                name: message
                label: Message
                classes: form-control
                placeholder: 'Your message or tea you are interested in...'
                type: textarea
                validate: { required: true }
        buttons:
            -
                type: submit
                classes: 'btn btn-primary'
                id: odeslat-formular
                value: Send
        process:
            -
                email: { subject: '[Site Contact Form] {{ form.value.name|e }}', body: '{% include ''forms/data.html.twig'' %}' }
            -
                save: { fileprefix: contact-, dateformat: Ymd-His-u, extension: txt, body: '{% include ''forms/data.txt.twig'' %}' }
            -
                message: 'Thank you. We will contact you shortly.'

But i need insert, for example, field: sended from page {{ page.path }} or {{ header.product.name }} to know from which product page it was sended.

flaviocopes commented 7 years ago

You can add a hidden field that has the option to evaluate the Twig.

So,

- 
  name: referral
  type: hidden
  evaluate: true
  default: 'page.url' 

will generate

<input data-grav-field="hidden" type="hidden" class="input" name="data[referral]" value="/grav-develop/it/form" />

flaviocopes commented 7 years ago

(and will be available in the email body just like any other field for your use)

moreplavec commented 7 years ago

Thanks for help, i tried, but i get only this in code:

<div class="form-group">

<input data-grav-field="hidden" data-grav-disabled="false" type="hidden" class="input" name="data[referral]" value="">
</div>

Don't know where is the problem, but when i save form in header, instead of default: 'page.url', it's presented as default: page.url when i load it again. I tried to save it via MD editor and the problem stays.

Saved version of email looks like:

name: lkjsdflkj
email: fasdlkfjasd@asdlfkjalsdf.com
phone: asdfasdfasfd
message: asdfasdf
referral: 
Sogl commented 6 years ago

Works fine for me with:

    name:
        label: Name
        type: hidden
        evaluate: true
        default: 'page.title'

    link:
        label: Url
        type: hidden
        evaluate: true
        default: "base_url_absolute ~ page.route"