Inheritance with Ace Templates

I’ve built Hugo from git, and I have Ace templates working now, but I can’t figure out how I refer to the base template properly to make this happen. In Jinja2, this would be something like {% extends ‘base.html’ %}.

I think splitting up HTML opening and closing tags between files is one of the biggest contributing factors to the nightmare that is WordPress, and I’m desperate to find an alternative to that with Hugo.

1 Like

There is a another thread about Ace where I ramble on about the same.

Currently I do not think what you (and me) want is possible. Not a limitation with Ace, but how templates are handled in Hugo.

The brilliant @tatsushid added the Ace support, and I would be a happy puppet if we could find a way to make the base- and inner thing work.

I haven’t spent too much time thinking about it, it should be possible – just have to find the simplest path.

2 Likes

I agree, it shouldn’t be too hard. I’ve started learning Go a bit in hopes of getting to know Hugo’s code, with the end goal of making some sort of proposal about how this might work. Anyway, hopefully we can grow interest in this approach, because I truly believe that it is the right approach to templating.

@spf13 @tatsushid I’ve been looking at the code, and the Ace docs, and it seems like the implementation here will not be terribly difficult. The trouble is that Ace needs to have the base template defined on the Go side, rather than the template side. The trick here will be deciding how this extra bit of information ought to be specified.

The theme metadata file might be a nice place for it, but it seems like that could end up being either inflexible, or growing to be unwieldy. I’d love to get some sort of discussion going on this before I begin work on a pull request.

1 Like

Hi there,

Does any progress have been made on this feature? I do templating using some PHP template engine and I feel limited every time I used Go template engine. No inheritance and very limited loop operations.

I created this issue:

I have assigned it to myself, but anyone is free to chip in.

I use mostly Ace for templating now. My templates isn’t big enough for this to be a problem, but it annoys me to look at it.

I have created a “work in progress” pull request for this. It works great, but need some polish.

Look how my site cleaned up:

The pull request:

Some notes below. I have to sleep on one or two of these. Input welcome:

  • On my site I have only one base template and one content section (= content main). One can have many sections (it handles the case with many sections in base and only some of them in a given template) and more than one base template.
  • The current rules for base template is:
  • It looks for baseof.ace (only Scandinavians will get that) first in current dir and then in layouts/_default.
  • If baseof.ace isn’t found, it behaves like today.

Some examples:

layouts/_default/single.ace
layouts/_default/list.ace
layouts/_default/baseof.ace
index.ace

In all of the above cases, the base template in _default will be used.

layouts/_default/single.ace
layouts/_default/list.ace
layouts/_default/baseof.ace
layouts/blog/list.ace
layouts/blog/baseof.ace
index.ace
baseof.ace

In the example above the front page and the list template in the blog section have their own base template.

This was the most obvious way and the simplest implementation.

Make sense?

@spf13 any thoughts/objections about the base template handling above before I wrap it up?

I think this approach makes a lot of sense.

Is it a reference to Ace of Base, the band from the 90s?

So it doesn’t cascade up the tree? If the baseof.ace file isn’t found in the same directory, it just doesn’t have a base. Is that correct?

  1. Yes, it is the Swedish band from the 90s.
  2. It looks for a base template in this order
    1. Same folder as the template it tries to load (i.e. /blog/single.ace and /blog/basefrom.ace)
    2. In _default
  • In most cases (like my example), you will have one base file in _default.
  • If no base file is found, the template is handled like today

So you can have some templates that doesn’t use a base template, but not if you put one into _default.

To avoid some stupid duplication in a project of mine, I expanded the naming scheme for the Ace base templates:

And now with some docs:

https://github.com/spf13/hugo/blob/master/docs/content/templates/ace.md

Nice work! I hope this goes into a release soon as I think it will really DRY up my Ace templates. Thanks guys!