How can I create a Jekyll variable from another in _config.yml? - jekyll

I would like to create a variable for the image path in _config.yml. I have defined baseurl and want a new one called urlimg: 'baseurl+'images/' Is that syntax correct?
urlimg: baseurl.'/images/'

My advice would be to create two variables in your config.yml, like this:
url: https://www.example.com/
baseurl: site/
imageurl: images/
And use them in your template, like this:
{{ imageurl | prepend: site.baseurl | prepend: site.url }}

Related

jekyll find filename portion of page url

I need to extract the filename portion of the page url, that is from a post saved in 2011-12-31-new-years-eve-is-awesome.md I would like to just the part new-years-eve-is-awesome .
Unfortunately post_url contains also the directory tree 2011/12/31/
This page https://jekyllrb.com/docs/permalinks/ seems to suggest that defining
---
shorttitle: :title
---
in the front matter should work but that produces an empty string from {{ post.shorttitle }}
Here is my solution so far. Perhaps there is a variable left in the code but it's not documented, therefore I just filter the post URL:
{% assign spliturl = post.url | split: '/' %}
{% assign postname = spliturl[4] | replace: '.html', '' %}
{{ postname }}

Installing disqus comment on a jekyll blog

I have followed the instruction on the Disqus page on how to install disqus comment but, it does not load up when I deploy my blog site.
I put the disqus universal code into the includes folder and then added it in the post.html file.
I changed the following code in the universal disqus code:
var disqus_config = function () {
this.page.url = {{ site.url }}{{ page.url }}; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = {{ page.url }}; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
In the post.html file I have:
{% include comments.html %}
Is there something else I am missing?
var disqus_config = function () {
this.page.url = "{{ site.url }}{{ page.url }}"; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = "{{ page.url }}"; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
I had to put the this.page.url into strings quotes as you can see from the code above. The same goes for the this.page.identifier

Using {{ myvariable }} in Nunjucks just prints "{{ myvariable }}" in final HTML file

I use Metalsmith with Nunjucks in a static site generator. In the project, I am importing a Nunjucks macro into a file and trying to invoke it using {{ someMacro }}. However, in the resulting HTML file, "{{ someMacro }}" gets printed as a string instead of the markup inside the macro.
I've also tried {% set someVariable = "abc" %}, and invoke the variable with {{ someVariable}}, both in the same file, but I get the same issue with "{{ someVariable }}" being printed right into HTML.
I've tried invoking both inside in a {% block xyz%} {% endblock %}. Any ideas?
I found my own answer. In the project I'm working on, we use Metalsmith in-place, and there our engineOptions looks like this:
const templateConfig = {
engineOptions: {
filters: {},
tags: {
variableStart: '<<<',
variableEnd: '>>>',
}
}
};
Instead of using {{ }} to use variables or macros, I just had to use <<< >>> to call variables or macros in the main file, and likewise when passing arguments into macros in macro files.

How to pass variables from layout to page in Liquid?

I try to create a global variable in layout and call it in pages
E.g:
<!-- _layouts/post.html -->
{% assign filename = page.url | split: '/' | last | replace: '.html', '' %}
{{ content }}
in post page:
<!-- SomePage.html -->
<h1>Page Name: {{ filename }}</h1> //-> # Page Name: SomePage
The page name should be printed but I do not know how to do it
Any ideas?
I guess you are misusing Jekyll as it is not suppose to work like that. It should be used other way around. Templates are designed to show pages with variables, so that you follow many to one relation pages in templates.
The idea is to put repetitive part of your pages in your layout. So you should have your layout defined like this:
<!-- _layouts/post.html -->
{% assign filename = page.url | split: '/' | last | replace: '.html', '' %}
<h1>{{ filename }}</h1>
{{ content }}
Then you don't have to write that part in your every page, but it is handled automatically for you.

Liquid Template: Append variable to data object

I have the following structure in my Jekyll app:
_data/
test.json
items/
test/
index.html
I am using the following to grab just the name of the ending folder name of my item:
{% assign listing = {{ page.url | remove: 'items/' | replace:'/',' ' | truncatewords: 1 | remove:'...' | escape }} %}
What I'm then trying to do is access the data file which the matching folder name from the _data directory.
I've gotten it to:
{{ site.data.{{ listing }} }}
which allows me to see the data, but I can't actually go inside the JSON object to grab a specific item, like {{ site.data.{{ listing }}.test }} does not work. Any help would be greatly appreciated. Thanks!
Use some brackets like this :
{% assign datas = site.data[{{listing}}] %}
You can now access datas.test.