Sendgrid-php: SendGrid templates how to parse array variable?

Created on 22 Feb 2016  Â·  7Comments  Â·  Source: sendgrid/sendgrid-php

I use SendGrid and I crate template and give in template variable 'name' like this:

why addSubstitution need array parameters and how this array parsing in template ?

$name = array('Test');
//
->addSubstitution(":name", $name)

befor I use tqig and send all template and in twig I have good algorithmization function like this

{% for skill in project.projectSkills %}
{{ skill }}
{% endfor %}

how do this in template sendGrid ? Or how work with array in template sendGrid ?

templte in sendGrid:

Hello :name,
Hot work with array :skill ?
<%body%>

With Best Regards,

Your Library Tester

Code

    $sendgrid = new \SendGrid($this->container->getParameter('sendgrid_key'));

    $email = new \SendGrid\Email();
    $name = array('testName');
    $skills = array('skill1', 'skill2', 'skill3') ;

    $email
    ->addTo('[email protected]')
    ->setFrom(''[email protected])
    ->setFromName('Test')
    ->setSubject('subject')
    ->setText('I\'m text!')
    ->setHtml('<strong>I\'m HTML!</strong>')
    ->addFilter('templates', 'enabled', 1)
    ->addFilter('templates', 'template_id', $template_id)
    ->addSubstitution(":skill", $skill)
    ->addSubstitution(":name", $name);


    $result = $sendgrid->send($email);
help wanted question

Most helpful comment

Are there any plans to include for loops in the future? If yes .. any ideas when?

All 7 comments

Hello @shubaivan,

Currently, we do not support arrays or control structures in our templates. You would need to loop through the "skills" array in your code and pass the final result into the template (using addSubstitution).

With Best Regards,

Elmer

@thinkingserious Can you please add an example? It's a very common use case where I want to send transactional email with order summary which has list of products.

@amitava82 You have to go through your list of products to html and then do the substitution with the full list.

@danjesus thank you for providing a solution!

Are there any plans to include for loops in the future? If yes .. any ideas when?

hello, it would be good, but I don't work in sendrid and I don't know when. But when I need loops variable, I prepare in local template, loops variable and set result in send grid variable, then add this variable in my requst. Then in sendgrid template put this variable.21.11.2016, 13:56, "Sorin" [email protected]:Are there any plans to include for loops in the future? If yes .. any ideas when?

—You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub, or mute the thread.

-- Best regards, Shuba I.V.

Hey all! Great news––

Dynamic content for Transactional Templates is here!

This morning, our team publicly launched dynamic content for transactional templates. It is now available for all customers sending over v3 of our Mail Send API. Iterate over lists, handle conditionals and more, thanks to native support for a subset of Handlebars syntax!

More information can be found in our blog post announcement.

Documentation:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

micahwalter picture micahwalter  Â·  3Comments

morazain picture morazain  Â·  3Comments

iamanupammaity picture iamanupammaity  Â·  4Comments

rsalunga29 picture rsalunga29  Â·  4Comments

mwsasser picture mwsasser  Â·  3Comments