Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v5.0 preparation #48

Merged
merged 5 commits into from
Jan 19, 2021
Merged

v5.0 preparation #48

merged 5 commits into from
Jan 19, 2021

Conversation

blakek
Copy link
Contributor

@blakek blakek commented Jan 12, 2021

✨ Additions

Can now render lists of items using section tags {{#example}} and {{/example}}. Empty lists and falsy values aren't rendered:

const template = `
{{#stooges}}
<b>{{name}}</b>
{{/stooges}}
`;

const data = {
  stooges: [
    { name: "Moe" },
    { name: "Larry" },
    { name: "Curly" }
  ]
}

render(template, data);
//» <b>Moe</b>
//» <b>Larry</b>
//» <b>Curly</b>

Added a "Templates" section to the documentation. Before, it was pretty simple because this only replaced one tag: {{example}}. With the addition of the section tag (and potentially more in the future), this needed to be documented.

Added functions renderGlob and renderToFolder for programmatically rendering multiple files to either a string or compiling them to a folder. They were in the last v4 release, but were undocumented. renderGlob takes a glob of files to render and calls a function with the filename and rendered contents. renderToFolder is basically a programmatic version of the CLI that renders files matching the input glob and writes them to a destination folder (returning nothing).

Can now handle the edge-case where an object property has a dot in its name:

import { render } from 'template-file';

const data = { 'with.dot': 'yep' };
render('Does this work? {{with.dot}}', data);

💥 Breaking Changes

  • renderString was renamed render
  • renderTemplateFile was renamed renderFile
  • Object properties with a dot now are resolved.

🐛 Known Issues

The output spacing isn't exactly what I want. I figured it's not a huge deal for now, but here's an example:

<!-- Example Template -->
<ul>
  {{#people}}
  <li>{{name}}</li>
  {{/people}}
</ul>

<!-- Wanted -->
<ul>
  <li>Eric</li>
  <li>Anne</li>
</ul>

<!-- Actual -->
<ul>
    <li>Eric</li>
  <li>Anne</li>
</ul>

@blakek blakek requested a review from meyer-mcmains January 12, 2021 14:29
Comment on lines +28 to +29
const tagRegEx = /\{\{\s*(.*?)\s*\}\}/g;
const sectionRegEx = /\{\{\s*(?:#(.*?))\s*\}\}\n*([\s\S]*?)\s*\{\{\s*\/\1\s*\}\}/g;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna trust that these work 😂

@blakek blakek merged commit b56553c into master Jan 19, 2021
@blakek blakek deleted the v5-release branch January 19, 2021 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants