Is it possible to setup configuration values within the config.yml file and have them be printed within the HTML page within Jekyll? I would like to set the default title and description of my website within the config.yml file and have them printed out in the header of all my layouts.
Every template has a site variable, which contains the settings from _config.yml. For example, if you have something like my_setting: "string" in _config.yml, you can access the value in a template using {{ site.my_setting }}.
Note: if you are using jekyll serve, you will need to restart the process for changes to take place. Indeed, _config.yml is not reloaded with the watch option.
Related
The default site setup for a new Jekyll site has a layout specified as "home" in index.md:
---
# You don't need to edit this file, it's empty on purpose.
# Edit theme's home layout instead if you wanna make some changes
# See: https://jekyllrb.com/docs/themes/#overriding-theme-defaults
layout: home
---
If I follow the link, it tells me to create a _layouts folder and create a file in it named home.html and that will be used as the home layout. But if that file doesn't exist Jekyll defaults back to the normal home page.
Where is Jekyll pulling the default layout from?
This default layout comes from the theme, which is gem based and is stored on your computer.
To locate a theme’s files on your computer:
Run bundle show followed by the name of the theme’s gem, e.g., bundle show minima for Jekyll’s default theme. This returns the location of the gem-based theme files. For example, the Minima theme’s files might be located in /usr/local/lib/ruby/gems/2.3.0/gems/minima-2.1.0 on macOS.
Source
I would suggest to start without a theme. Invisible files do not really help you to understand an already quite abstract concept. Remove the theme and write your own layouts and CSS. When you get how it works, you also truly understand how a theme works and what it can and cannot do.
Removing the standard (or any other) theme is simple. Just go to the _config.yml file and remove theme: minima. Now you will use only visible files. You might also want to remove the 'Gemfile', but that requires you to also remove the 'jekyll-feed' plugin from the config. No problem, as you can easily roll your own: https://jekyllcodex.org/without-plugin/rss-feed/
From this version of the manual:
Run bundle info --path followed by the name of the theme's gem, e.g., bundle info --path minima for Jekyll's default theme.
The layout files will be in the _layouts sub-directory of the path returned by the command above.
I'm trying to add folders inside the "_includes" directory on Jekyll. It doesn't work. I've tried adding an underscore to the folder but it doesn't work either. What am I doing wrong? I'm using Jekyll 3.7.0
The most useful link I found about it was this one
Here are some screenshots:
And the stacktrace is:
Incremental build: disabled. Enable with --incremental
Generating...
Pagination: Pagination is enabled, but I couldn't find an index.html page to use as the pagination template. Skipping pagination.
Liquid Exception: Could not locate the included file 'sidebar.html' in any of [".../_includes"]. Ensure it exists in one of those directories and, if it is a symlink, does not point outside your site source. in /_layouts/page.html
jekyll 3.7.0 | Error: Could not locate the included file 'sidebar.html' in any of [".../_includes"]. Ensure it exists in one of those directories and, if it is a symlink, does not point outside your site source.
I think I'm supposed to be able to use sub-directories. What am I doing wrong? Thank you in advance!
Apparently, I found the problem. I don't know why but Jekyll is somehow able to parse comments. It detected I had commented a piece of code referencing the old sidebar.html.
As #DavidJaquel mentions in a comment below, HTML comments <!-- --> are not stopping Liquid to parse our page content, so we should be using Liquid syntax:
{% comment %}
...
{% endcomment %}
I have a documentation project made with MkDocs. I would like to define global variables in the configuration file (mkdocs.yml) to be used in the markdown pages (*.md).
Reading this issue, it seems it can be done by including an extra configuration in the mkdocs.yml file, for example:
extra:
version: 1.0
... and then, use that variable in page for example as follows:
---> My version: {{ config.extra.version }}
I tried that, but unfortunately it is not working in my example (no variable substution):
Am I doing something wrong?
Is is possible to make this work?
No, this is not possible at this time.
You say that you "use that variable in page". I'm assuming you mean a "Markdown" page. At this time template variables are not available in the Markdown pages. The template engine is not even run against the Markdown. The output of the Markdown parser is one of the variables passed to the template. For a more detailed explanation of how that works, see my answer to How do you include flask/jinja2 code inside a markdown file?.
Specific to MkDocs, there is an open issue (#304) discussing adding a feature to support some limited templating within the Markdown pages, but it is scheduled for post-1.0, so its not a top priority at this time.
The given answer is out of date as this can be done with plugins like macros or markdownextradata as mentioned above except you would just reference {{ version }}.
As an update, it is possible to insert the variables from the extra slot in mkocs.yml, exactly as you describe.
To make this work, you need to install the markdownextradata plugin.
I have this:
(defn about-page []
(layout/render "about.html" {:title "About"}))
But since I have moved the directory "templates" from "resources" to the root directory and on a server I might put it yet in another place, it doesn't work. I did it because I don't want the html templates to be embedded in the output jar.
So how can I make the code work, how can I get access to my html files in "templates" then?
And the same question for static images, css, js: I put them in the root directory for now, so they aren't in "resources". They're in "public" folder. However, when I refer to them as "public/css/css1.css", they aren't getting found, that is, the path localhost:3000/public/css/css1.css doesn't exist.
How can I tell Luminus where my statics are located now?
Templates location
Selmer's documentation describes how to change the location of the templates:
By default the templates are located relative to the ClassLoader URL.
If you'd like to set a custom location for the templates, you can use
selmer.parser/set-resource-path! to do that:
(selmer.parser/set-resource-path! "/var/html/templates/")
It's also
possible to set the root template path in a location relative to the
resource path of the application:
(set-resource-path! (clojure.java.io/resource "META-INF/foo/templates"))
This allows the templates to be refrerenced
using include and extends tags without having to specify the full
path.
To reset the resource path back to the default simply pass it a nil:
(selmer.parser/set-resource-path! nil)
The application will then look
for templates at this location. This can be useful if you're deploying
the application as a jar and would like to be able to modify the HTML
without having to redeploy it.
As you want your templates to be reload when you change them you should also remember that Selmer caches them:
When rendering files Selmer will cache the compiled template. A
recompile will be triggered if the last modified timestamp of the file
changes. Note that changes in files referenced by the template will
not trigger a recompile. This means that if your template extends or
includes other templates you must touch the file that's being rendered
for changes to take effect.
Alternatively you can turn caching on and off using
(selmer.parser/cache-on!) and (selmer.parser/cache-off!) respectively.
Assets location
Handling of static resources is configured using site-defaults in your <app>.middleware namespace. You need to configure its' :static entry to use :files instead:
(-> site-defaults
(assoc :static {:files "/var/www/html"}))
and you need to copy files from resources/public directory to that location.
My goal is to create links from any published jekyll page back to its location on Github.
And so, I'd need access to a page's pathname when constructing this url. However, I don't see anything in the api for templates that provides this info. I looked at the source for page, but none of the file/path attributes had values, when accessed via the template.
Update: nowadays you can use {{page.path}}.
It seems like you can construct your link just fine using the liquid code: {{ page.url }}
For instance, if you want this page: http://railsdocs.org/pages/get-involved.html to link to this page: https://github.com/dogweather/railsdocs.org/blob/gh-pages/pages/get-involved.md
Seems like you could add something like:
[source](https://github.com/dogweather/railsdocs.org/blob/gh-pages/{{page.url | replace:'.html','.md'}})
to the markdown and get the link you want.
A more general solution:
Alternatively, we could write a generator that allows a page to access its file name directly. Add this to a .rb file in your _plugins directory:
module Jekyll
class PagePathGenerator < Generator
safe true
## See post.dir and post.base for directory information.
def generate(site)
site.posts.each do |post|
post.data['path'] = post.name
end
end
end
end
and then we can reliably get the post's filename as {{ page.path }}. This solution is more robust than converting from the URL, since page names can have characters that get 'sanitized' out when converting them to URLs. (Posts would also need their date information, etc added back in). Instead, this plugin gives us direct access to a post's name.
A similar strategy could allow us to get the path to the post if that data is also needed.
I'm not sure when this was added, but page.path gives the source file's path relative to the Jekyll root directory.