More simple inclusion in EJS, avoiding nested file context - node.js

So kind of building off of Node.js - EJS - including a partial
I’ve got a series of EJS templates, all organized in a series of folder. Many include the same header.ejs file. For each include, I need to have an include along the lines of <% include ../partials/footers/footer %>. Because some files are nested deeper/in more folders than others, each has to be set manually.
Is there a solution to avoid this? For example, is there a parameter to set all includes to be based from the root of the views directory (in which all .ejs files are placed)?

Related

Use Twitter Bootstrap LESS files with NPM, in Apostrophe CMS

What is considered best practice if I want to use compile Bootstrap from LESS files in my Apostrophe project?
Is it better to install Bootstrap via NPM, or should I manually download Bootstrap and place the files in lib/modules/apostrophe-assets/?
I have reviewed the documentation that I am aware of to try and figure this out. I am not finding any documentation about including CSS and JS assets which are located outside of the lib folder.
I'm the lead developer of Apostrophe at P'unk Avenue.
You can put those files in lib/modules/apostrophe-assets/public/css (and subdirectories thereof, if desired) and include the one that imports all of the others in your configuration for apostrophe-assets, exactly as described in the tutorials:
Pushing assets to the browser
Or you can import it from a LESS file of your own that also imports your custom site-specific styles. That's really up to you.
Of course there is also nothing stopping you from adding assets directly to your outerLayout.html via link and script tags but if you want to take advantage of Apostrophe's minification process, follow the tutorial.
To be clear, any and all templates in Apostrophe can be overridden for your particular project WITHOUT modifying the node_modules/apostrophe folder in any way.
If a template exists at this path:
node_modules/apostrophe/lib/module/MODULE-NAME/views/TEMPLATE-NAME.html
Then just copy it here in your project:
lib/module/MODULE-NAME/views/TEMPLATE-NAME.html
And your version will be rendered instead.
You can use the Nunjucks extends keyword to extend templates that you invent yourself. You can also use cross-module syntax to extend templates that are in a different module:
{# Finds it in the apostrophe-templates module, your version first, #}
{# node_modules if that doesn't exist #}
{% extends "apostrophe-templates:layout.html" %}
Hope this clears things up a little better!

Loop over files in Twig

I am using Bolt CMS and want to have a table including all files of a specific directory on the server. How can this be achieved using a Twig template?
I have found that the following call works. The argument is a path in the files directory.
{% set files = app.filesystem.browse('SomeDirectory')[0] %}
This is not twig's task. This is something the controller layer should be responsible.
Get the list of files in your controller (though I'd probably create a model-layer class for this), and pass the list to your view, where it only needs to display it.

Style tags and Javascript in Template files

I'm looking through a project built by a foreign team. I am looking through some of the template files ending in ".ejs" and ".tmp", but from my limited knowledge, there is some bad convention here, can some of you confirm? Here are a few things I see on each template file:
1) The doctype, html tags and head are re-defined in each template file (stylesheet and JS files are linked on every one, but a different number of stylesheets and JS files linked depending on which template file it is).
2) there are Style Tags with a bunch of CSS in it, and Script tags with Javascript in it. So I'm guessing they did that because they only need those styles/JS in that particular template, but isn't it bad convention?
This is a node/express app I'm talking about. Is this a sign of bad convention/organization, or is it some sort of performance enhancement/convention I do not understand? Please advise me on this!

Must include files in nanoc always be in the layouts folder?

We would like to use server side includes (SSI) in the content of our nanoc site. This would allow us to chunk shared information and include it in multiple pages. It would also allow us to only maintain one source file of this shared information.
I figured out how to add an SSI (partial) in nanoc. As you know in nanoc we have the following root level folders, among others:
/content/
/layouts/
According to the instructions I found (https://nanoc.ws/doc/items-and-layouts/#partials), it seems the “partials” or include files need to be in the /layouts/ folder (outside of the /content/ folder).
For example:
The following code is used to insert the include file /layouts/partials/shared/test.html
<%= render 'partials/shared/test/' %>
In other words, the code assumes the include file will be placed in the layouts folder. Do you know of a way to change the default path for SSIs to /content/? This way we won’t mix content with layouts?
Thank you in advance.
Partials by default are in the layouts/ directory, and are used through the rendering helper using the #render helper method.
However, you can also put content to include (“partials”) in the content/ directory, too, although the approach then is different.
For example, create content/partials/foo.txt:
I am included!
Make sure the partial files are not routed nor compiled, so edit Rules and ensure these rules are executed first:
compile '/partials/*' do
write nil
end
Now in any item or layout you can include this partial:
<%= #items['/partials/foo.*'].compiled_content %>
This assumes the includer item/layout is filtered using ERB, but you could also use Haml or so.
You can let the compile rule do some processing, e.g. perform Markdown filtering, and then the HTML output from the filter will be included.

EmberJS in combination with Ember-Runner

Good day,
I just started with EmberJS in combination with Ember-Runner. I found an working example on Github.
Ember-runner automaticly adds all your template files and JS (at least that is what I thought) together and generarates a singe HTML / JS / templates / CSS file from your own files every time you save. (And optionally minify's and such). A build tool, just like it says in the description.
This is what it does for the CSS and templates part, however for the JS part it only seems to include only one file in my JS folder which is called 'main.js'. Is it possible to scan that dir for other files so it includes those as well? (So I can have separate controller / model files and such).
I hope my question is clear.
Kind regards,
Matthijn Dijkstra

Resources