ejs file. Creating a card to show articles "%> <%" - mongodb

I am following a youtube tutorial on creating a blog using node, MongoDB, and express.
I am having difficulty understanding some stuff.
<!--Creating the Article Cards-->
<% article.forEach(article =>{ %>
<div class="card mt-4"><%= article.title%></div>
<% }) %>
here is the code
%>
<div class="card mt-4"><%= article.title%></div>
<%
the area I don't understand is why there is and what " %> <% " mean as you can see above after doing the forEach.

Well, that is a scriplet tag that is used for the control-flow you need to use in the if-else as well. Here is more for the ejs ejs

Related

how to include a template with parameters in EJS?

As a real beginner in EJS, I have two charts in my html page, so I want to use my partial twice:
<% include partials/spider-chart.ejs %>
But I need to pass some parameters inside the ejs to differentiate between graphs.
What is the best way?
#Naeem Shaikh solution works.
Though include also gives you more intuitive way of including a partial template and also passing context variables to that as found in documention section of ejs.
<ul>
<% users.forEach(function(user){ %>
<%- include('user/show', {user: user}); %>
<% }); %>
</ul>
I think you want to render two different charts using same partial ejs template, just by providing different data(within the main ejs file).
You can just define a variable, which will be assigned to the data, which the first chart will use, than include the chart.ejs file, again change the data, and include the partial ejs file(chart.ejs) again, so now you have two files which can use same variable(data), but can plot different chart based on value assigned to data.
For Example:
<% var data= 'data to be used by first chart(parameter)'; %>
<% include partials/spider-chart.ejs %>
// re-initializing data for second chart
<% data= 'data to be used by second chart(parameter)'; %>
<% include partials/spider-chart.ejs %>
where your spider-chart.ejs file could be something which will use data
spider-chart.ejs
<li>
<%= data %> // just an example
</li>
here, as you use data, the data variable accessed by both charts will be different because you are reassigning values for data before every chart.
You can pass single as well as multiple data here is how to do it
In render funtions
We can pass multiple data as an object like this
app.get("/account", function(req, res) {
res.render("account", {
name: 'Jon Snow',
age: 35
});
});
And then can access the data inside account using ejs simple template tags like this
<h2> hello <%= name %> </h2>
<p> your age is <%= age %> </p>
In Partial views
Pass the data like this
<%- include('partials/logout', {name='triyon'}) %>
And access it like we did above
<h2> logged out <%= name %> </h2>
This is the best workaround by just passing the view file name as a context while rendering the base ejs file.
/base.ejs:
<html>
<%- include(content) %>
</html>
/extra.ejs:
<div> some content which is to be added in base ejs file </div>
/controller.js:
res.render('base', { content: 'extra' })

EJS: pass a variable to the included file

I'm using EJS as a part of my front-end dev stack.
For example my normal index.ejs looks like that:
<%- include parts/header.ejs %>
<%- include parts/navigation.ejs %>
<!-- HTML content: divs, spans, etc. -->
<%- include parts/footer.ejs %>
What I want is to pass somehow a variable with the include <%- include parts/footer.ejs?variable=value %> and want to read it in the included file, to conditionally show/hide some parts of the content.
I can't find the way to do it. Is it possible with EJS?
Two ways to do this:
Dumb Way
This way is compatible with EJS 1.0, and has the advantage of being compile-time.
Just declare the variables right before includeing.
Example:
included.ejs:
<p><%= variable %></p>
main.ejs:
<% var variable = 'hola' %>
<% include included %>
Smart Way
This method is only available with EJS 2.0 or newer, but might be marginally slower (or a lot slower if caching is not enabled) than the last method:
included.ejs:
<p><%= variable %></p>
main.ejs:
<%- include('included', {variable: 'hola'}) %>

Which code should I use to show related posts/docs on a docpad index-website?

Unfortunately I am no scripting-guru. So this might be obvious for the most of you.
I installed the docpad-plugin-related plugin and it works on individual posts/docs when I include the following code (see instructions)
Now, I would like to show posts/docs sorted by tag on my index-website. For example posts/docs tagged with webdesign should show up as a list. How do I have to change the following code for this purpose?
<ul>
<% for document in #document.relatedDocuments: %>
<li><%= document.title %></li>
<% end %>
</ul>
Is there a solution or does the plugin only work on individual posts/docs?
The docpad-plugin-related plugin is all about getting the related documents on an individual document which is not what you want. You don't need a plugin to do what you want.
Assuming your documents all have a tags property, you can get a list of documents that match a specific tag (e.g. webdesign) with code on your index page like this:
<ul>
<% for doc in #getCollection('documents').findAll({tags: '$in': `webdesign`}).toJSON(): %>
<li><%= doc.title %></li>
<% end %>
</ul>
If you want tag specific indexes for all of your tags, you probably want to look at docpad-plugin-tagging (link).

Why Isn't The Partial File Automatically Created?

Getting through Michael Hartl's tutorial great; I've encountered a few snags along the way but nonetheless, it's working out better than I expected.
My question, is regarding the partial file. In the tutorial if I have read correctly, chapter 5- it advises to edit the 'application.html.erb' file with...
'<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
</div>
</body>
</html>'
The tutorial then says if this line worked I should find a file called 'app/views/layouts/_shim.html.erb' and I cannot find it, thus, it was not automatically created, further not allowing me to pull up the referring static page in my browser (which may or may not be related).
Thanks in advance.
Of course, to get the partial to work, we have to fill it with some content; in the case of the shim partial, this is just the three lines of shim code from Listing 5.1; the result appears in Listing 5.10.
So yes. You have to create partial file by yourself and fill it with proper content. In case you code editor doesn't support partial extraction.
For example:
Using rails.vim plugin for VIM there is the possibility to extract selected lines into partial by using Rextract <partial_name> command. Which creates new file, moves selected lines into it and replace selected lines from source file with <%= render :partial => '<partial_name>' %>

HTMLHelper.Beginform query

I'm learning about HTMLHelpers in ASP.NET MVC.
To render the form HTML tag you would write something like
<% using(Html.BeginForm("HandleForm", "Home")) {%>
<!--Form content goes here-->
<% } %>
or
<% Html.BeginForm(); %>
… Form Contents …
<% Html.EndForm(); %>
To render the a checkbox you would use
<%= Html.CheckBox("bookType") %>
What I would like to know is is why we need to use <% when we use BeginForm whereas we need to use <%= when we use other HTMLHelper methods
Cheers,
Html.CheckBox returns a string of HTML containing an <input> tag.
You need to print this string to the page by writing <%= ... %>.
Html.BeginForm prints the HTML inside the method (by calling Response.Write), and doesn't return HTML. (instead, it returns an IDisposable, so that you can use it in a using block)
Since you aren't printing its return value, you put it in a <% ... %> block, which executes code without printing its results.
<% %> wraps a code block
<%="string" %> is equivalent to <% Response.Write("string") %>
and in ASP.NET MVC 3 you can automatically HtmlEncode with <%: "<htmlTag>" %>
You can definitely write <%=Html.BeginForm() %> but you will also need to write <%=Html.EndForm() %>. Wrapping Html.BeginForm() within a using block will just render the closing </form> tag for you.
Because <%= means "Print this for me", pretty much the same as doing:
<% Response.Write("content"); %>
When <% means that you have a code-block that might do more than just print the value you have nested in it.