passing data to partials in ejs - node.js

I am using ejs-locals to give me support for partials in express/node.js
The problem is the library has a bug when you include a partial within a partial. The 2nd partial include is trying to find the file relative to the grandparent or page.ejs that called the 1st partial.
So this requires I either flatten out my ./views so all files are only in ./views (no sub-dirs)...or I symlink to the partial from the directory that made the initial partial call.
Perhaps there is another/better way of doing this?
In a lot of my views, I have a face mash, that gives me html with people's avatars. I can't just use <% include faces %> and call it a day, because the collection of people comes from many different objects depending on the page from which it is called.
here's an example:
on an item page, I call <% include stats %>, which calls faces partial
<- partial('faces', { people: item.users }) %>
or on a list page, I call <% include stats %>, which calls faces partial
<- partial('faces' { people: list.users }) %>
The partial 'faces.ejs' just loops over people and spits out a link to their profile. But because I can't include a partial within a partial I'm stuck either sym-linking all over the place or trying something different.
./views/list/index.ejs calls a stats.ejs partial that calls faces.ejs partial...so I need to symlink ./views/list/faces.ejs to ./views/stats/faces.ejs for it to work.
You cannot pass data to a simple include, so I cannot pass in different collections of users depending on the context in which it was called.
<% include faces %> would require the face.ejs loop over either item.users or list.users hardcoded in faces.ejs
What else can I do that will work with nested partials/includes? I have filed a bug with ejs-locals, and a few others have as well, but its been a month w/o any fix.

Related

How do I render multiple partials based off of JSON data?

I have a database with user data, If I wanted to render a partial for each document in my MongoDB collection how would I do that?
I can render one partial by just adding it to my view but how do I make it render multiple ones automatically without me retyping this {{>somePartial}} for each new document added?
I looked around and found something suggesting AJAX they were not completely similar to my problem. If this is the solution is there another way other then AJAX?
every time a new document is added to MongoDB I'd like a new partial added to my view (I don't mind if it only shows when the page is refreshed)
{{>somePartial}} // Different data
{{>somePartial}} // Different data
{{>somePartial}} // Different data
You can make an array of all the data from your monogdb then you can render your page and pass your data to the ejs template and add dynamic includes and pass different data e.g.
<% for (let i = 1; i <= <length>; i++) { %>
<%- include("<partial>",{data:"your_data_for_this_index"}) %>
<% } %>

Including a partial within existing partial - Express and EJS

I am using partials in my views but am wondering if it is possible to include another partial within an existing partial. It seems not at present but cannot find any information on if this is possible at present.
My current setup looks like this
views
partials
home
section1
file.ejs
partial.ejs
home.ejs
So within my home.ejs file I can include my partial.ejs partial
<% include partials/partial %>
But within my partial.ejs I can't seem to include another partial
<% include home/section1/file %>
Has anyone done this ?
Thanks

Sails js render partial view

I'm new in SailsJS so i have a question about rendering partial views.
I've came from .NET world and partial views in ASP.NET MVC are much more clever than in sails. In .NET MVC i can provide some controller as partial view path and this controller will be executed and partial view will be rendered with that controller data.
For example I want to show some account info in the corner of my website. So in .NET i can call partial view, providing controller path, this controller will query account info, provide it to partial view, and then this result will be inserted to main view.
In Sails i can call partial view, but all data (locals) for partial view will be pushed there from main view. So i have to put account information in every controller in application?
How can i solve this problem?
In other questions i've found that I can take username from req.session but it does not solve all cases. How can i show three random topics in page footer for example?
I think you are looking at the wrong place about the partial views.
This is a front-end problem .What I use for such partial views is Angular UI-router . Actually I am not sure if using angular is in the scope of the solution for you,So I'll keep this short . If you feel my method is useful ,you can comment it ,and I'll elaborate further on how to do it .
Depends on the template engine, that you use.
With ejs you can use <%- include('someview') %> or <%- include('someview', { mydata: { foo: bar} }) %>
The latter allows the include file to be called several times with diff data. E.g. <%- include('someview', { mydata: foo }) %> will copy foo (from locals) to the mydata key. This will be added to locals, so all other locals are available too.
As for providing the actual data, you can use a sails service. Just add data to res.locals.myKeyForTheInclude.
You will have to call the service each time.
But you can add it in config/http as a middleware.
middleware: {
myFoo: myService.myFunc,
order: [
...
'myFoo', // before router
'router',
...
],
All that is left is that each template needs the include.
You can not avoid that, only the template knows where the include should be.
But if you use a layout, that maybe it fits into the layout.

Does nanoc support nested partials?

I am using partials in nanoc, and I was wondering if it's possible to nest partials in nanoc. In other words, can I have a partial within a partial?
When I tested this, the site compiled, but the nested partial did not display.
I'm using the Partials implementation described in this stackoverflow post: Must include files in nanoc always be in the layouts folder?
While nested partials are not required for what we're trying to do, I was just wondering if this is possible.
Thank you in advance.
Yes, you can use nested partials with nanoc. Here's a way to demonstrate this:
Create a new site using nanoc.
From within the site directory, create the folder content/partials.
Create the "outer" and "inner" partial content. In the file content/partials/_outer.html, place:
<p>This is the outer partial.</p>
<p><%= #items['/partials/_inner/'].compiled_content %></p>
And in the file content/partials/_inner.html:
This is the inner partial.
Note that we now have one partial including the contents of another.
Edit the main page, content/index.html, so it embeds the outer partial:
<h1>A Brand New nanoc Site</h1>
<%= #items['/partials/_outer/'].compiled_content %>
Add these rules to Rules above the ones already present:
# Filter but do not lay-out partial content
compile '/partials/*' do
filter :erb
end
# Do not output partials; they are only ever embedded in other content
route '/partials/*' do
nil
end
Now generate the site with nanoc compile. When you view it you'll see the inner partial content nested inside the outer partial content, nested inside the main page, like this:
A Brand New nanoc Site
This is the outer partial.
This is the inner partial.

How to include html code in a view?

I'm using express.js and EJS as template engine.
I don't understand how to use partials, I have seen at the examples but the author used JADE template engine, so I don't know how to apply it with EJS.
I have a simple view named: test.ejs and another .ejs file named part1.ejs
I need to show part1.ejs inside test.ejs.
I tried putting <% partial('part1', {}) %> (into test.ejs) but nothing happen, It does not include that file.
Could someone give me an example?
Thank you!
The correct code in your situation would be:
<%- partial('part1') %>
If you want to include unescaped HTML use <%- and if you want to escape HTML (unlinkely though when including a partial) you can use <%=.
Resources:
Node.js - EJS - including a partial
http://groups.google.com/group/express-js/browse_thread/thread/62d02af36c83b1cf
Its an old thread, but here is how you do it in the newer version of EJS.
<% include part1 %>
given part1.ejs contains the html you wish to include.

Resources