This page looks best with JavaScript enabled

Extending Nikola

 ·  ☕ 2 min read

Background

This post shows how I’ve extended nikola to fit my needs.

Details

New Posts/Pages Template

This is about the template of new posts/pages (not about compile time template usage). After using nikola to build several sites I found I consistently had a common layout for new posts and/or pages, for instance I always added a TOC, followed by a Background section, a Details section, Notes, etc.; however, when I use nikola new_post... a new post would be created with the standard metadata and the default text that said “Write your post here.”. I then had to paste in my template:

### Background
Background info go here...

### Details
Details (how to do it or what it is about) go here...

### Notes
Any notes go here...

### Links
Any/all links go here...

There had to be a better method and with a little digging I found a simple approach that worked for me. I use the default translation file to modify the “Write your post here.” string to enclude the template text I would like when I run the new_post command. To do this:

  • copy the messages_xx.py file located in nikola source at nikola/data/themes/base/messages/messages_xx.py (where xx is your default language) and place it in the themes/yyy/messages/ (where yyy is the current/used theme for your project) directory in your project
  • open the themes/yyy/messages/messages_xx.py file and change the “Write your post here.” entry to whatever you would like as your template (for example, my entry is: "Write your post here.": "[TOC]\n\n### Background\n\nBackground info go here...\n\n### Details\n\nDetails (how to do it or what it is about) go here...\n\n### Notes\n\nAny notes go here...\n\n### LinksAny/all links go here...",

Now, if I run the new_post command, the basic “Write your post here.” string will be replaced with “[TOC]\n\n### Background\n\nBackground info go here…\n\n### Details\n\nDetails (how to do it or what it is about) go here…\n\n### Notes\n\nAny notes go here…\n\n### Links\n\nAny/all links go here…” (the \n values are translated to new lines) and I can write my new post using my standard template.

Note: you can modify new_page pages by modifying the ‘Write your page here.’ entry.

Share on

drad
WRITTEN BY
drad
Sr. Consultant