0
votes

I'm trying to publish my own website on GitHub Pages. I want to include some repeating parts of code (header) in many files so that I don't always have to copy-paste them. I wanted to use the include functionality offered by Jekyll:

{% include example.html %}

I've put the file I want to include in the _includes directory, however, for some reason it seems to work only if I use a layout in the main file. Am I doing something wrong? If not, is it possible to use Jekyll includes without a layout? (Because I have set all my html files to work without it)

1
What do you mean by "use a template in the main file?" How do you add this template? Which file is the main file? When you don't add this template are you getting an error or just no output? Also, if you have this code in a public repository, including the link in your question will help people provide better feedback.Brad West
Thanks for your fast reply! When I wrote "use a template in the mail file" I meant that if at the beginning of my index.html file I put --- layout: default --- it actually works, but if I don't, it doesn't.Leonardo
Oh, I see ... so that's not a template, that's a layout. I'll still need the answers to the other questions. And a link to the repo would be best.Brad West
All right, thanks for the correction, I'm going to edit this in the question. The main file is the index.html . When I don't add the layout it just shows me the code not rendered (so, it shows me {% include example.html %} ). Regarding the repo, here's the link: github.com/matematicoso/markdown-portfolio . However take into account that for testing I'm just using the repo GitHub made to learn GitHub Pages, so nothing special there!Leonardo
As you can see in the repo with the layout added everything works just fine, but as soon as I remove that the include doesn't work anymore.Leonardo

1 Answers

1
votes

My best guess is you are also removing the yaml tags when you remove the layout. Try this in your index.html file

---
---
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>First try</title>
  </head>
  <body>
    {% include first-try.html %}
  </body>
</html>

You need those --- tags to process Liquid in the file.