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.
Related
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!
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)?
If you want to serve a static equivalent of your site, you might want to consider transforming the underlying content by serving a replacement which is truly static. One example would be to generate files for all the paths and make them accessible somewhere on your site.
What they mean exactly? And how to do it?
Your question: What do they mean exactly?
If you want to serve a static equivalent of your site - static refers to html pages that are not dynamically created.
you might want to consider transforming the underlying content by serving a replacement which is truly static. Have 'hard copies' of your pages with the different alternatives
One example would be to generate files for all the paths and make them accessible somewhere on your site. Go through your site and create static html pages (or pdf's) of each one and store them in the file structure that is represented by the URL.
Example of the last:
http://site.tld/product/pear which today is a dynamic (created on the fly by the code and database) but is not really in an actual folder on the server called product. They are suggesting to create a copy of the dynamically created page and store it in an actual folder on the server called product with the name pear.
Your question And how to do it?
Will that work - sort of if you wanted to by adding a .html to the physical file (copy of the dynamic one) and save it but I suspect you will run into all sorts of difficulties that you will need to overcome with the redirect code in places like .htaccess. Another option may be change the domain part of the URL to include static ie http://static.site.tld/ for the static copies and the original URL as is for the dynamic version.
The other big challenge then becomes maintaining the two copies because the concept they talk about is for the content (what is shown in the browser) to remain static over time. Kind of breaks the whole concept of how we build dynamic web sites today e.g. online shops etc.
For example if it's a shop, I would use PHP to also create the physical file when a product is added and not include parts that are going to change, rather include a link to the dynamic info something like:
<?php
$file = 'product/pear.html';
// mysql code here to extract the info and format ready for writing
$content = "<html><head><title>$title_from_db</title></head><body>$page_content_from_db</body></html>";
// Write the contents to the file
file_put_contents($file, $content);
?>
For directory listings, I've created a common .htaccess file as well as a common header ("HeaderName /header.html).
Everything works fine, except one thing: while I don't want the derpy h1 version of "Index of /blah", I'd like to display a customized (and normal-sized) version of that, e.g., "CurDir = /blah". By default, specifying the HeaderName replaces all of that with some static text.
I tried embedding a bit of PHP (getcwd()) but that just returned the root directory where the header.html lives, no matter what subtree was being displayed. Surely there's some way to access the CWD without having to sprinkle control files like this in each subfolder?
I stumbled upon what may be a suitable environment variable while looking at the phpinfo() output... but is there a better (more common) way people handle this, perhaps without even using PHP?
<?php
$dir = getenv("REQUEST_URI");
?>
Say you have a css files loader style.php:
<?php
header('Content-type: text/css');
foreach(array('style1.css', 'style2.css', 'style3.css') as $f)
echo file_get_contents($f)
?>
Style1.css has 12KB, style2.css is 400kgs, and in the red corner obese style3.css weighting 800LBs is world champion at static resource bandwidth consumption!
I'm using style.php to combine the three files and send them to the client. I'm also using similar php files to send out JS resources, combined.
Is there some htaccess rule that I can tell to combine several static resources into a big one, and send that on-the-fly?
/EDIT:
This type of job CAN be handled by htaccess I'm sure I've read somewhere about server files included or something like that but I don't remember where. And I've also seen free hosting services that put a custom header or banner regardless of what files you host there.
Well this type of job (combining css files) cannot be handled by .htaccess. You can at best use mod_deflate to compress the css file's contents.
However in PHP code you can combine and compress various CSS files. Take a look at third method in http://www.catswhocode.com/blog/3-ways-to-compress-css-files-using-php
Finally take a look at minify here: http://www.minifycss.com/minify-tools/minify-css-tools.php
Eventually found what I was looking for. The thing was called Server-Side Includes (SSI).