Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global include #12

Closed
ghost opened this issue Feb 7, 2017 · 4 comments
Closed

Global include #12

ghost opened this issue Feb 7, 2017 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 7, 2017

continuing from jekyll/jekyll#5868

I've dealt with similar things in past, and am curious if this is similar to what you're looking for. Using a variant of using includes as custom functions we can create year_diff.html:

https://github.com/xHN35RQ/alecive.github.io/commit/0f3ad47b191b0e00cf9e87c1a3169460d3d82530

Now can call {% include year_diff.html minus="2010" %} anywhere in Layouts, Includes, or pages to include the above code into the layout, include or page. And output the result of the function using {{ yearDiffStr }}:

https://github.com/xHN35RQ/alecive.github.io/commit/cd1d55e2779dcf4040c89a6231fe80b7856ad8cc

You can make the include global by including it into a global include or layout such as head.html just like this:

{% include year_diff.html minus=include.minus %}

Now we have an include (head.html) that includes a second include (year_diff.html) and each include receives the minus variable.

Now, wherever you include head.html for example in default.html https://github.com/xHN35RQ/alecive.github.io/blob/master/_layouts/default.html#L4 you could change that to {% include head.html minus=page.minus %} then just set minus: 2010 in your page front matte YAML, and use {{ yearDiffStr }} anywhere in that page to get the return from that function.

Make sense? Hope I understood your questions, and this helps. I've used this process several times before for global functions in Liquid and it's worked well for me.

@alecive
Copy link
Owner

alecive commented Feb 8, 2017

Hey @xhn35rq , thank you for your detailed help! The solution you proposed works (I didn't know that we could pass variables from the included year_diff.html file up to the file that calls it), and is clearly the best way to work around the issue.

Still, it would be nice to have some dynamically allocated global variables to be defined somewhere and to be used where needed.

I would like to create a pull request from your code, can I ?

@alecive
Copy link
Owner

alecive commented Feb 8, 2017

Another small improvement: if you {% include year_diff.html minus="2010" %} into the layout file, e.g.:

<!DOCTYPE html>
<html>
    {% include year_diff.html minus="2010" %}
    {% include head.html %}

    <body id="page-top" class="index">

    {% include header.html home="yes" %}
    {{ content }}
    {% include about.html %}
    {% include footer.html %}
    {% include js.html %}

    </body>
</html>

You can use the variables defined in year_diff.html throughout the files you included later on in the layout.

@ghost
Copy link
Author

ghost commented Feb 8, 2017

PR sent, select which commits you want.

And it's indeed useful that includes can include other includes and pass variables between them. Include chaining...

Another small improvement: if you {% include year_diff.html minus="2010" %} into the layout file

Good point. I forgot to mention this yesterday to include directly into post or page templates, good to see you're thinking along the same lines!

@alecive
Copy link
Owner

alecive commented Feb 8, 2017

Fixed in #13 , thank you!

And it's indeed useful that includes can include other includes and pass variables between them. Include chaining..

I agree, this is definitely a big plus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant