How to render data in groups to ejs - node.js

I want to display data that has been grouped into the EJS?
I have the following data:
"results": {
"Angular CLI": [
{
"category_name": "Angular CLI",
"article_title": "Mengenal Framework Angular CLI"
}
],
"Tips dan Trik": [
{
"category_name": "Tips dan Trik",
"article_title": "Tutorial : Cara Membuat Repositori Github Private"
},
{
"category_name": "Tips dan Trik",
"article_title": "Mengenal Fitur Media Query pada CSS"
}
]
}
Following on the EJS file:
<% results.forEach(function(data) { %>
<div>
<p><%= data %></p>
</div>
<% }); %>
But it does not display data at all.
EDIT :
Dear all, thank you for giving some explanation. So that I can find a solution to this problem.
<% for (let category in results) { %>
<b><%= category %></b>
<% results[category].forEach((data, key) => { %>
<p><%= data.article_title %></p>
<% }) %>
<% } %>

Provided your object is something like this below
var obj = {"results": {
"Angular CLI": [
{
"category_name": "Angular CLI",
"article_title": "Mengenal Framework Angular CLI"
}
],
"Tips dan Trik": [
{
"category_name": "Tips dan Trik",
"article_title": "Tutorial : Cara Membuat Repositori Github Private"
},
{
"category_name": "Tips dan Trik",
"article_title": "Mengenal Fitur Media Query pada CSS"
}
]
}
As your obj.results is object and not array, you could iterate through all properties of obj.results object and display values like this
<% for (var prop in obj.results) { %>
<div>
<p><%= prop%> : <%= results[prop] %></p>
</div>
<% }); %>
Let me know if I misunderstood your issue

Related

how can I loop through this referenced data (tagged episode) in ejs

I basically want to display the episodes nested in tvSeason which is also nested in seriesMovie
<% seriesMovie.tvSeasons.forEach(function(tvSeason) { %>
<h1>
<%= tvSeason["episodes"].episode_number %>
</h1>
<% }) %>
"tvSeasons" : [
{
"season_id" : "4321",
"episode_count" : "22",
"season_number" : "1",
"overview" : "nothing",
"image_path" : "grey.jpg",
"release_date" : "2005-12-24",
"episodes" : [
{
"episode_id" : "4553",
"episode_number" : "1",
"episode_title" : "rage",
"image_path" : "rey.jpg",
"video_path" : "rom.mp4",
"episode_runtime" : "42"
}
]
]
episode_number is not defined
Why doesn't it work ?
It seems that episodes is an array... So if you want to access the first episode, just indicate the episode[0] ...
<% seriesMovie.tvSeasons.forEach(function(tvSeason) { %>
<h1>
<%= tvSeason["episodes"][0].episode_number %>
</h1>
<% }) %>
Because "episodes" is a list holding objects. You should first access the element you want in that list then call episode number upon that. Somethin like this
tvSeason["episodes"][0].episode_number

Access object.param in parent context handlebars.js

Not able to get parent object's param value
{{#each myObject.details}}
<span class="marginL15 pull-left hidden-xs">
<a href="
/rates
/{{../port_data.name}}
/{{../port_data.name}}
/{{name}}
/{{port_code}}
">
{{name}}
</a>
</span>
{{/each}}
My object is of format
{
"meta_title": "fw",
"port_data": {
"display_name": "Rajkot (INRAJ), Rajkot, India",
"name": "Rajkot",
"port_code": "INRAJ"
},
"details": [
{
"_id": {
"$oid": "58f04ef3c0a35f10b7cc08fa"
},
"display_name": "Sokhna(Al Sokhna) (EGSOK), Suez, Egypt",
"name": "Sokhna(Al Sokhna)",
"port_code": "EGSOK"
},
{
"_id": {
"$oid": "58ff42cfc0a35f493be68031"
},
"display_name": "Rouyn Noranda Airport - CAYUY (YUY), Canada, usa",
"name": "Rouyn Noranda Airport - CAYUY",
"port_code": "YUY"
},
{
"_id": {
"$oid": "58f1c6e9c0a35f2d16dd4f44"
},
"display_name": "Sharjah (AESHJ), Sharjah, United Arab Emirates",
"name": "Sharjah",
"port_code": "AESHJ"
}
]
}
Also tried {{#root.port_data.name}} but to no use.
When passing myObject.details to the {{#each}} helper, you pass only the referenced array stored in the details field of myObject variable - there is no way to look up to something stored in ../, as there's nothing there.
What you could do:
{{#myObject}}
{{#each details}}
<span class="marginL15 pull-left hidden-xs">
<a href="/rates
/{{../port_data.name}}
/{{name}}
/{{port_code}}
">
{{name}}
</a>
</span>
{{/each}}
{{/myObject}}
It gives you an access to the whole myObject variable one scope above the {{#each}} loop.

ejs forEach function not working for json data in node

forEach is not a function when I am iterating json file in ejs using nodejs giving some error while iterating.. below is three section what i have done in .ejs file, json and nodeJS
Here is my JSON Data
<pre>
{
"MenuName": "Main Menu Here",
"widget": {
"window": {
"data": "Click Here",
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"window": {
"data": "Click Here",
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"window": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
}
</pre>
This is my ejs
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1> My Name Is <%= title %></h1>
<h1> My Name Is <%= printIt.MenuName %></h1>
<ul>
<% printIt.widget.forEach(function(item){%>
<li><%= item.window.data%></li>
<% }); %>
</ul>
</body>
</html>
</pre>
in NodeJS
<pre>
app.locals.printIt = require("./sample.json");
</pre>
I can able to print
My Name Is <%= title %> and My Name Is <%= printIt.MenuName %>
What i missed here..?
First of all, your JSON data will keep only single record of widget window and which is last one. Because window key will overwrite previous hold value.
Update your script with this one
<ul>
<% for(var item in printIt.widget){%>
<li><%= printIt.widget[item].data%></li>
<% } %>
</ul>
Hope this will help to solve your query !!

filter.commentAfter by Emmet in Sublime Text 3 not work

In Emmet by ST3, I tried to customize a little comment after giving tab when creating a label, for example #div or .span
I read that this was a solution in the settings of Emmet (Emmet.sublime-settings):
"preferences": {
"filter.commentAfter" : "<!-- <%= attr('id', '#') %> <%= attr('class', '.') %> -->",
"filter.commentBefore" : "<!-- <%= attr('id', '#') %> <%= attr('class', '.') %> -->"
}
But not work. Please Help
In your User/Emmet.sublime-settings file make sure you have your structure like this
{
"preferences": {
"filter.commentAfter" : "<!-- <%= attr('id', '#') %> <%= attr('class', '.') %> -->",
"filter.commentBefore" : "<!-- <%= attr('id', '#') %> <%= attr('class', '.') %> -->"
}
}
and it should work.
Make sure you dont forget about the |c at the end
div#test.myclass|c
As an extra if you want to automatically add the c filter you should set your syntaxProfiles to something like this
{
"preferences": {
},
"syntaxProfiles": {
"html" : {
// auto add the comments
"filters": "html,c"
}
}
}
make sure it's on the same level as preferences

Pulling Tweets into a Spotify app

I'm building a little Spotify app that displays tweets from the current Artist playing. I'm using Bocoup's jQuery Twitter plugin to grab and display the tweets: http://code.bocoup.com/jquery-twitter-plugin/
MANIFEST.JSON
{
"BundleType": "Application",
"AppIcon": {
"18x18": "tutorial.png"
},
"AppName": {
"en": "News"
},
"SupportedLanguages": [
"en"
],
"RequiredPermissions": [
"http://twitter.com"
"http://*.twitter.com"
"http://*.twimg.com"
],
"VendorIdentifier": "com.News",
"RequiredInterface": "1",
"BundleVersion": "0.2",
"BundleIdentifier": "News",
"AppName": "News",
"AppDescription": "Twitter updates from the Artist playing."
}
From the Index file:
<script type="text/javascript">
$(document).ready(function() {
$('#tweets').twitter({from: 'mediatemple', replies: false})
launch();
});
</script>
</head>
<body>
<div id="info">
</div>
<div id="news">
</div>
<div id="tweets">
</div>
</body>
Nothing displays in Spotify when I open my app, but if I open the index.html file in a browser, the tweets appear. What am I missing?
The RequiredPermissions JSON needs commas.
try:
"RequiredPermissions": [
"http://twitter.com",
"http://*.twitter.com",
"http://*.twimg.com"
],
Things fail silently if there is a problem in the manifest. Always use a JSON linter (http://jsonlint.com/) to verify at least the format is proper JSON.
UPDATE: After any changes to manifest.json you must restart Spotify.

Resources