Skip to content

1 min read中文

Using the <template> Tag to Hide Content Blogger Inserts

A method that wraps a fake </body> marker in a <template> tag to get around Blogger's block on nested comments, hiding its auto-inserted content.

Machine-translated from the Chinese original.

TL;DR: just wrap the content you want to hide in <template></template>.

0.webp

While hacking up a theme following the article Notes on accessing Blogger from mainland China, I found that the method it offers,

Replace </body> with &lt;!--</body>--&gt;&lt;/body&gt;

no longer works.

Let me briefly explain how the original method worked. When Blogger inserts the content we want to get rid of, it probably just does a simple string replacement: str.replace(r'</body>', target + r'</body>', 1). But we rig up a fake </body> and comment it out, so the content Blogger inserts also ends up inside the comment.

The reason the original approach stopped working is that Blogger’s own code added a comment of its own, and HTML apparently can’t nest comments (this is somewhat uncertain).

Stack-Overflow-driven development After some digging, I found this: Are nested HTML comments possible?. It mentions several ways to hide content; I went with wrapping a fake </body> in a <template> tag.

Just replace </head> with

&lt;template&gt;</head>&lt;/template&gt;
&lt;/head&gt;

and replace </body> with

&lt;template&gt;</body>&lt;/template&gt;
&lt;/body&gt;

and that’s it.