Recent searches


No recent searches

複数あるセクション情報の中で1つだけを取得する方法について



Posted Nov 11, 2022

記事情報を取得する方法について質問です。

 

現在顧客に公開するページを刷新しており、ジャンルごとに記事の一覧を表示するページを作成中です。

しかし、セクションの情報を取得し表示するページがうまく実現できていません。

 

やりたい事

・複数あるセクションの情報の中で1つだけを取得したい

試したこと

・下記URLを元に、homeページでCategoryの情報を取得しループによってセクション、サブセクション全ての情報を取得できることは確認できました。

※{{#each categories}}の中で{{#each sections}}をループするやり方は確認できた。

https://developer.zendesk.com/documentation/help_center/help-center-templates/home_page/

 

現状の認識

現在データ構造の認識は下記のとおりです。

質問としては下記のsub section2の情報を直接取得できるようなやり方はあるのかが知りたいです。

categories{
      sections{
     section1{
      sub section1{
name,
url,
...
},
      sub section2{},
      sub section3{},
      ...
     },
     section2{},
     section3{},
     ...
     }
}

期待している回答

ループではなく直接取得をしたいので、{{categories.sections.subsectino2}}のようなやり方で取得できないのかを期待しています。

そもそも実現可能かの判断もつけられない状態で困っております。

データ構造の認識が間違っている、この記事が参考になる等でも構いませんので、ご存知の方がいらっしゃったらご教授いただきたいです。

 

よろしくお願いします。


0

2

2 comments

あまり美しくはないですが、eachの中でbuilt-inのifかisを使って、当該のIDだったら表示するみたいなロジックを組む必要があります。

https://handlebarsjs.com/guide/builtin-helpers.html#if

例えば、下記のサンプルではこのままだとサブセクションが全て表示されてしまうので、isなどで特定のIDであればみたいな形で表示します。コードのテストはしていないので悪しからず。

Ref

- https://support.zendesk.com/hc/en-us/articles/4408883063066-Displaying-subsections-in-your-help-center

- https://developer.zendesk.com/documentation/help_center/help-center-templates/helpers/#is-helper

 

{{#if section.sections}}
  <section class="sections blocks">
    <ul class="blocks-list">
      {{#each section.sections}}
        <li class="blocks-item">
          <a href="{{url}}" class="blocks-item-link">
            <h4 class="blocks-item-title">{{name}}</h4>
            <p class="blocks-item-description">{{excerpt description}}</p>
          </a>
        </li>
      {{/each}}
    </ul>
  </section>
{{/if}}

 

       {{#each section.sections}}
{{#is section.sections.id 'xxxxx'}}
The author of the article is John.
        <li class="blocks-item">
          <a href="{{url}}" class="blocks-item-link">
            <h4 class="blocks-item-title">{{name}}</h4>
            <p class="blocks-item-description">{{excerpt description}}</p>
          </a>
        </li>
{{else}} not available
{{/is}}
    {{/each}} 

2


Toru Takahashi

回答ありがとうございます!

やはりロジックで表示という感じになるのですね・・・試してみます。

実現可能ということがわかっただけでも大変ありがたいです。

本当にありがとうございました!

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post