Forum Discussion

DougS's avatar
DougS
Khoros Oracle
14 years ago

Featured thread meta tag (Freemarker)

Description

This component can be used to add a meta tag to the <head> element if the thread being viewed is a featured thread.

 

Requirements

Lithium version 9.18 or higher, Lithium Studio

 

How to add this to a Lithium community

There are two parts to adding a meta tag to the head element for featured threads:

  • Create a component
  • Place the component in the head element

To create the component:

    1. Go to Studio > Components.
    2. Click New Component. Enter a name for the component (for example, featured-thread-meta-tag) and click Create
    3. Add the following markup to the component:

 

<#if page.context.thread??>
  <#assign resp = rest("2.0", "/messages/" + page.context.thread.topicMessage.uniqueId + "?fields=conversation(featured)") />
  <#if resp.data.conversation.featured>
   <meta name="featuredtopic" content="yes" />
  </#if>
</#if>

                            4. Click Save.

To place the element in the <head> tag:

    1. Go to Community StyleWrapper.
    2. Copy the following markup into the "Page Head Content" text area:

 

<#include "featured-thread-meta-tag.ftl" />

                           3. Click Save.

 

            Example

            There is no example page for for this component.

 

NOTE: This message used to contain the following (REST v1) code snippit, but has been replaced with the REST v2 code above because that is more efficient and concise:

 

<#if page.context.thread??>
  <#list rest("/boards/id/${coreNode.id}/threads/featured?page_size=999").threads.thread as thread>
    <#if thread.messages.topic.id?number == page.context.thread.topicMessage.uniqueId>
      <meta name="featuredtopic" content="yes" />
      <#break />
    </#if>
  </#list>
</#if>

 

  • This is a great post, Doug.   So I'm wondering...could I do something similar for solved threads or messages?    We are working on a federating search project and I would like to identify and mark solved threads with a meta tag in a similar manner.

     

    • rwm's avatar
      rwm
      Advisor

      So I think I answered my own question.   Using your example, I changed the REST API call from a featured fetch to one that gets solutions, then once I matched on the thread I looped through the solutions (since there can be more that one) and gathered the IDs into a sequence, then output them into the meta tag like so:

       

      <#if page.context.thread??>
          <#list rest("/boards/id/${coreNode.id}/solutions/recent?page_size=999").threads.thread as thread>
              <#if thread.messages.topic.id?number == page.context.thread.topicMessage.uniqueId>
                  <#assign seqSolutions = []>
                  <#list thread.solutions.solution as solution>
                      <#assign seqSolutions = seqSolutions + solution.id>
                  </#list>
                  <meta name="solutions" content="${seqSolutions?join(",")}" />
                  <#break />
              </#if>
          </#list>
      </#if>

       

      Seems to work well, but if there is a better or more efficient way I would love to know.

       

      Cheers,

      Robert

       

      • DougS's avatar
        DougS
        Khoros Oracle

        That seems like a good approach.  The only optimization I might recommend is to use a LiQL query instead so you can limit the amount of data that needs to be retrieved and processed -- something like this should do the trick:

         

         

        <#if page.context.thread??>
        <#assign query = "SELECT id FROM messages WHERE topic.id = '" + page.context.thread.topicMessage.uniqueId + "' AND is_solution = true" />
        <#assign resp = rest("2.0", "/search?q=" + query?url) />
        <#assign seqSolutions = []>
        <#list resp.data.items as solution>
        <#assign seqSolutions = seqSolutions + [solution.id]>
        </#list>
        <#if (seqSolutions?size gt 0)>
        <meta name="solutions" content="${seqSolutions?join(",")}" />
        </#if>
        </#if>

         

         

        (I would of course also reccomend that you put in proper error handling for your rest v2 calls, as we write about in the Error handling in FreeMarker with Community API v2 article)

         

        -Doug