# 그룹 이름에 특정 이름이 포함되면 해당 그룹 설정 숨기기

**URL:** https://meta.discourse.org/t/hide-certain-group-settings-if-group-name-includes-a-specific-name/237917
**Category:** Development
**Created:** [9월 2, 2022, 8:25오전 UTC](https://meta.discourse.org/t/hide-certain-group-settings-if-group-name-includes-a-specific-name/237917 "2022-09-02T08:25:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [9월 2, 2022, 8:25오전 UTC](https://meta.discourse.org/t/hide-certain-group-settings-if-group-name-includes-a-specific-name/237917/1 "2022-09-02T08:25:48Z")

</div>

그룹에 몇 가지 추가 필드를 포함시키기 위한 커스텀 플러그인을 만들었습니다. 하지만 이 필드들은 이름에 "chapter"가 포함된 그룹에만 적용됩니다. 따라서 다른 용도의 그룹에는 이 필드가 필요하지 않습니다. 예를 들어 `if chapter in group.name; then chapter = true`와 같은 조건을 만들 수 있는 방법이 있을까요?

현재 다음 코드를 사용하고 있습니다:  
[https://github.com/kees-closed/discourse-group-custom-fields/blob/master/assets/javascripts/discourse/connectors/group-edit/field-container.hbs](https://github.com/kees-closed/discourse-group-custom-fields/blob/master/assets/javascripts/discourse/connectors/group-edit/field-container.hbs)

그룹 이름에 'chapter’라는 단어가 없으면 상단의 컨트롤 그룹을 숨기고 싶습니다. 참고한 예제에서 이미 일부 Jinja2 구문을 사용하고 있는 것 같습니다. 만약 이것이 다른 Jinja2와 비슷하다면, 거기에 그런 조건을 만들 수 있을 것입니다. 하지만 그 전에 몇 가지 알아야 할 것이 있습니다.

- Jinja2가 완전히 사용 가능한 경우, 조건을 만들기 위해 그룹 이름 변수에 어떻게 접근할 수 있나요?
- Jinja2가 (완전히) 사용 불가능한 경우, 이 조건을 어떻게 만들 수 있나요? 아마도 다른 JavaScript가 불리언 값을 설정해야 할 것 같습니다. 하지만 이러한 그룹 변수와 JavaScript 로직의 구조는 제가 잘 모르므로, 예제가 있다면 매우 도움이 될 것입니다!

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [9월 2, 2022, 8:27오전 UTC](https://meta.discourse.org/t/hide-certain-group-settings-if-group-name-includes-a-specific-name/237917/2 "2022-09-02T08:27:55Z")

</div>

아 잠깐… 이제 갑자기 테스트할 수 있는 몇 가지 메서드가 보입니다. 여기서 Jinja2가 동작한다면, 실제로 `g.json`에서 그룹 이름이 표시되고 조건에 사용할 수 있겠네요…

아니, 그건 작동하지 않아요. 그리고 이건 Jinja2가 아니라 [Handlebars 특유의 기능](https://getpublii.com/dev/contains-helper/)입니다 (다만 `contains`가 네이티브 함수는 아닌 것 같네요?).

```yaml
{{#contains "chapter" group.name}}
<div class='control-group'>
    <label class="control-label">{{i18n 'admin.groups.my_map.map_title'}}</label>
    {{input type="checkbox" checked=group.custom_fields.show_map}}
    <span>{{i18n 'admin.groups.my_map.map_switch'}}</span>
</div>
{{/contains}}

```

하지만 그건 작동하지 않고, 여러 오류가 발생합니다. 아마도 변수 조회를 올바르게 사용하지 않는 것 같습니다. `group.name`을 단순히 `"chapter_netherlands"`로 교체해도 실패하긴 마찬가지입니다.

```plaintext
오류 발생:

- 렌더링 중:
  -top-level
    application
      discourse-root
        group
          group.manage
            group.manage.profile
              groups-form-profile-fields
                plugin-outlet
                  plugin-connector

Uncaught Error: Attempted to resolve `contains`, which was expected to be a component, but nothing was found.

```

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [9월 4, 2022, 4:28오후 UTC](https://meta.discourse.org/t/hide-certain-group-settings-if-group-name-includes-a-specific-name/237917/3 "2022-09-04T16:28:37Z")

</div>

제길잡이가 되어 주실 분 계신가요? 🤓

---

<div class="post-metadata">

### Author: ![AquaL1te](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aqual1te/32/201966_2.png) [@AquaL1te](https://meta.discourse.org/u/AquaL1te)
#### Post date: [9월 8, 2022, 2:59오후 UTC](https://meta.discourse.org/t/hide-certain-group-settings-if-group-name-includes-a-specific-name/237917/4 "2022-09-08T14:59:35Z")

</div>

그러니까 내가 이 함수들을 직접 만들어야 한다는 걸 이해했습니다. Jinja2처럼 이 작업을 수행하는 데 유용한 내장 기능이 있는 건 아니니까요.

그런데 플러그인에서 이 Handlebars 함수를 어디에 만들어야 하나요? 여기 꽤 많은 예시를 볼 수 있습니다: [Repository search results · GitHub](https://github.com/discourse/discourse/search?q=handlebars)

하지만 이 폴더 구조에서 패턴을 잘 찾지 못하겠습니다. `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가 이 경로를 검색하지 않는 것이거나, 제가 무언가를 잘못하고 있는 것 같습니다.

```js
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 확장자를 붙여 넣어야 한다고 제안하고 있습니다.

> **[A not so simple Discourse Plugin - Handlebars.registerHelper](https://www.sitepoint.com/community/t/a-not-so-simple-discourse-plugin-handlebars-registerhelper/169496)**
>
> Inspired from this discussion https://meta.discourse.org/t/adding-share-buttons-to-every-post-image/27590 I decided to look into what would be involved with creating such a plugin. Expanding on what I’ve learned previously, That is, plugin.rb...

그래서 contains.js를 해당 디렉토리로 이동시키고 확장자를 .e6으로 변경해 보았지만 같은 오류가 발생했습니다. 도움이나 문서가 있으면 좋겠습니다 🙂
