I’ve started migrating a simple site to Jekyll. I have a set of templates that everything inherits from, so that all I have to do is fill in the actual content. However, in moving the content over, I find that some of it needs to modify the tags previously specified by the template.
For example, I have a ‘main’ template, which specifies everything up to and including the opening <body> tag. One of my site’s pages uses a bit of JavaScript to modify an html5 canvas, and so its opening tag has to be something like <body onload="startDrawing();">. Now, it seems silly to have an entirely separate template whose only change is this single tag, but equally silly to remove this tag from the template and have every page manually specify the opening <body> tag. What is the right way to go about solving this?
In the individual page that needs the special onload command, add a line to the YAML frontmatter:
Then, in the default template, change
<body>to
<body {{ page.special_command }}>On pages that have that YAML frontmatter, it’ll render the content. On pages that don’t have that “special_command” line in their frontmatter, it’ll render nothing. You’ll be left with an extra space in the <body> tag on those pages, but that still passes the W3 specs as valid code (or, at least, it passed the Validator when I just ran that code through it.)