How to override templates #3406
Replies: 7 comments 1 reply
-
|
Thanks! Great tutorial! |
Beta Was this translation helpful? Give feedback.
-
|
While override is recommended, I found a solution for certain things that don't have their partials HTML parts By using js injection in extra_Js const logoContainer = document.querySelector('.md-header__button.md-logo');
const logoImage = logoContainer.querySelector('img');
const iconifyStructure = `
<div class="iconify">
<div class="iconify_wrapper">
<div class="iconify_box">
<div class="iconify_box-inner">
<div class="iconify_bar"></div>
<div class="iconify_bar"></div>
<div class="iconify_bar"></div>
<div class="iconify_bar"></div>
<img class="iconify_img" alt="${logoImage.alt}" src="${logoImage.src}">
</div>
</div>
</div>
</div>
`;
logoContainer.innerHTML = iconifyStructure; |
Beta Was this translation helpful? Give feedback.
-
|
Very good tutorial, it increased my knowledge |
Beta Was this translation helpful? Give feedback.
-
|
Excellent thread, thank you for sharing. |
Beta Was this translation helpful? Give feedback.
-
|
Very good approach to finding the template. |
Beta Was this translation helpful? Give feedback.
-
|
Dear all, please use reactions to say thanks as this reduces the noise for moderators and people who are involved in a thread. I know you mean well but the contribution guidelines ask to make sure to post only things that make a substantive contribution. |
Beta Was this translation helpful? Give feedback.
-
|
This is great thank you! |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
Recently solved a problem by overriding a template. Thought I'd document my steps as a mini tutorial for others.
Problem
I plan on having a lot of top-level sections in my
navlikeThe problem is, this creates a very messy header..
So, I'd like to exclude Super Awesome Course 1, Super Awesome Course 2, ... from the header. (They'll still be accessible via links on my home page.) How can I exclude these links from the header?
Solution
A good solution is to tweak the html template to exclude them. The final html for the site is generated from a spider web of templates that reference variables and other templates. The whole thing is based on a templating engine called Jinja.
Which Template?
First question is which template do I override?.
To answer this, inspect the header in chrome developer tools and look for a unique element identifier for tabs..
"md-tabs__list" looks pretty identifying. Let's search for it in the repo..
src/partials/tabs.htmllooks like what we want.How to override?
The code here is quite simple.
Our goal is to modify it like this, so that only Home and Contributors show in the header.
Here's how we do it (following the instructions here).
src/partials/tabs.htmltooverrides/partials/tabs.htmlwhereoverrides/is a sibling directory ofdocs/overrides/directory by setting thecustom_dirattribute inmkdocs.ymlliketabs.htmlto make use of the if statement (as shown above ^).mkdocs serveas usual to build the site..This doesn't work.
How to debug?
To see what went wrong, change
tabs.htmllike soand re-serve the site.
As you can see,
nav_itemis an object. To get the title, we need to usenav_item.title. Again, we change our overridden tabs.html file asAlas, it works!
How to use a variable
One more thing.. I don't want to tweak
tags.htmlevery time I want to include or exclude a page. It'd be nicer if I could create a variable inmkdocs.ymllikeinclude_headers = ['Home', 'Contributors']which I could reference in thetabs.htmltemplate.To set this up, create the
header_itemsvariable as an attribute belowextra. Like thisThen in
tabs.htmlchangeto
Beta Was this translation helpful? Give feedback.
All reactions