Latest 3 blog articles (Freemarker)
Taken from http://lithosphere.lithium.com/t5/Everything-Studio/Cross-promote-blogs-and-discussion-boards/m-p/19658#M80 with big thanks to Kaela!
Description
Displays the Subject and Teaser (if there is a teaser) of the latest 3 blog articles in the community.
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 text keys that we will use in the component we create:
2a. Go to the Text Editor tab.
2b. Click the Search button.
2c. Enter the following custom text keys for the "posted by" label (include {0} to represent a variable our component will pass in to fill in the screen name of the user who posted the blog article) and for the "read more" link text, then click the save button:
custom.component.latest_three_blog_articles.posted_by = Posted By {0} custom.component.latest_three_blog_articles.read_more = Read More
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 latest-three-blog-articles.
3c. Add the following markup to the component and click the save button:
<#assign messages = rest("/topics/style/blog/recent?page_size=3").messages /> <div class="custom-recent-articles-container"> <#list messages.message as message> <#assign posted_by_userid = message.author.login?string /> <#if posted_by_userid == "Anonymous"> <#assign posted_by_userid = "" /> </#if> <#assign posted_by_label = text.format("custom.component.latest_three_blog_articles.posted_by", posted_by_userid) /> <div class="custom-recent-article-wrapper"> <div class="custom-article-header"> <div class="custom-article-subject"> <a href="${message.@view_href}">${message.subject}</a> </div> <div class="custom-article-posted-by"> <span class="author">${posted_by_label}</span> </div> </div> <div class="custom-article-teaser"> <#if message.teaser?length gt 0> ${message.teaser} <span class="custom-read-more"><a href="${message.@view_href}">${text.format("custom.component.latest_three_blog_articles.read_more")}</a></span> </#if> </div> </div> </#list> </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 "main-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 somewhere on the "main-content" section of the page.
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 main section of the page. It doesn't look styled very well, so we should add some css selectors to style it. Also, we've decided we would like more than just the screen name to display – we'd also like to see the rank of the user and have the user's screen name hyperlink to their profile page. We could write custom logic for this, or we could use a core component named common.widget.user-name that already does this for us. First let's add in the core component, then we'll add some css styling. Let's also add a title for this component, but only have it show if there is at least 1 blog article.
5b. Go to the Components tab and click on the component you created.
5c. Change the markup to something more like this:
<#assign messages = rest("/topics/style/blog/recent?page_size=3").messages /> <div class="custom-recent-articles-container"> <#if messages.message?size gt 0> <div class="custom-recent-articles-heading-bar"> <span class="custom-recent-articles-heading-bar-title">${text.format("custom.component.latest_three_blog_articles.title")}</span> </div> </#if> <#list messages.message as message> <#assign posted_by_userid = message.author.login?string /> <#assign posted_by_label = text.format("custom.component.latest_three_blog_articles.posted_by") /> <div class="custom-recent-article-wrapper"> <div class="custom-article-header"> <div class="custom-article-subject"> <a href="${message.@view_href}">${message.subject}</a> </div> <div class="custom-article-posted-by"> <span class="author">${posted_by_label} <#if ((posted_by_userid != "") && (posted_by_userid != "Anonymous"))><@component id="common.widget.user-name" user="conv:${message.author.login}" /></#if></span> </div> </div> <div class="custom-article-teaser"> <#if message.teaser?length gt 0> ${message.teaser} <span class="custom-read-more"><a href="${message.@view_href}">${text.format("custom.component.latest_three_blog_articles.read_more")}</a></span> </#if> </div> </div> </#list> </div>
5d. We'll also want to go back to the Text Editor tab, add the component title text key, and change the text key value we added for the "posted by label" so it doesn't take a parameter any longer, something like this:
custom.component.latest_three_blog_articles.title = Latest Blog Entries custom.component.latest_three_blog_articles.posted_by = Posted By:
6. Finally, let's add some css to our community skin to style the component:
6a. Go to the Community Style tab
6b. Click on an existing custom skin if you already have one, or click the "new skin" button to create a new skin.
6c. Click on the CSS sub-nav and add the following CSS:
#lia-body .lia-content .custom-recent-articles-container .custom-recent-articles-heading-bar { padding:0 10px 5px; margin:0 0 5px; } #lia-body .lia-content .custom-recent-articles-container .custom-recent-articles-heading-bar-title { line-height:27px; font-size:27px; color:#${color_primary_01}; } #lia-body .lia-content .custom-recent-articles-container .custom-recent-article-wrapper { padding:3px 0; border-bottom: 1px solid #E5E5E5; margin:0 10px 5px; } #lia-body .lia-content .custom-recent-articles-container .custom-recent-article-wrapper .custom-article-subject { font-weight:bold; }
Example
Go to the Code Sample 3 Example Page to see this in action.