How can I pass variables between DRY templates and ncludes in Jekyll

coneybeare

I have a simple Blog site where the front page index is a list of posts, non truncated and rendered exactly like the individual pages.

I have the index page setup and working:

---
layout: default
---

<div>
  {% for post in site.posts %}
    {% include post/post.html %}
  {% endfor %}
</div>

Where post/post.html contains the post layout using the post variable like so:

<article>
    <header>
    {% include post/title.html %}
    {% include post/metadata.html %}
  </header>

  <div class="entry-content" itemprop="text">
    {{ post.content }}
  </div>
</article>

Now, on a specific post page, I want to reuse this include layout so that I can keep my code DRY, so I have the posts use the post.html layout (different from posts/post.html above):

---
layout: default
comments: true
---
{% include post/post.html %}

but the problem is that the include file expects a post variable to exist such post.content is how you access the content.

I have tried:

{% assign post = page %}

and that seems to be the right to pass a variable in, but page is not the right one as it renders as a markdown string instead of the html on the page.

So, how can I pass self -- or whatever is needed -- so that the include file does not need to be altered for the index page or the post page, thus sharing the same code?

coneybeare

While @David Jacquel's answer work, I found it to be unclean and a little verbose, though it did get me on the right track.

_includes/post/post.html swaps the {{ post.content }} for {{ body }}

<article>
    <header>
    {% include post/title.html %}
    {% include post/metadata.html %}
  </header>

  <div class="entry-content" itemprop="text">
    {{ body }}
  </div>
</article>

_layouts/index.html now uses

{% assign body = post.content %}
{% include post/post.html %}

_layouts/post.html uses:

{% assign post = page %}
{% assign body = content %}
{% include post/post.html %}

Simple.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i pass ENV variables between make targets

From Java

AngularJS: How can I pass variables between controllers?

From Dev

How can I pass around variables between handlers

From Dev

How can i use enum to pass variables between two scripts?

From Dev

How can I pass global Meteor variables to templates and access and use them?

From Dev

Can I define tiny templates or functions in jekyll?

From Dev

How do I pass variables into a function, with any signature, using templates?

From Dev

How do I pass values between templates in Laravel?

From Dev

How can i pass variables in Zend?

From Dev

How I can make variables autocomplete in the PhpStorm 9 for Blade templates?

From Dev

How can I share content-properties between templates

From Dev

How can I pass an object between functions

From Dev

How can I pass an object between functions

From Dev

JavaScript - How can you pass global variables between .js files?

From Dev

how to pass data between templates meteor js

From Java

How do I pass variables between stages in a declarative Jenkins pipeline?

From Java

PHP, how do I pass variables between methods?

From Dev

How do I define variables to pass data between JQuery and javascript?

From Dev

Can I nest variables in Flask templates?

From Dev

How can I pass variables into an R markdown .Rmd file?

From Dev

How can I pass variables from awk to a shell command?

From Dev

How can I pass all current environmental variables in a node spawn?

From Dev

How can I pass variables to main view in phalconphp

From Dev

How can i pass multiple variables in the Url for the Spring controller?

From Dev

How can I pass multiple variables into a template in Webpy?

From Dev

How can I pass extra variables with image over an AJAX call

From Dev

How can I pass variables into my title with Latex Interpretation?

From Dev

How can I pass Express.js variables to MongoDB functions?

From Dev

How can I pass variables to main view in phalconphp

Related Related

  1. 1

    How can i pass ENV variables between make targets

  2. 2

    AngularJS: How can I pass variables between controllers?

  3. 3

    How can I pass around variables between handlers

  4. 4

    How can i use enum to pass variables between two scripts?

  5. 5

    How can I pass global Meteor variables to templates and access and use them?

  6. 6

    Can I define tiny templates or functions in jekyll?

  7. 7

    How do I pass variables into a function, with any signature, using templates?

  8. 8

    How do I pass values between templates in Laravel?

  9. 9

    How can i pass variables in Zend?

  10. 10

    How I can make variables autocomplete in the PhpStorm 9 for Blade templates?

  11. 11

    How can I share content-properties between templates

  12. 12

    How can I pass an object between functions

  13. 13

    How can I pass an object between functions

  14. 14

    JavaScript - How can you pass global variables between .js files?

  15. 15

    how to pass data between templates meteor js

  16. 16

    How do I pass variables between stages in a declarative Jenkins pipeline?

  17. 17

    PHP, how do I pass variables between methods?

  18. 18

    How do I define variables to pass data between JQuery and javascript?

  19. 19

    Can I nest variables in Flask templates?

  20. 20

    How can I pass variables into an R markdown .Rmd file?

  21. 21

    How can I pass variables from awk to a shell command?

  22. 22

    How can I pass all current environmental variables in a node spawn?

  23. 23

    How can I pass variables to main view in phalconphp

  24. 24

    How can i pass multiple variables in the Url for the Spring controller?

  25. 25

    How can I pass multiple variables into a template in Webpy?

  26. 26

    How can I pass extra variables with image over an AJAX call

  27. 27

    How can I pass variables into my title with Latex Interpretation?

  28. 28

    How can I pass Express.js variables to MongoDB functions?

  29. 29

    How can I pass variables to main view in phalconphp

HotTag

Archive