I have a notices section on my website which are stored in my Mongo Database. If the collection is empty, how would i display a message to say something like "No new notices"
I am looping this on my page using EJS my code is below.
<% for (let notice of notices) {%>
<p class="lead"><%= notice.noticeText %>
<% } %>
I guess you can put a condition like so:
<% if (!notices.length) {%>
<p>desired text if no notice exists</p>
<% } else { %>
<% for (let notice of notices) {%>
<p class="lead"><%= notice.noticeText %>
<% } %>
<% } %}
Related
I'm using Skeleton for my 16 columns grid system on Silverstripe.
My code below:
<% loop $AllChildren %>
<div class="three columns"><h5>$Title</h5></div>
<% end_loop %>
produces:
Children1 Children2 Children3 Children4 Children5
Children6 Children7 Children8 Children9 Children10
My question is how do I put a space between the rows so the output will
be like:
Children1 Children2 Children3 Children4 Children5
Children6 Children7 Children8 Children9 Children10
In SS3 you can use $MultipleOf() (api docs) in your templates to output something every nth item, e.g.
<% loop $AllChildren %>
<div class="three columns"><h5>$Title</h5></div>
<% if $MultipleOf(5) %>
<hr class="extra-space">
<% end_if %>
<% end_loop %>
In Docpad, the following code (using a Query-Engine helper and eco) pulls a list of file names from a directory tree and addds their url to an array:
<% images = []; %>
<% for file in #getFilesAtPath({relativeOutDirPath: 'images/'}).toJSON() : %>
<% images.push(file.url) %>
<% end %>
How might I limit the query to a subset of files, say only PNGs?
So like stated in my answer to your other question: What methods can be called on Docpad's Query tools?
Object returned by your query has some additional default metadata you can't see. As you can see here http://docpad.org/docs/meta-data, one of the metadata is "extension". So you can query with condition like:
extension:'png'
So your code might look like (notice findAll part that gives you a possibility to set search condidtions):
<% images = []; %>
<% for file in #getFilesAtPath({relativeOutDirPath: 'images/'}).findAll(extension:'png').toJSON() : %>
<% images.push(file.url) %>
<% end %>
Or if you want to return all files and trigger different actions on different extensions you could:
<% images = []; %>
<% for file in #getFilesAtPath(relativeOutDirPath: 'images/').toJSON() : %>
<% if file.extension is 'png' : %>
<% images.push(file.url) %>
<% end %>
<% end %>
I'd like to find out if the current user is friends with the root node user, using Koala, and if so, print a statement saying: you are friends.
My problem is the parsing of data returned from facebook.
user.rb
def facebook
#facebook ||= Koala::Facebook::API.new(oauth_token)
block_given? ? yield(#facebook) : #facebook
rescue Koala::Facebook::APIError => e
logger.info e.to_s
nil # or consider a custom null object
end
def facebook_friend
facebook.get_connection("me", "friends/#user.user_id")
end
Profile.html.erb
<% if current_user.facebook_friend.include?(#user.uid)? %>
<p> you are friends</p>
<% else %>
<p> you are not friends</p>
<% end %>
Output:
<%= current_user.facebook_friend %>
will return: [{"name"=>"Elizabeth", "id"=>"100008217009369"}]
<%= #user.uid %>
will return: 100008217009369
Thanks!!!
It was much easier than I thought:
In the user model simple used the Koala method:
def facebook_friend(user)
facebook.get_connection("me", "friends/#{user.uid}")
end
def my_friends_list
facebook.get_connection("me", "friends")
end
Then in the Profile View, adding current_user and #user as arguments:
<% if #user.uid.present? %>
<% if signed_in? && current_user == #user %>
<p><%= current_user.my_friends_list.count %> friends</p>
<% elsif current_user.facebook_friend(#user).empty? %>
<p><%= current_user.mutual_friends.count %> friends in common</p>
<% else %>
<p>You are Facebook friends</p>
<% end %>
<% else %>
<p>Not connected</p>
<% end %>
I'm working on a simple Dancer app to log books a person has read, but in my template to show how many books a person has read, I'm stumbling into an error. I'm trying to go through ever row in the table of reading instances and add 1 to a counter if the reader is the same as the listed person.
Here's the code for the template:
<ul class="people">
<% IF people.size %>
<% FOREACH id IN people.keys.nsort %>
<li><h2 style="display: inline;"><% people.$id.name %></h2><br />
Born <% people.$id.birthday %><br />
<% FOREACH reader IN readings.keys.nsort %>
<% count = 0 %>
<% IF readings.$reader.person_id == people.$id.id %>
<% count = count + 1 %>
<% END %>
<% END %>
<% count %>
<% END %>
<% ELSE %>
<li><em>Unbelievable. No people here so far</em>
<% END %>
</ul>
However, when I display it, count is only 1. Does anybody know what I'm doing wrong, or do you need more code?
Thanks.
Looks like you need to pull the count initialization out of the FOREACH reader loop:
<% FOREACH id IN people.keys.nsort %>
<li><h2 style="display: inline;"><% people.$id.name %></h2><br />
Born <% people.$id.birthday %><br />
<% count = 0 %>
<% FOREACH reader IN readings.keys.nsort %>
<% IF readings.$reader.person_id == people.$id.id %>
<% count = count + 1 %>
<% END %>
<% END %>
<% count %>
<% END %>
I'm currently learning MVC2, and I have seen three variants of the tags that contain the actual code for a View:
<% ... %>
With a = after the %
<%= ... %>
and with a : after the %
<%: ... %>
What is the difference of these three code containers?
And are there any other variants of these?
<% ... %>
is just a block of code
<%: "blah blah" %>
Is Shorthand for
<%= Html.Encode("blah blah") %>
Which is shorthand for
<% Response.Write(Html.Encode("blah blah")) %>
1.<% ... %> just block of code
<%if (Model.HelloWorld != null){%>
Hello World!!
<%} %>
2.<%= ... %> plain text without escaping
<%=Model.HelloWorld %>
3.<%: ... %> text with escaping equal <%= Server.HtmlEncode(Model.Something) %> Details here
<%:Model.HelloWorld %>