I've updated my templating language, tumble. It's not quite ready for NPM, but it's getting close. It now handles this case (see if you can guess why I care about this case), in which micro-templates are embeddable and recursable; I've put limiters on recursions, both in the database side of the business and on the number of times you can drop down into a context (this is a build-time limit, although it could be configurable), to prevent runaways or abuse by external users.

This version also limits the list of block-type keywords accepted by the lexer to the set allowed by the parser/renderer, which helps the error handler report when a template is corrupt.

The people who wrote Tumblr were f'ing brilliant. Fortunately, so am I.

Here's the test template, and it works beautifully!

<doctype html>
<html>
<body>

{if:story}{body}{/if:story}

{template:showseries}
{many:contents}
   <li>{if:series}<h3>{title}</h3>
   {if:contents}{showseries}{/if:contents}
   {/if:series}
   {if:story}<h4>{title}</h4>{/if:story}
   </li>
{/many:contents}
{/template:showseries}

{if:series}
<h1>{title}</h1>
{if:contents}<ul>{showseries}</ul>{/if:contents}
{/if:series}
   
</body>
</html>