Forum Discussion

Myko_P's avatar
Myko_P
Expert
7 years ago

A custom component that shows 3 last posts selected from all the community blogs

Hi,

I am trying to build custom component/widget for the main page. The idea is to show our users in a visual manner 3 last posts selected from all the community blogs.

Selected blog posts should be sorted by the time of publication in descending order. I want also to show the post subject, post cover image, link to the post, post time, post author's nickname and avatar for each of the 3 selected blog posts.

Could you please take a look at my code below:

<#--  SELECTS DATA AND ASSIGNING VARIABLES -->
<#assign messageId = rest("2.0","/search?q=" + "SELECT id FROM messages WHERE board.id in ('Help', 'Blogs') AND depth = 0 ORDER BY post_time DESC LIMIT 3"?url).data.items />
<#assign subject1 = rest("/messages/id/${messageId[1].id}") />
<#assign subject2 = rest("/messages/id/${messageId[2].id}") />
<#assign subject2 = rest("/messages/id/${messageId[3].id}") />

<#assign cover1 = rest("messages/id/${messageId[1].id}/images") />
<#assign cover2 = rest("messages/id/${messageId[2].id}/images") />
<#assign cover3 = rest("messages/id/${messageId[3].id}/images") />

<#assign link1 = rest("/messages/id/${messageId[1].id}?restapi.response_style=view") />
<#assign link2 = rest("/messages/id/${messageId[2].id}?restapi.response_style=view") />
<#assign link3 = rest("/messages/id/${messageId[3].id}?restapi.response_style=view") />

<#assign post_time1 = rest("/messages/id/${messageId[1].id}?restapi.response_style=view") />
<#assign post_time2 = rest("/messages/id/${messageId[2].id}?restapi.response_style=view") />
<#assign post_time2 = rest("/messages/id/${messageId[3].id}?restapi.response_style=view") />

<#assign author1 = rest("/messages/id/${messageId[1].id}?restapi.response_style=view") />
<#assign author2 = rest("/messages/id/${messageId[2].id}?restapi.response_style=view") />
<#assign author3 = rest("/messages/id/${messageId[3].id}?restapi.response_style=view") />

<#--  SHOWS SELECTED DATA ON THE MAIN PAGE  -->
<#--  Shows Link and subject of the blog message #1  -->
<a href="${link1.message.@view_href}">${subject1.message.subject}</a>

<#--  Shows Cover Image of the blog post #1  -->
<img src="${cover1.images.image.url}"/>

<#--  Shows Post time of the blog post #1  -->
${post_time1.message.post_time.@view_date}

<#--  Shows Author's avatar of the blog post #1  -->
<img src="${author1.userAvatar.image.url}"/>  

<#--  Shows Author's login of the blog post #1   -->
${autthor1.author.login.@view_date}

PS Is it possible to select from all the community's blogs without addressing every single blog (board) ID?

Thank you!

  • Myko_P

    After seeing your code looks like you are not very much familiar with the Lithium coding.

    Use below code to fetch all the data for messages from community blogs:

    <#assign messagesData = rest("2.0","/search?q=" + "SELECT * FROM messages WHERE conversation.style='blog'  AND depth = 0 ORDER BY post_time DESC LIMIT 3"?url).data.items />
    <#list messagesData as msgs >
    ${msgs.subject}
    </br>
    </#list>

    Above example I have added to show the subject only. Same you can use to show the other information from API. like author, url,avatar etc. And for images in message you need to add another API in the list by passing the id for message to it.

    "SELECT * FROM images WHERE messages.id = 'msgs.id'"

    and use it again in the list to fetch all the images from message.

     

    • Myko_P's avatar
      Myko_P
      Expert

      Thank you, Parshant ,

      You are right, I am not very much familiar with the Lithium coding. It is actually not  easy to learn some practically useful things from the documentation sections in the Lithosphere. I mean I miss code examples not the glossaries and tables with the key/values and their definitions.


      I am a little bit confused by the things like:

      ${msgs.subject}

      As I can see there is no such key "msgs" in the API 2.0 Collections glossary. But there is key "messages". Is the glossary out of date or I missed something?

      Could you please suggest me some practical tutorials/video lessons explaining how to use LiQL together with the FreeMarker to customize common community components like for example Latest Blog posts?

      Thank you!

      • Parshant's avatar
        Parshant
        Boss

        Myko_P

        If you check my code.

        <#list messagesData as msgs >
        ${msgs.subject}
        </br>
        </#list>

        I have added a <#list> which is basically we use for loop the data to listing a sequence (or collection) of items, and to list the key-value pairs of a hash.

        in above code you can see I have added

        <#list messagesData as msgs >

         which list out all the collection in between "messagesData" and then make them to a varibale defined as "msgs" .

         

         

  • Myko_P -  Yes, you can fetch the messages from all the community blogs by using the below query:

    <#assign messageId = rest("2.0","/search?q=" + "SELECT id FROM messages WHERE conversation.style='blog'  AND depth = 0 ORDER BY post_time DESC LIMIT 3"?url).data.items />

     

    Thanks

    • Myko_P's avatar
      Myko_P
      Expert

      Thank you, Payal !

      Could you please take a look at my code above? Is it OK or I missed something?

      • Payal's avatar
        Payal
        Director

        Myko_P - You can fetch all the required data by using  v2 Query only and there is no need to use rest v1 API call:

        <#assign messageId = rest("2.0","/search?q=" + "SELECT * FROM messages WHERE conversation.style='blog'  AND depth = 0 ORDER BY post_time DESC LIMIT 3"?url).data.items />

        replace 'id' by '*' and then use <#list> to display all the required messages.

        Thanks,

        Payal