Forum Discussion

qinglau's avatar
qinglau
Mentor
11 years ago

Exclude topic in REST API response

Hello Guys,

 

I am working on a custom componment which will display list of blog articles under one category node. I am using a REST API call to get all topics from the specifc catergory. For example:

 

http://qszwf76546.stage.lithium.com/restapi/vc/categories/id/Blog/topics?restapi_response.style=view.node_message_context

 I create my own array object and try to iterator the response result from my REST API call to exclude the feature topic id. 

 <#assign articles=[]>
	     <#assign totArticles = 4 />
		 <#assign counter  = 1 />
	     <#-- Exclude hero article from blogArticles list-->
         <#assign AllblogArticles = rest("${rootPath}/topics?restapi_response.style=view").node_message_context />
	     <#assign featuredID = latestFeatured(coreNode.id) />
	     
		 <#list AllblogArticles.message as msg>
		      <#if msg.id!=featuredID>   		  		 
			              <#assign articles = articles + [ msg ] />
			  </#if> 
	      </#list>

 

The lastestFeatured is the function that gets the lastest featured id.

 

After my own array is done and exclude the featured topic in my REST API response results. I am using my own arrray "articles" to display my results in HTML . I lmisted it that will only show 4 results in once

 

 <#list articles as msg>
			 	 <li class="tiles-tile">
					<@component id="custom-kpncoblog-article" hero="false" message=msg slim=is_slim/>
			 	 </li> 
				 	 <#if counter  == totArticles>
				         <#break/>
				      <#else>
				       <#assign counter  = counter  + 1 />
				     </#if>   
		 	 </#list>	 	

 

However, i find out that there is an empty content array in my custom array object. It brings me an empty component. 

 

I think it is the something wrong during my own arrray creation process. The if condition code(

		      <#if msg.id!=featuredID>   		  		 

) skips the content of the specific topic , but my own array just create an index for that.

 

12-4-2014 10-12-36.png

 

I am wondering whether someone has a better solution or advice ?

 

Thank you

 

 

5 Replies

  • HaidongG's avatar
    HaidongG
    Lithium Alumni (Retired)
    11 years ago

    Hi qinglau ,

     

    your array initialization looks alright, I tried your code (with minor modification) with my test environment, it is working.  I guess, you may have to focus with the following points to debug

    • what is the value for ${rootPath} in your component?
    • what is the value for latestFeatured(coreNode.id)?
    • any CSS blocking the display?

     

    <#assign rootPath = "/boards/id/my-category-id" />
    
    <#assign articles=[]>
    <#assign AllblogArticles = rest("${rootPath}/topics?restapi_response.style=view").node_message_context />
    
    <#list AllblogArticles.message as msg>
         <#if msg.id!="33333"> 
           <#assign articles = articles + [ msg ] />
         </#if>
    </#list>
    
    <#list articles as msg>
    ${msg.id} <br>
     </#list>

     

  • qinglau's avatar
    qinglau
    Mentor
    11 years ago

    Hi HaiDong,

     

     

    1. rootPath is the current node level. The code is

     

    <#if nodeType == 'board'>
    	 <#assign rootType = "blogs">
    	 <#assign rootPath = "blogs/id/" + rootNodeId/>
    	<#else>
    	 <#assign rootType = "categories">
    	 <#assign rootPath = "categories/id/" + rootNodeId/>
    	</#if>
    	 

     2. latestFeatured(coredNode.id) will return featured topic ID in "init" format.

     

    3. There is no CSS blocking the code.

     

    Is there a way to convert the int value into string value? and then i can compare "featuredID !=msg.id" Currently, i got error said only compare string to string or int to int  error.

     

    Thank you,

    Cheers,

    Qing

  • HaidongG's avatar
    HaidongG
    Lithium Alumni (Retired)
    11 years ago

    Hi qinglau ,

     

    oh, your latestFeatured(coredNode.id) returns a integer? if like that, 

     

    you can do it either 

     <#if msg.id!=featuredID?string> 
    

     or 

    <#if msg.id?int!=featuredID> 

     

  • Hi qinglau ,

     

    Let me just clarify my understanding. You would like to display 4 blog topics  and exclude featured blog article from it. If my understanding is correct , this might be of help.

     

    <#assign featuredThreadId = rest("/blogs/id/cs_blog/threads/featured").threads.thread.id />
    <#assign blogList = rest("/blogs/id/cs_blog/topics").node_message_context />
    <#assign counter = 0 />

    <#assign limit = 4 />
    <ul>
    <#list blogList.message as blog>

    <#if blog.id!=featuredThreadId && (counter< limit-1)>
    <#assign counter = counter?number +1/>

    <li>${counter} ${blog.subject} <br/></li>
    </#if>
    </#list>
    </ul>

     

    The above snippet is under the assumption , there would be only one featured article. If there might be more than one , we would have to change the code a bit. 

    Since I have initialized the counter as 0 , I have checked for limit-1 .

    You can improvise on this.

     

    Hope that helps.

     

    Thanks,

    Sam

  • qinglau's avatar
    qinglau
    Mentor
    11 years ago

    Hi Guys, 

     

    thank you for the replying. During few days testing, i found out that two main issues in my current solutions. 

     

    1. When i am build the list of articles via my REST API call: 

     

             <#assign AllblogArticles = rest("${rootPath}/topics?restapi_response.style=view").node_message_context />

     

    The limit of this REST API call is 25 articles. Since we have more than 25 articles in our blog.

     

    2.  Next step, i create an new array  and stores all the articles into an array

     

     <#assign articles=[]>

     


    <#list AllblogArticles.message as msg> <#if msg.id!=featuredID> <#assign articles = articles + [ msg ] /> </#if> </#list>

     

    If there are plenty of users accessing our blog, we will have memory issues. 

     

    Still looking for a better way. 

     

    Cheers,
    Qing