Finding Current Page in SailsJS - node.js

Does anyone know of a prettier way to output a class name to a list element (in this case) using EJS?
<% var active = "active"; %>
<div class="header">
<ul class="nav nav-pills pull-right">
<li class="<% if(req.path == '/user') { %><%= active %><% } %>">Users</li>
<li class="<% if(req.path == '/about') { %><%= active %><% } %>">About</li>
<li class="<% if(req.path == '/contact') { %><%= active %><% } %>">Contact</li>
</ul>
<h3 class="text-muted">Sails Tutorial App</h3>
</div></li>

You can do it like this :)
<li class="<%= (req.path == '/user') ? 'active' : '' %>">Users</li>
It's a little bit prettier I guess...

Related

Conditionals(else if) in ejs file throwing a syntax error

I am unable to use the else if condition in my ejs file.
<% if(locals.user && (locals.user.id == post.user.id)){ %>
<small>
<a href="/comments/destroy/?comment_id=<%= comment.id %>&post_id=<%= post.user.id %>">
<i class="fa fa-trash"></i>
</a>
</small>
<% } else if(locals.user && (locals.user.id == post.user.id)){ %>
<small>
<a href="/comments/destroy/<%= comment.id %>">
<i class="fa fa-trash"></i>
</a>
</small>
<% } %>
It throws a syntax error for some reason. If I remove else from the code it works. How can I fix this?

Cannot read property 'firstname' of undefined in view engine node js

I am trying to display json data on a website using view engine (ejs) but I am facing following error Cannot read property 'firstname' of undefined
this is my program
node.js
io.readEmp().then(function(data){
res.render('Dashboard',{data:data});
}).catch(function(err){console.log(err.message);});
index.ejs
<ul class="list-group list-group-flush">
<% for(var key in data){%>
<li class="list-group-item"><%= data.key.firstname %></li><%}%>
</ul>
jsonfile
{
"id01":{"firstname":"abc","lastname":"xy"},
"id02":{"firstname":"pqr","lastname":"xy"}
}
error
Cannot read property 'firstname' of undefined
If you have data as an object like you display then 'key' will be the item key and you need to use the key access notation for an object thusly:
<ul class="list-group list-group-flush">
<% for(var key in data){%>
<li class="list-group-item"><%= data[key].firstname %></li><%}%>
</ul>
'key' for the first item will be "id01", so this is equivalent (on the first pass) for saying data.id01.firstname.
Like I was trying to say earlier. Turning an Object into an array is better. Because if you are using a hashMap structure these can be deeply nested. Example being an object like the below
{"id01":{
"address": {
"street": "123 Main St",
"city": "San Francisco",
"state": "CA"
},
"dogs": {
"spot": {
"type": "German Shepherd",
"foods": {
"soft" : {
"Purina": "Cat Chow"
},
"unconventional": {
"vomit": "Vomit",
"squirrels" : "Squirrels"
}
}
},
"spike": {
"type": "Doberman"
}
}
}}
will result in something looking like this using the method suggested.
<ul class="list-group list-group-flush">
<% for(var key in data){%>
<li class="list-group-item"><%= data[key].firstname %></li>
<li class="list-group-item"><%= data[key].address.street %></li>
<li class="list-group-item"><%= data[key].address.city %></li>
<li class="list-group-item"><%= data[key].address.state %></li>
<li>
<ul>
<% for(var dogKey in data[key].dogs){%>
<li class="list-group-item"><%= data[key].dogs[dogKey].type %></li>
<li>
<ul>
<% for(var dogFoodKey in data[key].dogs[dogKey].foods){%>
<li class="list-group-item"><%= data[key].dogs[dogKey].foods[dogFoodKey]. %></li>
<%}%>
</ul>
</li>
<%}%>
</ul>
</li>
<%}%>
</ul>
It is terribly untidy. It needs a serious refactoring. As a practice you should not repeat yourself.
Instead, I would suggest the following which re-writes the object into an array as it receives it.
<ul class="list-group list-group-flush">
<% for(var person in Object.values(data)){%>
<li class="list-group-item"><%= person.firstname %></li>
<li class="list-group-item"><%= person.address.street %></li>
<li class="list-group-item"><%= person.address.city %></li>
<li class="list-group-item"><%= person.address.state %></li>
<li>
<ul>
<% for(var dog in person.dogs){%>
<li class="list-group-item"><%= dog.type %></li>
<li>
<ul>
<% for(var food of Object.values(dog.foods)){%>
<li class="list-group-item"><%= food %></li>
<%}%>
</ul>
</li>
<%}%>
</ul>
</li>
<%}%>
</ul>
you need to make few changes as below in your index.ejs file
<ul class="list-group list-group-flush">
<% for(var key in data){%>
<li class="list-group-item">
<%= key.firstname %>
</li>
<%}%>
</ul>
Note: you have to use key.firstname and not data.key.firstname

How to concatenate HREF to get id from database?

NodeJS, Mongoose
<% for(var i = 0; i < articles.length; i++){ %>
<ul class="list-group">
<li class="list-group-item">
<a href="/article/<%=articles._id%>">
<%= articles[i].title %>
</a>
</li>
</ul>
<% } %>
<% include partials/html-footer %>
How do I make it go to /articles/articles._id? I've tried -
<a href="/article/"+<%=articles._id%>>
as well. If there's anything else you need to see, let me know. Don't know what else to post.
You are missing the array index:
<a href="/article/<%= articles[i]._id %>">

Nodejs ejs if else condition

Here i was trying for image to load, If image comes then it should show else default image. I have tried but no result
<% getdata.forEach((user) => { %>
<div class="col-md-4 col-sm-4">
<div class="service-box">
<% if ( typeof user.image == 'undefined' ) { %>
<img src="images/logo.png" class="" width='260' height='220'>
<% }else { %>
<img src="<%= user['image'] %>" class="">
<% } %>
</div>
</div>
<% }) %>
user.image may also be null or an empty string, in which case it doesn't pass your test for undefined.
Try this:
<% if (! user.image) { %>

How to create a pagination system in Hexo

I love using Hexo.. :)
I've setup custom page. Is it possible to show all post in my page as paginated?
Using site.posts got me all post without pagination.
What should I do to get all post as paginated from page?
Thanks.
In the main configuration file _config.yml, there is a per_page variable to allow you to choose how many post you want ot display per page.
Create a template, index.ejs for example :
<% page.posts.each(function(post) { %>
<article>
// do what you have to do to display each post
</article>
<% }) %>
<%- partial('pagination', {type: 'page'}) %>
As you can see, We use the page variable to get 7 posts.
After that, create an other template pagination.ejs, that allow you to navigate through the page :
<div class="pagination-bar">
<ul class="pagination">
<% if (page.prev) { %>
<li class="pagination-prev">
<% if (page.prev === 1) { %>
<a class="btn btn--default btn--small" href="<%- url_for(' ') %>">
<% } else { %>
<a class="btn btn--default btn--small" href="<%- url_for(page.prev_link) %>">
<% } %>
<i class="fa fa-angle-left text-base icon-mr"></i>
<span><%= __('pagination.newer_posts') %></span>
</a>
</li>
<% } %>
<% if (page.next) { %>
<li class="pagination-next">
<a class="btn btn--default btn--small" href="<%- url_for(page.next_link) %>">
<span><%= __('pagination.older_posts') %></span>
<i class="fa fa-angle-right text-base icon-ml"></i>
</a>
</li>
<% } %>
<li class="pagination-number"><%= _p('pagination.page', page.current) + ' ' + _p('pagination.of', page.total) %></li>
</ul>
</div>
I wrote a Hexo theme : Tranquilpeak, I recommend you to check the source code to understand how I built it and of course read the official Hexo documentation

Resources