EJS extract value then compare it to a string that is not undefined - ejs

Is this possible in EJS?
Extract the value inside the local and do a comparison to that value and not just comparing if it's undefined.
eg.
<% if ( <%=person.name %> == 'john'){ %>
do something here
<% } else { %>
do something here
<% } %>

Related

display a message when mongoDB collection is empty

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 %>
<% } %>
<% } %}

SIlverstripe <%loop AllChildren %> with Skeleton

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 %>

Filtering #getFilesAtPath results in Docpad

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 %>

Simple foreach loop in MVC

<% foreach (var car in Model.AvailableCars)
{ %>
<label><%car.Text; %></label>
<% } %>
The above code throws the error
Only assignment, call, increment, decrement, and new object expressions can be used as a statement.
I know I can do it with html helpers, but why won't the above code work?
<label><%car.Text; %></label>
should read
<label><%= car.Text; %></label>
^
or you can use
<label><%: car.Text; %></label>
^
which will automatically HTML.Encode the value for you.
Add a colon to the car.Text tag to write it to the document, such as:
<label><%: car.Text %></label>
Here's a good explanation of <%: versus <%= asp.net mvc tags: <%: %> vs. <%= %>

How to assign ids for textboxes added using loop in asp.net MVC

I am arranging a dynamic table of textboxes using a loop.
Please review the following code segment in view -
<% foreach (var item in Model.Names) { %>
<% =Html.TextBoxFor(item.Name)%>
<% } %>
The problem here is that i want their unique ids to be generated so that i could access each textbox using JS.
Any help is highly appreciated.
Regards
Irfan
Do this instead:
<div class="editor-field">
<% for (int i = 0; i < Model.Names.Count; i++) { %>
<%: Html.TextBoxFor(m => m.Names[i].Name) %>
<% } %>
</div>