Forum Discussion

DougS's avatar
DougS
Khoros Oracle
14 years ago

Threads with no replies Skip to end of metadata (Freemarker)

Description

This example shows how to render a list of the 5 most recent threads that have no replies in a custom Freemarker component. This example makes use of a core component that is available for rendering message lists based on a message REST API call. Some important points that this example demonstrates:

  • You can (and should) create custom text keys for all text you want to display in a component – that way you can easily localize for other languages.
  • Many core components can be called from within a custom component by using the <@component> directive.
  • The /search/messages REST API call lets you do a message search using the same query parameters that are used in a normal search through the UI. the openresponse=true query parameter is added to tell the search query to return only messages that have no replies.
  • The use of standard Lithium css classes that start with lia- can allow you to quickly style a component in a way that matches other components on the page.
  • There is a core component with the id forums.widget.message-list-panel that can be used to render a message list when given a valid REST API message list call.

 

Requirements

Lithium version 9.18 or higher, Lithium Studio

 

How to add this to a Lithium community

1.   Navigate to Lithium Studio

 

2.   The first thing we want to do is add a text key that we will pass into our message list component:
2a. Go to the Text Editor tab.
2b. Click the Search button.
2c. Enter the following custom text key and click the save button:

 

 

custom.top_five_threads_with_no_replies.title = Threads with no replies

 

3.  The next thing we will want to do is create our custom component:

3a. Go to the Components tab.
3b. Create a new component – name it whatever you like. I called it top-five-threads-with-no-replies.
3c. Add the following markup to the component and click the save button:

 

 

 

<#assign message_list_title = text.format("custom.top_five_threads_with_no_replies.title") />
<#assign results_list_size = 5 />
<#assign rest_query = "/search/messages?openresponse=true&sort_by=-topicPostDate&page_size=" + results_list_size />
<div class="lia-panel lia-panel-standard top-five-threads-with-no-replies-wrapper">
<@component id="forums.widget.message-list-panel" title="${message_list_title}" messages="rest_v1:"+rest_query style="wide" numMessages="conv:"+results_list_size />
</div>

 
4. Next we want to decide where on the community we want to place the new component we have created. In this example we'll place it only on the community page, in the "side-content" section of the page:

 

4a. Go to the Page tab – the "Community Page" should be selected in the "Page Type" drop-down, but select it if it is not.
4b. In the Components bar on the left, expand "Custom Components" and hover over the component you added, then click the "Add" link to add the component onto the page – the component will appear in the "common-header" section of the page. Hover over your components you just added until you see the 4-directional arrow icon appear then drag and drop the component into the "side-content" section of the page, between the "Admin Links" component and the "Image Moderation" component.
4c. Click the save button at the bottom of the Page tab to save your page changes.

5. Next let's look at our changes so far:

 

5a. Navigate to the community page. You should see the component on the left. If there are not any threads without one or more replies you will not see a message about there being "no posts to display". You will want to add some threads with no replies at this point.
5b. After you have added some threads with no replies, refresh the community page. You will most likely notice that the content of the component bleeds out of the right column – this is because we passed in "wide" to the "style" parameter of the "forums.widget.message-list-panel" component. This would be fine if we were placing the component on the wider left column of the page, but since we are placing it in the right column, we should change it to "slim".
5c. Go to the Components tab and click on the component you created.
5d. Change the "style" parameter of the "forums.widget.message-list-panel" component to "slim" and save – the markup will look like this now:

 

<#assign message_list_title = text.format("custom.top_five_threads_with_no_replies.title") />
<#assign results_list_size = 5 />
<#assign rest_query = "/search/messages?openresponse=true&sort_by=-topicPostDate&page_size=" + results_list_size />
<div class="lia-panel lia-panel-standard top-five-threads-with-no-replies-wrapper">
<@component id="forums.widget.message-list-panel" title="${message_list_title}" messages="rest_v1:"+rest_query style="slim" numMessages="conv:"+results_list_size />
</div>

 

Example

Go to the Code Sample 2 Example Page to see this in action.

 

20 Replies

  • jmurray's avatar
    jmurray
    Champion
    13 years ago
    This thread has been immensely helpful.

    If I wanted to throw this component into a side rail, how would I go about displaying only the topic name instead of having kudos, author, and date in there along with it?
  • AdamN's avatar
    AdamN
    Khoros Oracle
    13 years ago

    The component has a parameter named "style" which is set to "wide". You can change the value to "slim" to condense things down a bit. That sample code is actually at the bottom of Doug's post. Here it is again for easy reference:

     

    <#assign message_list_title = text.format("custom.top_five_threads_with_no_replies.title") />
    <#assign results_list_size = 5 />
    <#assign rest_query = "/search/messages?openresponse=true&sort_by=-topicPostDate&page_size=" + results_list_size />
    <div class="lia-panel lia-panel-standard top-five-threads-with-no-replies-wrapper">
    	<@component id="forums.widget.message-list-panel" title="${message_list_title}" messages="rest_v1:"+rest_query style="slim" numMessages="conv:"+results_list_size />
    </div>

     

  • Agata's avatar
    Agata
    Mentor
    13 years ago

    Hi,

     

    how should the code be modified to exclude one board? I would like to used the first version that shows top 5 threads with no reply for all the boards except one board. Is this possible?

     

    Thanks,

     

    Agata

  • Here's the latest iteration. This widget only shows topics without replies in the current board/category focus:

    <#assign message_list_title = text.format("custom.top_five_threads_with_no_replies.title") />
    <#assign results_list_size = 5 />
    <#assign coreNodeType = coreNode.nodeType />
    <#if coreNodeType == "board">
    	<#assign rest_query = "/boards/id/${coreNode.id}/search/messages?openresponse=true&sort_by=-topicPostDate&page_size=" + results_list_size />
    <#elseif coreNodeType == "category">
    	<#assign rest_query = "/categories/id/${coreNode.id}/search/messages?openresponse=true&sort_by=-topicPostDate&page_size=" + results_list_size />
    </#if>
    <div class="lia-panel lia-panel-standard top-five-threads-with-no-replies-wrapper">
    	<@component id="forums.widget.message-list-panel" title="${message_list_title}" messages="rest_v1:"+rest_query style="wide" numMessages="conv:"+results_list_size />
    </div>

     

  • aguycalledadam's avatar
    aguycalledadam
    Contributor
    12 years ago

    Thanks everyone, great thread.

    I dont seem to be able to get this last set of code to work though.

     

    Keep getting this:

    "Expression rest_query is undefined on line 10, column 109 in preview. The problematic instruction: ---------- ==> user-directive component [on line 10, column 9 in preview]"

     

    I am trying to put an Unanswered threads widget in the sidebar which is board specific.
    Thanks for any and all help

     

  • AdamN's avatar
    AdamN
    Khoros Oracle
    12 years ago

    Do you get that error when you're previewing the component, or when you place it on the page? Based on the error message, it sounds like maybe that error is showing when you preview the component in Studio? If so, it's probably because the component doesn't have the proper context to operate properly. It could probably be changed to be more sensitive to those types of issues and avoid displaying an error, but if you try the component out on a board or category page, you may have better luck than in the preview.

  • Is there a way to add a 'SUBJECT' and 'Posted' header title and a 'View All' link to this Custom Component?

     

    Like shown in the screen shot.

     

    LatestPostsComponent.png

  • How can I pass v2 query to this?
    My query is select * from messages where conversation.solved=false and depth=0 and board.id in ('Developer-Discussion')
  • DougS's avatar
    DougS
    Khoros Oracle
    8 years ago

    To pass a REST v2 call (instead of a REST v1 call), use the "rest_2.0:" prefix instead of the "rest_v1:" prefix. See this post for a code example.