그러니까 내가 이 함수들을 직접 만들어야 한다는 걸 이해했습니다. Jinja2처럼 이 작업을 수행하는 데 유용한 내장 기능이 있는 건 아니니까요.
그런데 플러그인에서 이 Handlebars 함수를 어디에 만들어야 하나요? 여기 꽤 많은 예시를 볼 수 있습니다: Repository search results · GitHub
하지만 이 폴더 구조에서 패턴을 잘 찾지 못하겠습니다. discourse-group-custom-fields/assets/javascripts/discourse/app/helpers/contains.js에 제 함수를 만들었는데, discourse-group-custom-fields/assets/javascripts/discourse/connectors/group-edit/field-container.hbs에서 사용하려고 하면 Uncaught Error: Attempted to resolve "contains", which was expected to be a component, but nothing was found.이라는 오류가 발생합니다. 분명히 Discourse가 이 경로를 검색하지 않는 것이거나, 제가 무언가를 잘못하고 있는 것 같습니다.
import RawHandlebars from "discourse-common/lib/raw-handlebars";
import { htmlSafe } from "@ember/template";
import { rawConnectorsFor } from "discourse/lib/plugin-connectors";
//RawHandlebars.registerHelper("contains", function (args) {
// const connectors = rawConnectorsFor(args.hash.name);
// if (connectors.length) {
// const output = connectors.map((c) => c.template({ context: this }));
// return htmlSafe(output.join(""));
// }
//});
Handlebars.registerHelper('contains', function(needle, haystack, options) {
needle = Handlebars.escapeExpression(needle);
haystack = Handlebars.escapeExpression(haystack);
return (haystack.indexOf(needle) > -1) ? options.fn(this) : options.inverse(this);
});
아래 토픽에서는 초기화자(initializers) 디렉토리에 es6 확장자를 붙여 넣어야 한다고 제안하고 있습니다.
그래서 contains.js를 해당 디렉토리로 이동시키고 확장자를 .e6으로 변경해 보았지만 같은 오류가 발생했습니다. 도움이나 문서가 있으면 좋겠습니다 ![]()