I've looked through the documentation for Hexo in it's entirety, but I'm still not exactly sure how to integrate structured data (YAML, JSON) into a page/post using Hexo templating. The closest I could find is the File Data section of the plugins documentation. I'm not really sure if this is what I'm looking for, and there aren't any examples of implementation.
For those familiar with Jekyllrb, the popular Ruby static site generator, you can use a _data directory to store JSON and YAML files. In templates you can reference this data using something like {% for event in site.data.events %}...{% endfor %}.
Is there an equivalent in Hexo?
If you want to access YAML or JSON data in your markdown files, then do the following.
Create your static data file: /source/_data/mydata.yml
var1: "value 1"
var2: "value 2"
var3: "value 3"
Then access it in your post /source/_posts/mypost.md:
---
title: "My Post"
data: 2021-01-01 14:41:29
---
{{ site.data.mydata.var1 }}
You may use expressions like <%= page.title %>, <%= page.date %> or even <%= page.foo %> (in EJS templates) where title, date and foo is a YAML attribute from the header of your post or page, e.g. source/_posts/2015-06-14-my-awesome-post.markdown":
---
layout: post
title: "My Awesome Post"
date: 2015-06-14T17:23:00+04:00
foo: bar
---
Hello World
Hexo 3 now does data files, Jekyll-style.
From documentation:
This feature loads YAML or JSON files in source/_data folder so you can use them in your site.
For example, add menu.yml in source/_data folder.
Home: /
Gallery: /gallery/
Archives: /archives/
And you can use them in templates:
{% for link in site.data.menu %}
{{ loop.key }}
{% endfor %}
Related
I am building a hugo blog site and I have some front matter that I would like to get rendered in the layouts. When I view the results on my localhost:1313, I can see the front matter being populated, but when I host the site on Netlify, the front matter and partial files won't render.
I'm not sure what else to do. I've cleared the console of any errors, made sure my content files match the layout files etc.
Any help?
Here is the link to the live netlify site: https://stoic-meninsky-a5758a.netlify.app/
Here is my directory structure:
-content
-about
-_index.md
-blog
-_index.md
-post-1.md
-_index.md
-layouts
-_default
-baseOf.html
-about
-section.html
-blog
-section.html
-single.html
_index.html
This is a sample file that is pulled from one of my content markdown files:
---
title: "The Herman Show | Blog"
linktitle: "Blog"
draft: true
newsletter: "This is the newsletter"
---
This is how I call the front matter: <p>{{ .Params.newsletter }}</p>
This is how I loop over the section pages to get a navbar
{{ range .Site.Sections }}
<li>{{ .LinkTitle }}</li>
{{ end }}
Make sure your markdown files in the content directory are marked to: drafts:false
I wanna include some HTML file on post!
I wanna show a reference on the right side of my blog.
Reference is HTML file, and it looks like simple doc.
So I created the layout file jekyll.html and I wrote a tag <div>.
Then I added a header variable to my post and reference file is located on _layouts.
This is the code in jekyll.html:
<div class="right">
{% for ref in page.refs %}
{% include ref %}
{% endfor %}
</div>
And this code post's YAML header.
---
layout: jekyll
title: "02. "
date: 2018-12-30 14:38:42 +0900
category: Jekyll
refs: [ bundler.html, test.html ]
---
So, Liquid says:
Liquid Exception: Could not locate the included file 'ref' in any of ["E:/Projects/Jekyll/_includes"]. Ensure it exists in one of those directories and, if it is a symlink, does not point outside your site source. in /_layouts/jekyll.html
What's wrong? Did I do anything wrong?
Or is there another way?
If you want to use a liquid variable in an include tag, you must surround it with curly braces :
{% include {{ ref }} %}
I have a collection of markdown files and I would like to render in two different set of files using two different layout files when building the page:
– html files for the website
– xml files
Is it possible to do this? It seems that a markdown file can be only parsed once. Any suggestions? Many thanks
Instead of having a markdown file that points to a layout file, you're going to need two pages that point to the markdown post file.
There are a number of ways to point to a post file. I like to use a where filter:
{% assign post = site.posts | where:"url", include.url | first %}
Now that you have a post, let's say you have fileOne.html that you want to put that post into. Here's how you'd approach that:
---
layout:main
---
<h1>Some html here</h1>
{% assign post = site.posts | where:"url", include.url | first %}
{{ post.content }}
Then you could do the exact same thing in another file named fileTwo.html.
Browse markdown files from files using each layout and use the liquid filter markdownify to process the same content in different contexts.
Markdownify
Convert a Markdown-formatted string into HTML.
{{ page.content | markdownify }}
You could use Includes for this.
Note: You can either store your source Markdown files in the _includes folder and include them into others with include.
Or you store them directly in the folder where the destination files are and use include_relative.
I'm showing the first option here.
This is the file whose content will end up in another file:
/_includes/foo.md:
**bold**
[Stack Overflow](https://stackoverflow.com)
And here's the file which includes foo.md:
a) When it's a Markdown file:
---
layout: default
title: blah
---
This is the "destination file" for the include.
{% include foo.md %}
b) Anything that's not Markdown:
(if you want the Markdown from foo.md rendered, you have to do this manually)
---
layout: default
title: blah
---
<p>This is the "destination file" for the include.</p>
{% capture tmp %}{% include foo.md %}{% endcapture %}
{{ tmp | markdownify }}
Result in both cases:
<p>This is the "destination file" for the include.</p>
<p><strong>bold</strong></p>
<p>Stack Overflow</p>
I want to create an archive for old blog posts on my jekyll site. Previously, my structure was serving the contents of _posts on my website homepage, index.html. After reading the collections documentation and a few tutorials online, I have added a collection folder _archive to my structure and a test file inside called test-file.markdown.
However, the url mysite.com/archive/test-file fully regenerates my main index.html, not the collection contents.
Structure:
_archive
index.html
test-file.markdown
_includes
about.html
head.html
... other stuff ...
_layouts
default.html
_posts
post1.markdown
post2.markdown
... other stuff ...
css
img
js
_config.yaml
... other stuff ...
test-file.markdown
---
layout: default
title: test
---
_config.yml
# Site settings
title: test
email: test#test.com
url: http://www.test.com
# Color settings (hex-codes without the leading hash-tag)
color:
primary: ffffff #80B3FF
primary-rgb: "24,288,156" #"128,179,255"
secondary: 2c3e50 #FD6E8A
secondary-dark: 233140 #A2122F
third: 979797
collections:
archive:
output: true
permalink: /archive/:path/
# Build settings
markdown: kramdown
permalink: pretty
mysite.com/archive/index.html
---
---
{% for p in site.archive %}
{{ p}}
{{ p.title }}
{% endfor %}
This re-renders the main index.html, not the contents of test-file.markdown.
How can I properly render the contents of _archive at mysite.com/archive/?
EDIT: added --- to index.html
Did you add the:
---
---
{% for p in site.archive %}
{{ p}}
{{ p.title }}
{% endfor %}
on the top of the index.html file? If it's missing it won't run any content within in that file through jekyll's templating engine.
It's hard to tell what your problem really is without seeing the whole site. Provide a repo URL if possible.
If the contents of /index.html are appearing in the /_archive/test-file.markdown output, the post loop is probably in the default layout file, since both of the files share that layout. The solution here would be to move the relevant content into /index.html.
I believe that your /_archive/index.html is not being output. Move /_archive/index.html to /archive.html. Jekyll doesn't process pages inside of the _archive folder because it starts with an underscore.
You'd then have these files output with your current config:
/archive/index.html
/archive/test-file/index.html
In my opinion, you should keep posts as posts, whether they are archived or not. You would then keep the URLs when posts are archived rather than having to set 301s or losing them to the great void.
To do this, add front matter to your archived posts:
---
archived: true
---
And your current posts (you could use defaults to save repetition):
---
archived: false
---
Exclude the archived posts in your main post loop:
{% assign posts = site.posts | where: "archived", false %}
Exclude the current posts on your archive page:
{% assign archived_posts = site.posts | where: "archived", true %}
Hi I'm working on a Jekyll project and I need to put a variable in _config.yml that I want to change dynamically from the template code.
This is what I want to do, but I can't get it to work. Is it possible to do this?
In _config.yml:
my_var: "value"
In template.html:
{% site.my_var = "newvalue" %}
{% case site.my_var %}
{% when "value" %}
//do this
{% when "newvalue" %}
//do this instead
{% endcase %}
While you apparently cannot use Liquid conditionals nor Environment Variables (as in many other build scripts), you can do a selective overrides with a second yml file:
$> bundle exec jekyll serve --drafts --incremental --config _config.yml,_dev.yml
with a _dev.yml:
# overrides title in _config.yml
title: "My Website (dev mode)"
# see my styles uncompressed for dev work
sass:
style: uncompressed
Perhaps that fits your needs...