Adding meta to the document_head file based on the current page | The place for Zendesk users to come together and share
Skip to main content
Question

Adding meta to the document_head file based on the current page

  • August 26, 2021
  • 5 replies
  • 0 views

Hello,

I would like to add a meta to the document_head file but just for a particular page.

We were doing the process using JS but we need this during render time.

Through the documentation, I saw we have many properties available, but it depends on which template you are.

Is there a way to use the same concept in the document_head file? Something like:

{{#if current_page 'the_page_I_need'}}
<meta content='noindex' name='robots' />
{{/if}}

Thanks in advance! 

5 replies

Greg29
  • Employee
  • August 26, 2021

Hi Italo! I tried something out here and it seemed to work for me without issue, so I'll share what I did, along with links to the relevant docs. I wasn't doing anything to the meta and I don't know of a good way to test it in real time, so if you have any advice on that front, please share that!

Because the `if` statement is baked into Handlebars, it appears to be too clunky to evaluate anything other than top level object itself, as opposed to any of the object's properties. For that reason, I tried this out with an `is` statement instead and it gave me the results that I was going for and hopefully will help you:

{{#is article.title 'Taking the Hobbits to Isengard'}}
// meta goes here
{{/is}}

You don't have to use `article.title` either, you can use any of the article's properties listed here. Let us know if this works out for you!


  • Author
  • August 27, 2021

Hi Greg! Thanks for your answer but I'm afraid it doesn't apply to my problem.

In the document_head template file, I don't have access to a property to check the current page in order to show or not the meta. This code of yours works just if you are in an article template.

To test the meta, actually, just see if it appears in your document tree. Or in your Google Console Tool.


  • August 30, 2021

Yes, that's the answer I wanted to know to thank you very much.


Greg29
  • Employee
  • August 30, 2021

Hi Italo, thanks for clarifying that for me! Unfortunately there would not be a way to do that since we don't have any global helpers for articles and there are no specific helpers that we have for the document head template. 

While this is not a great suggestion at all, the best advice that we've previously given is to utilize User Segments on articles that you don't want crawled, as they will be behind authentication. The obvious downside here being if you don't want the content behind authentication, it creates new problems.

I wish I had better news here!


  • September 9, 2021

Tes, in agreement