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

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).

Related

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

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

Dashing - dynamically delete widgets

how can i dynamically delete widgets from a job(rb) in Dashing?
I am building the dashboard dynamically by sending a data to the erb file:
<div class="gridster">
<ul>
<% settings.servers.each do |data| %>
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="<%=data['webHost']%>" data-title="<%=data['name']%>" data-version="<%=data['Version']%>" >
</li>
<% end %>
</div>
Yes. I wrote a simple example job that can do just that here:
http://www.mapledyne.com/ideas/2015/6/30/delete-a-dashing-dashboard-widget
You basically just want to manipulate the Sinatra::Application.settings.history variable, but the code in that link should get you most of the way to where you want to be.
Or skip the post and go right to the gist file:
https://gist.github.com/mapledyne/6fb671c17c3f865309f3#file-delete-widget-rb
You can also generate parts of the erb dynamically if you don't know the widgets in the first place (more complicated), but it starts with the same - leveraging that same history variable.

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

Display records in refinery page

I need to get the record from database and i want to display that record value on home page. My table name is refierny_about. I have a model. It was created while I generating the engine.
Thanks for your advise.
My guess is "About" is your custom extension do this.
The syntax here is: The Refinery Namespace::Your Extension Namespace::Extension Model Name
so you can use the controller is Refinery::Abouts::About.all
app\decorators\controllers\refinery\pages_controller_decorator.rb here past this code
Refinery::PagesController.class_eval do
def home
#posts = Refinery::Abouts::About.all
end
end
and its your html code.
<% #posts.each do |a| %>
<p><%= link_to about.title, refinery.abouts_about_path(a) %></p>
<% end %>