marko-js / marko

A declarative, HTML-based language that makes building web apps fun
https://markojs.com/
MIT License
13.4k stars 644 forks source link

Question: `input` values with <include> / static site generation in Marko #982

Closed zaiste closed 6 years ago

zaiste commented 6 years ago

I'm working on a static site generator called Kulfon. I've decided to replace Nunjucks with Marko for the templating to eventually make something similar to Gatsby. Currently I'm only using <include> along with renderToString() to construct final HTML pages.

I'm splitting templates into 3 directories: layouts/, partials/ and pages/ (at some point I'll also add components/).

Here's a simplified layout:

<!DOCTYPE html>
<html>
<head>
  <include('../partials/meta.marko')/>

  <title>${input.title}</title>
</head>
<body>    
  <h1>${input.title}</h1>

  <include(input.content)/>
</body>
</html>

Here's index.marko page:

<include('../layouts/base.marko')>
  <@content>
    <h1>Title is: ${input.title}</h1>
    <p>Some content goes here</p>
  </@content>
</include>

And here's how I render the final HTML:

page.renderToString({ title: 'This is the title' }))

The problem is that input.title in only visible within index.marko page. I cannot read this value inside the layout unless I pass it explicitly through <include(..., input)/>. In some scenarios I may also need this data in included partials e.g. meta.marko.

Is it possible to force the compiler to include the input values within the context of a single HTML that is generated from a single layout + multiple partials ?

jasonmacdonald commented 6 years ago

Including the input in the include tag is the right approach here. I'd argue that hiding it in some automagic that the compiler does would make reasoning about where the values in base.marko and meta.marko are coming from much more difficult to understand. It would also make testing in isolation harder.

Is it really that big of a deal to add <include('../layouts/base.marko', input) /> to your index.marko file?

zaiste commented 6 years ago

@jasonmacdonald it's not a big deal. I just wanted to make it easier for the end users of my tool. Thank you for the clarification!