Iterating through array in SendGrid email template - api

I'm trying to iterate through a collection and display information in a SendGrid template using Ruby on Rails.
recipient = SendGrid::Recipient.new("sergio#gmail.com")
recipient.add_substitution("username", user.github_id)
recipient.add_substitution("numbers", [1,2,3,4])
In gmail, this template arrives as:
sergiotapia
ARRAY(0x85b9d90)
The actual code for the template, copied from SendGrid's editor:
<html>
<head>
<title></title>
</head>
<body>
<div><%body%></div>
<div>username</div>
<div>numbers</div>
<p>This is a small example email.</p>
</body>
</html>
How can I iterate through a generic array or object in a SendGrid template? For this particular example, a user has many posts and I just want to show the title of the user's posts in a <li> element.
I'm just trying things out with a simple number array to see how it SendGrid works.

Update August 2018:
Sendgrid now offers iterators from the transactional email using handlebars, here's to the docs for more info:
https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#basic-iterator

Iterate example for the data:
{
"people":[{"name":"Bob"},{"name":"Sally"}]
}
Code:
{{#if people}}
<p>People:</p>
{{#each people}}
<p>{{this.name}}</p>
{{/each}}
{{/if}}
Result:
People:
Bob
Sally

{{#each data.products}}
{{name}}: {{price}} <br/>
{{/each}}
{"data":{"products": [{"name": "Tomato", "price": "5"}, {"name": "Banana", "price": "8"}]}}

Update
SendGrid now has support for dynamic templates!
You can read about it on their blog:
https://sendgrid.com/blog/how-to-use-sendgrids-dynamic-templates-for-your-transactional-emails/
Old answer:
Searching for this resulted the following GitHub issue. So it's not possible with SendGrid (yet?).
However there are other ways to do this. Using sendwithus you get access to a more powerful template editor that supports looping and iterating.
Simply set it up using your own SendGrid API key and you will be able to use the arrays in the sendwithus template which will send the mail using SendGrid.

Unfortunately the templates SendGrid provides are pretty minimal at this time. The templates don't support arrays as values and there are no conditional or looping controls, so you'd need to predetermine everything prior to building the template and template content. A more robust templating system is coming soon.

Here is a workaround Sendgrid is yet to update their template engine for this
Hello guys i need to do some iteration in my sendgrid mail and ran into this issue for now i had a temporal workaround which solved the problem. here is how i worked around it
created a txt file and loaded my html template there
Note areas i wanted to iterate on i replaced with a sendgrid variable e.g %list of items%
read the txt file into a string create a string builder and pass all iterated object into the %list of items% variable
then send the whole string content as message through sendgrid
public void sendSimpleMessage(String message,
String subject,
String toEmail,
String fromEmail){
Email from = new Email(fromEmail);
Email to = new Email(toEmail);
Content content = new Content("text/html", message);
Mail mail = new Mail(from, subject, to, content);
SendGrid sg = new SendGrid(sendgridApiKey);
Request request = new Request();
try {
request.method = Method.POST;
request.endpoint = "mail/send";
request.body = mail.build();
sg.api(request);
} catch (IOException ex) {
ex.printStackTrace();
}
}
hope it helps someone
https://github.com/sendgrid/sendgrid-nodejs/issues/221#issuecomment-361489007

"businessContributors" : [
{
"total" : {
"amount" : 11340,
"title" : "Dhama Ji Total"
},
"list" : {
"Dhama Ji Paid" : -296310,
"HDFC Account" : 0,
"Dhama Ji Received" : 307650
},
"title" : "Dhama Ji Owner Account"
},
{
"total" : {
"amount" : -1270,
"title" : "Rahul Total"
},
"list" : {
"Rahul Paid" : 243838,
"Rahul Received" : 242568
},
"title" : "Rahul Account"
},
]
in email template :-
<h4>Business Contributors </h4>
<ul>
{{#each businessContributors}}
<li> {{this.title}} <br>
{{#each this.list}}
{{#key}} = {{this}} <br>
{{/each}} </li>
<hr style="height: 2px; background-color: black;"><br>
<h2>{{this.total.title}}</h2><br>
<h2>{{this.total.amount}}</h2>
<br><br>
{{/each}}
</ul>

Related

Eleventy linking post author to author bio

I'm working on a site that essentially has posts, those posts have tags and also authors. there will only ever be one author per post.
I have the blogs working as intended as well as the tags, what I've hit a stumbling block with is relating an author key in my Frontmatter to their bio in contributors.json. Eventually this will be using Netlify CMS, so there will an "Add new author" form, then when an admin creates a new post, they can choose the author (some authors won't have access, they'll be guest contributors and just email the post over).
I can access both pages for my test authors and the correct posts are assigned, I just can't access the related data, when in the authors loop.
Anyway, my Frontmatter for authors.njk:
layout: base.njk
permalink: /authors/{{ author | slugify }}/
pagination:
data: collections.authorList
size: 1
alias: author
filter:
- all
- nav
- post
- posts
- tagList
eleventyComputed:
title: Posts written by {{ author }}
summary: "All posts written by “{{ author }}”"
Ideally, I'd need to access the data in the front matter, so I could use their actual name, as opposed to the key, in the page title etc.
A test post's Frontmatter:
title: "The fourteenth post"
summary: "A super useful post about something or other"
date: 2022-09-15
authors:
- jbloggs
tags:
- Android
- iOS
- MacOS
- Windows
In the above, my key is "jbloggs", I then have a global JSON file, like this:
[
{
"key": "jbloggs",
"name": "Joe Bloggs",
...
"img": {
"url": "/test.jpeg",
"alt": "Profile picture of Joe Bloggs"
}
},
...
]
In authors.njk, I have a card component which has a title, tags author etc and I'm correctly displaying all posts by jbloggs (I found I had to use set on a postAuthor variable and convert to JSON, as for some reason it was a JS object:
{% set postAuthor = null %}
{% for contributor in contributors %}
{% set postAuthor = contributor | toJson %}
{% if post.data.author == postAuthor.key %}
{%- set postAuthor = contributor[0] %}
{%- endif %}
{%- endfor %}
<h2>Showing {{ collections.authorList[author].length }} posts</h2>
<ul class="cards">
{%- for post in collections.authorList[author] | reverse -%}
<li class="card__item">
<article>
<h3 class="card__title">{{ post.data.title }}</h3>
...
<span class="card__author-name">
{{ postAuthor.name }}
...
</article>
</li>
{%- endfor %}
</ul>
Then finally, the .eleventy.js file creates a collection of authors, I tried using the same functionality as the tags, but it wouldn't display the posts and some Googling got me creating a collection in this way, which would be fine, if I didn't have related data.
function filterTagList(tags) {
return (tags || []).filter(tag => ["posts", "posts"].indexOf(tag) === -1);
}
eleventyConfig.addFilter("filterTagList", filterTagList)
eleventyConfig.addCollection("tagList", function(collection) {
let tagSet = new Set();
collection.getAll().forEach(item => {
(item.data.tags || []).forEach(tag => tagSet.add(tag));
});
return filterTagList([...tagSet].sort((a, b) => a.localeCompare(b, undefined, {sensitivity: 'base'})));
});
eleventyConfig.addCollection('authorList', collection => {
let authorSet = {};
collection.getAll().forEach(item => {
if (!item.data.authors) return;
item.data.authors.filter(
author => !['posts', 'all'].includes(author)
).forEach(
author => {
if (!authorSet[author]) { authorSet[author] = []; }
authorSet[author].push(item)
}
);
});
return authorSet;
});
What I think is happening, is the loop in the authors.njk file is converting my JSON to a js object, when it grabs the collection from config. As if I use {{ author.name }}, I get [object Object] hence why I ended up attempting to set to a new variable. I'm at that stage where I'm trying all sorts, to no avail, hence my ridiculous attempt with the new variable, so I know my I'm going about this in a complete noob type way.
What I need to do is:
Have access to the related JSON in the Frontmatter, so I can have a page title that is the persons full name, not the "key" I have set in Frontmatter
Query whether the Frontmatter key matches the JSON key, so I can access that data, to create an author bio at the top of the page and the authors/author urls are correctly generated on the post cards
But there is probably a much better way of doing this, that I am not seeing, so any help would be greatly appreciated.
Thank you
I managed to solve it, with a bit more Googling. I was over-complicating things with in my .eleventy.js file, but thanks to the chap who wrote this blog, I was able to refactor everything and got it working as I wanted.

Using For Loop in Liquid to build accordion FAQ

I am pretty new to Liquid and coding in general. I found a tutorial that helped me build a custom section for my Shopify page that allows me to add questions and answers to a FAQ page.
My goal is to have different categories of questions. Using the following code, when clicking on a question (tab) in an instance other than the original, it opens the tab in the original section rather than the particular section that I am editing.
<div class="grid-item-100">
<div class="faq">
{%for block in section.blocks%}
<div class="tab">
<input type="checkbox" id="tab{{forloop.index}}" class="tab-toggle">
<label for="tab{{forloop.index}}" class="tab-label">{{block.settings.question}}</label>
<div class="tab-content">{{block.settings.answer}}</div>
</div>
{%endfor%}
I know that this is because of the forloop being used for the tab id's, I just don't know how to rewrite this for it to work specifically on the instance of the section that I want.
Here is the rest of the schema as well:
{% schema %}
{
"name": "FAQ-SECTION-2",
"settings": [],
"blocks" : [
{
"type":"text",
"name":"Question/Answer",
"settings":[
{
"id":"question",
"type":"text",
"label":"the question"
},
{
"id":"answer",
"type":"richtext",
"label":"answer"
}
]
}
]
}
{% endschema %}
Here is an image to help visualize the issue:
I recommend following this tutorial: https://easycodeguide.com/build-customizable-faqs-page-using-section-shopify.html
It allows for separators within it and then has separate cases for either it is looping through a separator or Q/A.

json script with kotlinx.html

building html page using kotlinx.html
lots of content built without problems, but stumbling on adding json script tag with an id so the code on the client can retrieve the json data.
what to have the code something like..
<script id="jsondata" type="application/json">
{ "jsondata": "is here" }
</script>
using kotlinx.html i can have
script(type="application/json"){
+"""{ "jsondata": "is here"}"""
}
but unlike other tags, script does not seem to have an id property. Any ideas on how to get set the id for the script tag?
All you need is Tag.attributes (mutable map with tag attributes).
script(type = "application/json") {
attributes["id"] = "jsondata"
unsafe { +"""{ "jsondata": "is here" }""" }
}
will give you this result:
<script type="application/json" id="jsondata">{ "jsondata": "is here" }</script>

How to preserve \n on front-end when fetching data from firebase database?

I'm trying to fetch the data from Firebase Realtime database with \n preserved on front-end.
With css property: "white-space: pre-line", I'm able to send \n to firebase database, but when I'm trying to fetch the data on front-end using Vue.js it's not maintaining the \n.
Firebase database is an array so, I tried to convert the array to the string and it converts "\n" to the white space. I didn't find any online help how to preserve \n while displaying data on front-end
My firebase database:
answers
0: "some data"
1: "some data. more data.\nsomething"
I'm using for loop to display each answer to new line. But splits each key-value to new line not data with "\n".
Any help or guidance is appreciated.
You could replace the \n with <br/> and display your it as html.
let str = "some data. more data.\nsomething";
str = str.replace('\n', '<br/>');
// "some data. more data.<br/>something"
And then display it using the v-html directive.
<span v-html="str"></span>
This will display your content as html.
However : dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to XSS vulnerabilities. Only use HTML interpolation on trusted content and never on user-provided content.
See here :
https://v2.vuejs.org/v2/guide/syntax.html#Raw-HTML
Example :
Vue.config.devtools = false;
Vue.config.productionTip = false;
var app = new Vue({
el: '#app',
data: {
answers: ["some data", "some data. more data.\nsomething"]
},
computed: {
transformed_answers() {
return this.answers.map(a => a.replace('\n', '<br/>'));
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div v-for="answer in transformed_answers">
<p v-html="answer"></p>
</div>
</div>

Populate List based on json data in Worklight Application

i am developing a Worklight application using DOJO which shows list data based on webservice response.For getting webservice response i have created an adapter,i tested the adapter and its working fine.My problem is to show this json data on another view ,also in the new view i taught of showing this data in a list(list should populate based on json data array length) so that if i click on a list it will show that details.
How to do this .Any help is appreciated.
Code.
.js
function dispData()
{
var invocationData = {
adapter : 'getSampleAdapter',
procedure : 'sample'
};
WL.Client.invokeProcedure(invocationData,{
onSuccess : Success,
onFailure : Failure,
});
}
function Success(result)
{
var httpStatusCode = result.status;
if (200 == httpStatusCode) {
dijit.registry.byId("view0").performTransition("view1", 1, "slide");
}
}
html file
<div data-dojo-type="dojox.mobile.ScrollableView" id="view0"
data-dojo-props="selected:false,scrollDir:'v'">
<div data-dojo-type="dojox.mobile.Heading"
data-dojo-props="label:'Status'"></div>
<button data-dojo-type="dojox.mobile.Button" style="margin-left: 38%; margin-top: 30%"
data-dojo-props="label:'Data Details', onClick:function(e){dispData();}" ></button>
</div>
<div data-dojo-type="dojox.mobile.ScrollableView" id="view1"
data-dojo-props="selected:false,scrollDir:'v'">
<div data-dojo-type="dojox.mobile.Heading"
data-dojo-props="label:'Data'" back="Status" moveTo="view0"></div>
----------Json Data Should display here--------
</div>
this is a technical forum. You must try to do it before you post this.
However use dojox.mobile.EdgeToEdgeStoreList.Put a json response in a global variable such as
var mydata="[ ";
mydata+="{ 'label': 'bla bla', 'moveTo':'#' }, ";
mydata+="] ";
global_var= eval (mydata);
globalStoreList.generateList(global_var);
and build a list programmatly(example)