Help center templating cookbook

Looping through the results

This snippet is an example of how you can use the {{results}} helper to loop through the unified search results.

Replace both the{{article_results}}and{{post_results}}helpers and their associated search-results-subheading H3s

in the search results template with the following:

{{#如果结果}}< ul类= >“search-results-list”{{#each results}} 
  • {{title}}

    {{#if vote_sum}} {{vote_sum}} {{/if}} {{#if comment_count}} {{comment_count}} {{/if}}
    {{text}}
  • {{/each}} {{else}}

    {{t 'no_results_unified'}} {{#link 'help_center'}} {{t 'browse_help_center'}} {{/link}}

    {{/if}}

    Updating CSS styling for the sidebar filters in the search results page

    You can add CSS styling for the sidebar filters. The following example is based on the Copenhagen theme.

    Make sure the following CSS rules are included in your style.css file:

    / * * * * * * * * * *搜索结果/ .search-results{说play: flex; flex-direction: column; flex-wrap: wrap; justify-content: space-between; } @media (min-width: 1024px) { .search-results { flex-direction: row; } } .search-results-column { flex: 1; } @media (min-width: 1024px) { .search-results-column { flex: 0 0 75%; } } .search-results-sidebar { border-top: 1px solid #ddd; flex: 1 0 auto; margin-bottom: 20px; padding: 0; } @media (min-width: 1024px) { .search-results-sidebar { border: 0; flex: 0 0 20%; height: auto; } } .search-results-sidebar .sidenav-item[aria-selected="true"] { background-color: $brand_color; color: $brand_text_color; text-decoration: none; } .search-results-subheading { font-size: 18px; font-weight: 600; } .search-results-list { margin-bottom: 25px; } .search-results-list > li { padding: 20px 0; } .search-results-list > li:first-child { border-top: 1px solid #ddd; } .search-results-list > li h2 { margin-bottom: 0; } .search-result-title { font-size: 16px; } .search-result-description { margin-top: 15px; word-break: break-word; } .search-result-votes, .search-result-meta-count { color: lighten($text_color, 20%); display: inline-block; font-size: 13px; font-weight: 300; padding: 4px 5px; position: relative; } .search-result-votes::before, .search-result-meta-count::before { color: $brand_color; } [dir="ltr"] .search-result-votes, [dir="ltr"] .search-result-meta-count { margin-left: 5px; } [dir="ltr"] .search-result-votes::before, [dir="ltr"] .search-result-meta-count::before { margin-right: 3px; } [dir="rtl"] .search-result-votes, [dir="rtl"] .search-result-meta-count { margin-right: 5px; } [dir="rtl"] .search-result-votes::before, [dir="rtl"] .search-result-meta-count::before { margin-left: 3px; } .search-result-votes::before { content: "\1F44D"; } .search-result-meta-count::before { content: "\1F4AC"; } .search-result .meta-group { align-items: center; } .search-result-breadcrumbs { margin: 0; } .search-result-breadcrumbs li:last-child::after { content: "·"; display: inline-block; margin: 0 5px; } /* Non-latin search results highlight */ /* Add a yellow background for Chinese */ html[lang|="zh"] .search-result-description em { font-style: normal; background: yellow; } /* Use bold to highlight for the rest of supported non-latin languages */ html[lang|="ar"] .search-result-description em, html[lang|="bg"] .search-result-description em, html[lang|="el"] .search-result-description em, html[lang|="he"] .search-result-description em, html[lang|="hi"] .search-result-description em, html[lang|="ko"] .search-result-description em, html[lang|="ja"] .search-result-description em, html[lang|="ru"] .search-result-description em, html[lang|="th"] .search-result-description em { font-style: bold; }

    Updating JavaScript for the collapsible sidebar in the search results page

    You can add JavaScript for the collapsible sidebar in the search results page. The following example is based on the Copenhagen theme.

    Replace the following block in the script.js file in the custom theme:

    // Toggles expanded aria to collapsible elements Array.prototype.forEach.call(document.querySelectorAll('.collapsible-nav, .collapsible-sidebar'), function(el) { el.addEventListener('click', function(e) { e.stopPropagation(); var isExpanded = this.getAttribute('aria-expanded') === 'true'; this.setAttribute('aria-expanded', !isExpanded); }); });

    with the following code snippet:

    // Toggles expanded aria to collapsible elements var collapsible = document.querySelectorAll('.collapsible-nav, .collapsible-sidebar'); Array.prototype.forEach.call(collapsible, function(el) { var toggle = el.querySelector('.collapsible-nav-toggle, .collapsible-sidebar-toggle'); el.addEventListener('click', function(e) { toggleNavigation(toggle, this); }); el.addEventListener('keyup', function(e) { if (e.keyCode === 27) { // Escape key closeNavigation(toggle, this); } }); });
    Return to top

    27 Comments

    • Catherine Michalak
      Community Moderator

      Hi,

      Is there anyone who could help me add subsections to my custom HC code?

      I am struggling to get the section with subsections to show if it doesn't have an article in it (ie. the subsections have articles but until there's an article in the section itself, it doesn't show on the category page) and struggling to keep the layout the same on the section with subsections level as it is on the category with sections level.

      TIA!

      Catherine

      0
    • Emelie Stjernquist

      Hello@...,

      Just so I get it right, you wish to display an "empty" section?

      I'm not sure that that is possible to do (at least I've not manage to do that), however I have a suggested work-around which is to:

      1. Create an Article on the Section you wish to display (one that is OK to find if found via search function)
      2. Create a custom Section-template where you modify the html, remove the part that displays/list the articles -Guide from Zendesk help
      3. Edit the Section and assign the new template and publish the Section and Article

      我通常精心制作,测试支持d forth in the templates until I get the result I want. You can also preview it in "Preview mode" before publishing it.

      I hope this helps you to get closer to what you wish to accomplish, good luck!

      //Emelie

      0
    • Francois Spinnael

      The Current_local.name trick isn't working.

      I'm using this list:https://support.zendesk.com/hc/en-us/articles/203761906-Zendesk-language-support-by-product

      And for some reason, the rename of the "language" is working forEnglish (World)that is defined as a "Variant" but all the other ones, aren't working (Estonia, Lithuania, Latvia, Luxembourg,...)

      0
    • Augusto Silva
      Zendesk Engineering

      Hey@...

      Try something like this instead (language names can change):

      {{#contains current_locale.url "/en-us"}}
      true
      {{else}}
      false
      {{/contains}}
      0
    • blak3r

      我的活动“{{请求}}”模板变量on it. The Home Page does not. Is there a simple way to expose the `{{requests}}` to the home page for signed in users?

      I'd like to show them their 5 most recent requests on the homepage so it's more visible.

      If that cannot be done, could a Custom Page do this? Essentially I want a landing page from our product for support that has both their requests and then knowledge base shown under it. One stop landing page from my product. We use SSO so our users are already authenticated when going there from the app.

      1
    • Greg Katechis
      Zendesk Developer Advocacy

      你好,布莱克!我想到这一点看看I could get creative, but there isn't any way that I could envision where this could be accomplished. As you noted, `{{requests}}` don't exist in the home page template and custom pages alsodo not contain that helper. I wish I had better news and if I think of anything, I'll drop you a line here.

      0
    • blak3r

      Greg Katechis- thanks for giving it some thought. I suppose this would need to be accomplished with custom javascript to call an api endpoint we'd host that would then use the REST api and return the data back. Blockers here: 1) simple documentation showing how to add javascript that invokes web requests to a third party server 2) Any tips on how we could authenticate on our server code that the user account is in fact authenticated... like if we had /api/getTickets?user= we need to verify the user is in fact logged into zendesk somehow so they couldn't exploit the api to get other peoples tickets.

      1
    • Greg Katechis
      Zendesk Developer Advocacy

      1) For this, you could just make an AJAX request from the custom code in your help center to initiate this call to your server/middleware. While it isn't the exact scenario that you're looking to accomplish,this articleshould give the basic framework that you would need and then you can just swap out your URL/endpoint.

      2) This one is giving me all sorts of problems...both from a basic implementation standpoint, but especially from a security standpoint. We don't have any method of using OAuth in Guide, which would be the easiest way to accomplish this. You could also use the signed_in helper to confirm that someone is signed in and then grab the CSRF token to do...something clever? Like I mentioned, I don't feel comfortable with this, but maybe the idea would spark something in someone else to a function solution!

      0
    • blak3r

      Thanks Greg. Appreciate the link and the quick response. Maybe I can get a value that is set by the login to uniquely identify the session or something. I’ll hack around. I could provide a token in the return_to portion of the jwt flow. Then modify every page template to store that token in a cookie. And pass that along with server Ajax calls and of course have that same token stored in the user db for checking against. Or maybe just the primary entry point page and have it only work if they redirected there. I’ll sleep on it.

      1
    • Greg Katechis
      Zendesk Developer Advocacy

      Oh that's a great idea if you use JWT! I wouldn't recommend storing the token in the DB, as that will change on each login, but you could use the shared secret from Zendesk to verify the signature on your server. The JTI might cause a problem, although if you're just storing that as a cookie and not doing any authentication/authorization in the traditional sense, this could work.

      Let me know if you get this working or hit any snags with this. This could be a great tip for other users looking for a similar solution!

      0
    • blak3r

      It appears the jwt token is stored in an object in ```Session Storage.z2_sunco_widget_auth.jwt```

      So, in the requests to our server we could include the users email, jwt, and the JTI. The server would take the JTI and resign the payload. If that payload matched the jwt supplied with the request then we could assume that the user was in fact the email provided to the request.

      In terms of how to get the JTI, there are two approaches I see...
      1. Store the JTI in our user table each time a request is built... Con here is possible synchronization issues. For example, we were planning on having links in our product just include the jwt to avoid any perceived glitchy redirect behavior from enduser standpoint. For example to go to /hc/article/103 our link would behttps://subdomain.zendesk.com/jwt?jwt=&return_to=https://subdomain.zendesk.com/hc/article/103

      2. Pass the JTI as a GET param in the return_to and store that to a cookie with custom Javascript code on each page template. CON here is long urls look gross and add potential for user to bookmark a link with a JTI in it... Probably could mitigate this by updating the url.

      3. Get our helpdesk working on a subdomain of our domain. Usehttps://support.mydomain.cominstead of https://.zendesk.com and have our webapp set a cookie to root domain that the zendesk code could also see. Probably the best option.

      Questions for you... is this z2_sunco_widget_auth a reliable key for the JWT token? That sunco part is throwing me off. Also, I'm not a JWT security specialist, I can't see why having the JTI exposed would be that insecure... given you can't sign a payload without the shared secret but maybe i'm missing something. Feel free to DM me personally if you prefer.

      0
    • Maxim Ageev | null | null

      I need to disable search indexing on the zendesk subdomain.
      To only allow it on our company domain.
      I would like to add in header template the following line
      But only if the domain is helpcenter. Is there a variable in a template that stores the current domain?
      https://stackoverflow.com/q/71527073/7921383



      0
    • Katrina Greeves

      Is it possible to filter search results by label?

      e.g

      Article 1 is labelled A
      Article 2 is labelled B
      Article 3 is labelled A C

      I'd like to add a sidebar filter for each label.

      If I select filter A, I will only then see articles 1 and 3.

      0
    • Dave Dyson
      Hi Katrina,

      There isn't currently a way to filter by label, although articles with labels matching the search terms will be featured higher in the search results. If you'd like to suggest this to our product team, please post your use case to ourFeedback - Help Center (Guide)topic, using thistemplate. Thanks!
      0
    • Taehyoung Kim

      Hi, I'm trying to add the content tag feature in our customized template (Copenhagen 1.8.1) but facing some problems.

      I have successfully added content tags in our knowledge base article, but still figuring out how to add them to the search result. I'm basically having a look atsearch_results.hbsof the recent release. But if I add the lines here to my old template, it said that 'content_tag_filters' does not exist. Does the variable name differ from version to version? Is there a way to implement this on Copenhagen 1.8.1?

      Thanks in advance :)

      Taehyoung

      4
    • Beth

      I'm having the same issue asTaehyoung Kimabove. Appreciate any help - thank you!

      3
    • Christian de Heij

      Having the same for my template content_tag_filters does not exists. Any help to get it in?

      1
    • Sabine Hanna

      Same here regarding the content_tag_filters. Any help would be appreciated!

      0
    • mfg

      blak3r- were you able to solve for securely getting the requests on a separate/home page without exposing tickets the authenticated user shouldn't have access to?

      We are looking to have something similar (buttons for the forms at the top, [requests | article list] in a flex box beneath. Without that "{{requests}}" helper, the current solution we're considering is an iFrame / EMBED SRC= , and then style with CSS. It works - but it's not as secure as we'd like.

      0
    • Dane
      Zendesk Engineering
      Hi All,

      The current version of Copenhagen is Theme version 2.19.4. I have tried the content_tag_filters onsearch_results.hbsand it worked without any issues. Unfortunately, as customization is outside our scope the best option is to use the lates version of the Copenhagen Theme.

      0
    • Gorka Cardona-Lauridsen
      Zendesk Product Manager

      Hi All,

      We are conducting research to improve our Help Center developer experience and would love to talk to any of you that have tried to edit Help Center theme code.

      If you are interested please answersign up hereand answer the 3 survey questions. I will reach out to you to setup a 30 min Zoom call.

      The interview will be a semi-structured interview where we would like to hear you take us through examples of what you have done or tried to do with customizations and your experience with the tools, documentation etc. you used.

      Looking forward to talking to all of you.

      Gorka Cardona-Lauridsen
      Sr. Product Manager, Zendesk Guide

      0
    • Tomas M. Parra

      Hello!
      I'm trying to add a snippet of each article's body underneath the articles' title in the category page.
      This is the code as is:


        {{#each articles}}

      • {{#if promoted}}
        http://www.w3.org/2000/svg" width="12" height="12" focusable="false" viewBox="0 0 12 12" class="icon-star" title="{{t 'promoted'}}">


        {{/if}}
        {{title}}
        {{#if internal}}
        http://www.w3.org/2000/svg" width="16" height="16" focusable="false" viewBox="0 0 16 16" class="icon-lock" title="{{t 'internal'}}">



        {{/if}}

      • {{/each}}


      What I want is to add {{body}} underneath {{title}} but I can only print the full article's body, which obviously breaks the whole layout.

      I tried many things like {{body.slice 0 50}} and more, but nothing seems to work.

      For the records: I am a developer, I have been through Zendesk documentation (Plus forums, stack overflow, your support, etc) and nothing seems to work.
      Can you give me a hand to achieve this?

      Thanks!

      1
    • Tomas M. Parra

      By the way, your console says either something like "body.slice cannot be accessed" , or something like "body doesn't accept these parameters".

      1
    • Ryan Gilomen

      Tomas M. Parra,

      I use {{excerpt body characters=150}} on my section pages to achieve this.

      Hope this helps!
      -Ryan

      1
    • Tomas M. Parra

      Hello @Ryan!
      That worked great for me. Thanks so much!

      0
    • c.keefe

      Hey Zendesk & Community! I am trying to use current_locale.name on home_page.hbs to change content based on selected language.

      I have been able to use this in both header.hbs and footer.hbs with no problems, but getting the following error when I try to use it on home_page.hbs

      I am trying to avoid using the Dynamic Content feature if at all possible (just haven't had good luck with it).Thanks!

      0
    • Cory Waddingham

      Is there a way to add metadata for a page? For example, on article pages, I want to add a tag to the page header, so that page crawlers will know the title of the article without any trouble. But I can't find a way to add that page tag, either in the article_page.hbs or the document_head.hbs (adding</p> <pre>{{#if article.title}}<br><title>{{article.title}}
      {{/if}}

      just results in an error.

      0

    Pleasesign into leave a comment.

    Powered by Zendesk