Skip to content
This repository has been archived by the owner on Apr 24, 2018. It is now read-only.

nested layout support? #18

Open
liamstask opened this issue Jul 19, 2013 · 6 comments
Open

nested layout support? #18

liamstask opened this issue Jul 19, 2013 · 6 comments

Comments

@liamstask
Copy link
Contributor

Hi! Thanks for jkl - so far it seems like a nice, simple alternative to Jekyll that retains most of its good qualities.

I'm converting a simple site, and have a layout (post) that, in its front matter, specifies a parent layout (default). I'm not seeing the parent layout get applied, which is a bit of a pain since otherwise I need to duplicate the main page structure in both my post and default layouts.

Have I missed a way to do this with jkl, or is it currently not supported? I'd be willing to dig in and submit a pull request if that would be desirable.

@bradrydzewski
Copy link
Member

Yep, you should be able to apply a parent layout. You can see our reference site where we have a post:
https://raw.github.com/drone/blog.drone.io/master/_posts/2012-10-08-drone.io-for-opensource.markdown

And it refers to this parent layout:
https://github.com/drone/blog.drone.io/blob/master/_layouts/posts.html

@liamstask
Copy link
Contributor Author

Right - that much is working for me. The case I'm trying to solve is where my posts.html layout looks like http://pastie.org/8156399

ie, the markdown content would be rendered within the post layout, which in turn would ideally be rendered within the default layout.

edit: gah, formatting

@bradrydzewski
Copy link
Member

I haven't tried this myself, but based on the Go code it should be possible. Unfortunately I'm not on my development machine at the moment, so I can't test, but I can help walk you through the code so you can troubleshoot.

Here is where we compile our templates:
https://github.com/bradrydzewski/jkl/blob/master/site.go#L184

we walk the base directory and use the IsTemplate helper function to identify templates
https://github.com/bradrydzewski/jkl/blob/master/util.go#L48

this IsTemplate logic basically says it must be a .html file and in the _layouts or _includes directory. If it meets this criteria we add to an array:

layouts = append(layouts, fn)

we then process all the templates together:

s.templ, err = template.New("layouts").Funcs(funcMap).ParseFiles(layouts...)

you should then be able to import templates by file name (you should not include the path)

{{ template "footer" . }}

I'm not sure if you need the suffix or not:

{{ template "footer.html" . }}

Hopefully that helps. I'll try to run some tests later today when I get a chance

@liamstask
Copy link
Contributor Author

Ah, I think this is still not quite what I'm after :)

{{ template "footer.html" }}

would specify which child layout to include from within the parent layout, as far as I can tell.

I'm looking for a way for the child to specify which parent it wants to be rendered in. specifically, i'd like to be able to indicate in both my post and page layouts that they should each be rendered within the default layout. The default layout shouldn't need to (and can't, really) know which child is going to be rendered within it.

Jekyll solves this by allowing a parent layout to be specified in the front matter of the child layout, but I don't see any affordance for this kind of relationship in the existing jkl codebase.

@bradrydzewski
Copy link
Member

ahh, yes, now I understand. If I remember correctly a Jekyll template can have YAML front-matter and specify a parent template. The template will be rendered inside the parent template's {{ .content }}.

when I initially wrote jkl it was for myself and I didn't require that kind of relationship, although I did think about adding it just for compatibility. It would have required a large change to the codebase, so I left it out.

to do something like this we would need to split the _partials, which would be compiled as a single *Template, from the _layouts which would each be compiled individually into its own *Template and stored in a map. When compiling a layout template, we would need the partials, so we would want to clone the _partials *Template and use it to compile the individual layout template. see (http://golang.org/pkg/text/template/#Template.Clone)

So it is definitely possible

@liamstask
Copy link
Contributor Author

OK, cool. I'll perhaps poke around at this and see if I can come up with anything worth sharing. Thanks for the quick feedback.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants