npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@balena/doxx-handlebars-helper

v1.0.1

Published

Handlebars helper for smart partials import.

Downloads

132

Readme

Handlebars helper for smart partials import.

Extracted from doxx.

This is a very simple module that exposes a couple of convenience methods to render Handlebars templates by hand, and a "smart partials" helper.

Handlebars

The packaged Handlebars engine is exported as Handlebars

Utility methods

compileTemplate(templateString)

A memoized version of Handlebars.compile

render(template, context)

Compiles the template and renders it to a string with the given context. Uses the memoized compileTemplate.

getPartial(partialName)

Find the partial with the given name (the partial must be previously registered) and returns compiled template for it. Uses the memoized compileTemplate.

getBestPartial(prefix, options, separator = '/')

Loops through an array of options looking for partials named "#{prefix}#{separator}#{option}". Returns the first match as compiled template. Returns undefined if no matches found. Uses the memoized compileTemplate.

Example: getBestPartial('some/folder', [ 'specificPartial', 'defaultPartial' ]) will first check for some/folder/specificPartial and then for some/folder/defaultPartial and return the compiled template for the first existing partial.

Special JSON partials handling. For convenience multiple partials can be combined under a single JSON partial. To use this feature that special combined partial name must end with .json (if your partials are imported from the filesystem this usually means that the file should have double extension, like some/file.json.part). The partial is parsed and it should result in an object whose keys are treated as search partial names. In this case the separator parameter is ignored.

This is useful when you have lots of small (usually one-line) partials — instead of a folder with hundreds of files in it you can have a single JSON file.

createImportHelper(options)

This is the main feature of the module. The function creates a helper (but does not register it) that can be called like that:

{{ import "prefix" }}.

This helper will search for partials with their names starting with prefix (using getBestPartial). The options must be specified in the specific field on the helper execution context.

The options are:

importName, default 'import'. Used in exceptions and log message to identify the helper.

beforeRun, default null. If a function is passed it will be called before each helper call. A good place to do some checks and log things / throw errors.

partialsSearchFieldName, default '$partials_search'. The name of the property on the execution context where the search order is defined (corresponds to the options parameter of the getBestPartial method).

defaultPartialName, default '_default'. The name of the partial to be checked if not partials from partialsSearchFieldName are found.

contextFieldName, default '$variables'. Used for reporting only. If set and no partials are found the value of this field from the execution context is included into the exception message.

registerHelper(options)

Creates the import helper and registers it with Handlebars under options.importName. options are the same described above.

registerConsolidate(consolidate, options)

Convenience method if you use consolidate to render your handlebars templates. This calls registerHelper and then passes the reference to packaged Handlebars to consolidate.

License

The project is licensed under the Apache 2.0 license.