using rest and freemarker to build components
DESCRIPTION
Using REST and Freemarker to Build Components. “let’s make something”. “I want to show the most recent blog article and when it was posted”. Ok, no problem. First get the message from REST. /topics/style/blog/ recent?page_size =1 And assign that to a freemarker variable - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/1.jpg)
Using REST and Freemarker to Build Components
“let’s make something”
![Page 2: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/2.jpg)
“I want to show the most recent blog article and when it was posted”
Ok, no problem. First get the message from REST. /topics/style/blog/recent?page_size=1And assign that to a freemarker variable
<#assign recent= rest(“/topics/style/blog/recent?page_size=1”).messages />
![Page 3: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/3.jpg)
“No,wait! I only want it to come from my blog. It’s called “Bloggy” and it’s the
best
Still no problem. We’ll just change our REST call to be specific to one board.<#assign recent = rest(“/boards/id/bloggy/topics/recent?page_size=1”).messages />
http://lithosphere.lithium.com/restapi/vc/boards/id/lithiumblog/topics/recent?page_size=1
![Page 4: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/4.jpg)
“What’s that ‘/restapi/vc’ thing?
If you want to see your pure xml response in the browser you need to have “restapi/vc” in the url. If you are making the call in Freemarker, we take care of that part for you.
This:http://lithosphere.lithium.com/restapi/vc/boards/id/lithiumblog/topics/recent?page_size=1
Is the same as this:<#assign myCall = rest(“/boards/id/lithiumblog/topics/recent?page_size=1”) />
![Page 5: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/5.jpg)
Now let’s write out our article title and date
<a href=“${recent.message.@view_href}”> ${recent.message.subject}</a> <span class=“post-time”>${recent.message.post_time}</span>
![Page 6: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/6.jpg)
“What kind of date is that?? I can’t read that!”
Welcome to Bloggy 2013-11-18T17:34:08+00:00
Good point, you can get more user friendly info by adding a parameter to your rest call?restapi.response_style=view
![Page 7: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/7.jpg)
<#assign recent = rest(“/boards/id/bloggy/topics/recent?page_size=1&restapi.response_style=view”).messages />
This changes the response for post time from this:<post_time type="date_time">2013-11-18T17:34:08+00:00</post_time>
To this:<post_time type="date_time" view_date="12-02-2013" view_time="11:13 AM" view_friendly_date="yesterday">2013-12-02T19:13:12+00:00</post_time>
![Page 8: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/8.jpg)
Now we have:
<a href=“${recent.message.@view_href}”> ${recent.message.subject}</a> <span class=“post-time”>${recent.message.post_time.@view_friendly_date}</span>
Welcome to Bloggy Yesterday
Yay, I love it. Let’s test it!
![Page 9: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/9.jpg)
Aaahhhhhh!
![Page 10: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/10.jpg)
What Happened?
We used @view_friendly_date without ever checking that it existed.
![Page 11: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/11.jpg)
“FIX IT!!”<a href=“${recent.message.@view_href}”> ${recent.message.subject}</a> <span class=“post-time”><#if recent.message.post_time.@view_friendly_date[0]??> ${recent.message.post_time.@view_friendly_date}<#else> ${recent.message.post_time.@view_date}</#if></span>
Gives you:
![Page 12: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/12.jpg)
“Yay, it’s perfect”
Happy Dance
![Page 13: Using REST and Freemarker to Build Components](https://reader033.vdocuments.mx/reader033/viewer/2022042720/568163cd550346895dd5106f/html5/thumbnails/13.jpg)
Tips for Development
• Always check the XML first• Test for variables before you use them.
De-FENCE, De-FENCE!• Test as both an admin user and a regular user
- permissions will kill you –• “unit test” • Versions tab is your friend• Think about performance