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

How to write my own handlebars helper and use it in patternlab-node? #958

Closed
michaelworm opened this issue Oct 10, 2018 · 18 comments · Fixed by #959
Closed

How to write my own handlebars helper and use it in patternlab-node? #958

michaelworm opened this issue Oct 10, 2018 · 18 comments · Fixed by #959

Comments

@michaelworm
Copy link

I am using Pattern Lab Node v2.12.0 on Windows, with Node v8.11.1, using a Gulp Edition.

I am very confused how to actually write my own handlebars helper and use it in patternlab-node. I read there are patternlab plugins but is this the correct way to load a handlebars helper?

I hope someone can point me in the right direction. I also didn't find any information about plugins or whatsoever on the official documentation page.

Thanks in advance.

@michaelworm
Copy link
Author

michaelworm commented Oct 11, 2018

So I tried writing my own plugin and use it via npm link. Pattern Lab says it has been installed correctly but it doesn't seem to be executed when building. Thanks to poor documentation I am really lost. Has anyone been able to write a working plugin and could help me out?

Edit
I found out that the plugin directory requires the prefix plugin-node- because Pattern Lab explicitly searches in node_modules for it, see https://github.com/pattern-lab/patternlab-node/blob/v2.12.0/core/lib/plugin_manager.js#L85

I had a hard time finding this one, this should definitely be pointed out in the documentation!

@engelfrost
Copy link
Member

I have not tried writing a plugin recently, but I'm thinking that this functionality might be interesting inside the handlebars engine. Something like this:

  1. Add a new setting in patternlab-config.json, e.g. engines.handlebars.helpers with a list of helpers ["path/to/helper.js", "path/to/helper-dir/"].
  2. When the handlebars engine loads it reads the config and adds any helpers.

The React engine seems to do something similar:

usePatternLabConfig: function(config) {

@engelfrost
Copy link
Member

Got something working, submitting PR soon

@michaelworm
Copy link
Author

@engelfrost really nice! Thank you! In the meanwhile I published a Pattern Lab plugin to resolve my usecase. If anyone is interested, feel free to use https://www.npmjs.com/package/plugin-node-patternlab-inline-assets

@engelfrost
Copy link
Member

@Miw0 Cool, you figured the plugin thing out! I might combine our two solutions to make a helper-loading plugin.

@david-moldenhauer
Copy link
Contributor

@Miw0 @engelfrost
Adding a Handlebars helper is done via the plugin setting inside the patternlab-config.json:
"plugins": {
"plugin-node-handlebars-helpers": {
"enabled": true,
"more_options": "options1"
}
}
the key "plugin-node-handlebars-helpers" is the npm modules name you can check https://github.com/david-moldenhauer/plugin-node-handlebars-helpers for more details.

@engelfrost
Copy link
Member

Thanks @david-moldenhauer. A common way to add helpers/extensions to both Handlebars and Nunjucks is making it's way into the engines themselves now. If you have knowledge to add to the PR linked above based on your plugin that would be great!

@michaelworm
Copy link
Author

@Miw0 @engelfrost
Adding a Handlebars helper is done via the plugin setting inside the patternlab-config.json:
"plugins": {
"plugin-node-handlebars-helpers": {
"enabled": true,
"more_options": "options1"
}
}
the key "plugin-node-handlebars-helpers" is the npm modules name you can check https://github.com/david-moldenhauer/plugin-node-handlebars-helpers for more details.

I first thought you build a plugin which would allow me to add any handlebars helper I need but I see that your plugin ist just a collection of useful util helpers. Anyway thanks for posting, this would have helped me alot 2 weeks ago!

@stale
Copy link

stale bot commented Dec 22, 2018

It's hard to keep track of everything. This issue has been automatically marked as stale because it has not had recent activity, neither from the team nor the community. It will be closed if no further activity occurs. Please consider adding additional info, volunteering to contribute a fix for this issue, or making a further case that this is important to you, the team, and the project as a whole. Thanks!

@stale
Copy link

stale bot commented Jan 5, 2019

Issue closed after going stale. It can be re-opened if still relevant.

@stale stale bot closed this as completed Jan 5, 2019
@engelfrost
Copy link
Member

Still relevant, waiting for PR to be processed

@stale
Copy link

stale bot commented Mar 9, 2019

It's hard to keep track of everything. This issue has been automatically marked as stale because it has not had recent activity, neither from the team nor the community. It will be closed if no further activity occurs. Please consider adding additional info, volunteering to contribute a fix for this issue, or making a further case that this is important to you, the team, and the project as a whole. Thanks!

@engelfrost
Copy link
Member

Still relevant

@engelfrost
Copy link
Member

For anyone else who is impatient and needs this in PL v2 I've created this Handlebars Engine for myself. The project will not be maintained in the long run: https://github.com/engelfrost/patternengine-node-handlebars-extended

@stale
Copy link

stale bot commented May 11, 2019

It's hard to keep track of everything. This issue has been automatically marked as stale because it has not had recent activity, neither from the team nor the community. It will be closed if no further activity occurs. Please consider adding additional info, volunteering to contribute a fix for this issue, or making a further case that this is important to you, the team, and the project as a whole. Thanks!

@stale
Copy link

stale bot commented Jun 10, 2019

Issue closed after going stale. It can be re-opened if still relevant.

@stale stale bot closed this as completed Jun 10, 2019
@engelfrost
Copy link
Member

PR still waiting to be processed

@engelfrost engelfrost reopened this Jun 11, 2019
@stale
Copy link

stale bot commented Aug 10, 2019

It's hard to keep track of everything. This issue has been automatically marked as stale because it has not had recent activity, neither from the team nor the community. It will be closed if no further activity occurs. Please consider adding additional info, volunteering to contribute a fix for this issue, or making a further case that this is important to you, the team, and the project as a whole. Thanks!

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

Successfully merging a pull request may close this issue.

3 participants