Including output html in other file

Hi there, I’m very new so sorry if this is a dumb question:
I currently have a layout template and a .md file which uses that template. When I run jekyll it creates the .html file related to that .md file correctly. However, I would want to include that generated html file in another html file (one which isn’t generated by jekyll).
This is because I have lots of repetetive html code blocks, and it would be practical to have a template and include the compiled output html in the main html.

Is such a thing even possible, or should I try something else?
Thanks for your time

I can’t quite understand your use case, but have you looked at includes?
More info / an example of what you want to do may help someone provide some ideas.

1 Like

Let me try to explain a little better:
In my _layouts folder, I have something like this:

<some_html>{{ some liquid }}</some_html>

Then in my website root folder, i have a corresponding md file which uses thing.html as a template:

myvariable: foo
layout: thing
more content

Additionally, I have a index.html in the root, which is the base page of my site.

Now when I let Jekyll serve my site, it generates stuff.html. I would like to {% include stuff.html %} in index.html, but it tells me that the file doesn’t exist (because it doesn’t in the beginning, it only exists when it’s generated).

Does that make it more clear?

right, you can’t include anything in multiple files other than an include file, which is meant to be called from within another file.

So thing.html could be an include instead of a layout and then you can pass it myvariable - though the more content part could be a problem if it is unique.

Its hard to tell from your example whether this is a lot of content or something simple.

Have you looked at includes?

When you use include, pass the name of file as and not stuff.html

When you use include, it will render the given markdown or HTML file with liquid as html and then insert the result.

Using include looks in the _includes. Using include_relative allows you to files elsewhere like a page or layout, but that is very rare use of includes logic.

I’m confused the part about non Jekyll HTML file.

You can make an HTML file that Jekyll processes. And even leave layout off if you don’t want layout or theme usage.


layout: null
<!DOCTYPE html>

A bit of Liquid
{% include analytics.html %}


Your html body, without any liquid in if that's what you want.

But what I did is a weird example. If you have an entire HTML page to copy and paste you could do that.

Normally you’d setup a default.html layout like this.

Maybe also page.html layout which uses default.html and the layout uses head.html, which in turn uses analytics.html, etc.

And the code in a layout gets reused on every page on the site which uses the layout.

And then your pages are focused on content.

Example - no head or body setup in this file directly but they come from the use of layouts.

title: About 
layout: page

## Our business

We are an **IT business**.

Or about.html

title: About 
layout: page

<h2>Our business</h2>

<p>We are an <b>IT business</b>.</p>

I’d recommend going through the 10 step tutorial on the Jekyll docs to understand the Jekyll way of using includes and layouts to reuse content for templating.

Thanks for the pointers, I was able to solve my problem! I had read the page on includes, but was probably to tired to really understand it :sweat_smile:

1 Like