1. Adam Crossland
  2. mtemplate

Wiki

Clone wiki

mtemplate / WhatsNew

What's New?

Apr 26 2012 -- mtemplate is now compatible with Go 1.

Sep 19 2011 -- It quickly became apparent that having variable substitution being indicated with a name inside single curly-braces ( {varName} ) wouldn't work with rendering JavaScript. So, variable substitution is now indicated by an exclamation point (!) after the initial opening curly-brace, like this: {!varName} .

Aug 9 2011 -- The caching of parsed templates is complete, working and checked-in. Now, when your code calls ParseFile or MustParseFile, the created Template object will be saved so that it does not need to be recreated each time that a particular file needs to be rendered. If your code needs for some reason to explicitly remove a cached template, the ClearFromCache(filename string) function can be called to do that.

Added RenderFile and MustRenderFile convenience methods to wrap paired calls to ParseFile/MustParseFile and Execute. The two calls always go together, so Render saves some repetitive coding.

Aug 16 2011 -- The named blocks functionality is complete. Named blocks allows a template to specify that different sections of its content are to be available to its parent template as named entities. Those named entities can be included in the parent template as it sees fit. This is very similar to how Django templates work.

Here is an example of how this feature works.

base.html:

<html>
  <head>
    <title>Shazowie Magic Shoppe - {!pagetitle}</title>
    {.child js_libs}
  </head>
  <body>
    <img src="/imgs/banner.jpg">
    <h1>Shazowie Magic Shoppe</h1>
    <h2>Your one-stop shop for esoterica and some other stuff</h2>
    {.child page_header}
    {.child}
  </body>
</html>

faq.html:

{.parent base.html}
{.block js_libs}
<!-- all of my faq page-specific js libraries -->
<script href="/js/kQuery-min.js"></script>
<script href="/js/ajaxFaq.js"></script>
{.end}
{.block page_header}
<h3>Frequently Annoying Questions</h3>
<h4>about the Shazowie Magic Shoppe</h4>
{.end}
<div class="faq">
  <span class="faqq">So, are you guys totally nerdy or what?</span>
  <span class="faqa">NO! For the last time, <i>magic is for cool kids</i>.</span>
</div>

We have two templates: faq.html and base.html. Base.html holds the skeleton of all of the web pages that are displayed on our web site. Each individual page may need to include different JavaScript libraries so that they can provide cool effects and a rich, interactive user experience, so it has the directive {.child js_libs} inside the header section. Looking at faq.html, we see that it has a {.block js_libs} section. Everything between the block directive and the matching {.end} directive will be inserted into the parent template where the {.child js_libs} directive is found. Similarly, the content inside {.block page_header} will go where {.child page_header} sits.

The base template also includes a {.child} directive that does not have a name. What gets inserted there is all of the content in the child template that is not inside a {.block} section.

A {.block} directive can contain any template directives.

Aug 17 2011 -- There is now an .include directive that causes the given file to be parsed and then inserted into the template. In many respects, it is the opposite of the .parent/.child/.block directives; the .parent directive says, "after you render me, render this template and put me inside it" while the .include directive says, "when you render me, also render this other template and put it inside me."

Updated