tbranyen / combyne

A template engine that works the way you expect.
MIT License
144 stars 19 forks source link

Render parent template from a partial, via injecting a named partial. #34

Closed tbranyen closed 10 years ago

tbranyen commented 10 years ago

Conversation around being able to reference a parent partial came up. This level of inversion is very handy for Express development. Consider the following outside of Express:

var layout = combyne("<body>{%partial content%}</body>");

layout.registerPartial("content", {
  render: function() { console.log("hello world"); }
});

It is quite easy to see how composition can work going down from the layout referencing the partial. What isn't clear is how to go in the opposite direction:

var layout = combyne("<body>{%partial content%}</body>");
var content = combyne("hello world");

content.registerPartial("layout", layout);

There's no way here for the child to know how to inject itself into the layout. This is frustrating for pages in Express that would like to be contextual to the given route:

app.get("/about", function(req, res) {
  // Wants to be injected into the main page layout.
  res.render("about");
});

One way of handling this is an inject/yield style expression being added:

{%render layout as content%}
hello world
{%endrender%}

This would allow referencing a previously defined partial and injecting into it's own defined partial region.

An alternative syntax is also being considered, named yield:

{%yield into "layout" as "content"%}
hello world
{%endyield%}

Possibly render?

{%render layout as content%}
hello world
{%endrender%}
tbranyen commented 10 years ago

I've made an implementation of the latter:

https://github.com/tbranyen/combyne/issues/35

tbranyen commented 10 years ago

Closed via f967f0e4385b46ae936e3a49bda1ca3c96055d80