Plugin template helper

AI-generated summary

The discussion revolves around creating a template helper in a Discourse plugin. sevenmaxis initially shares their code for a helper function, insert_ad, but encounters an error in a production Docker environment. LeoMcA suggests that the issue is due to a different Discourse version being used and points to a commit that moved the helpers from discourse/lib/helpers to discourse-common/lib/helpers. LeoMcA advises updating the import statement to import { registerHelper } from 'discourse-common/lib/helpers';, which sevenmaxis confirms resolves the issue.

In my discourse plugin’s folder discourse-adplugin/assets/discourse/helpers/ I’ve created a file with helper

discourse-adplugin/assets/discourse/helpers/eq.js.es6:

import { registerHelper } from 'discourse/lib/helpers';

var makeBoundHelper = Ember.HTMLBars.makeBoundHelper;

registerHelper('insert_ad', makeBoundHelper(function(params) {
  var dfp_top_3_display = Discourse.SiteSettings.dfp_top_3_display;

  if (dfp_top_3_display < 0) return false;

  return params[0] == dfp_top_3_display;
}))

I’m using this helper in template, in development environment it works well but in docker production it produces an error:

Uncaught Error: Could not find module 'discourse/lib/helpers' imported from 'discourse/plugins/discourse-adplugin/discourse/helpers/eq'

What is the right way to create template helpers in plugin?

This would be because you’re using a different version of Discourse in development and docker production.

This commit moved the helpers from discourse/lib/helpers to discourse-common/lib/helpers:

https://github.com/discourse/discourse/commit/be1d74d20772e3a4ed2b3daed9700249024a133f

So if you update your development environment, and change the first line of your code to:

import { registerHelper } from 'discourse-common/lib/helpers';

then everything should work.

Thanks, that solved the issue.