Monday, September 7, 2015

Build APIs with Napa

APIs are one of the hottest topics in web development. With the increasing number of SPAs (Single Page Applications) and the microservice craze, APIs will become even more popular. As Ruby on Rails developers, we have an amazing framework at our disposal, very capable of building APIs. Though Rails is powerful, it is more complex as well. Sometimes we need a simpler, more nimble framework, one that only does one thing and does it well. I think I have found such a framework. Allow me to introduce you to a framework that is specifically designed for building APIs: Napa.

Napa combines gems like Grape, Roar, and ActiveRecord, allowing us to build powerful APIs with minimal effort. In this tutorial, we will learn how to build an API with token authentication using Napa and Devise.

Continue reading %Build APIs with Napa%


by Devdatta Kane via SitePoint

Skokov Profit

opl-small

'Skokov Profit' is a free One Page HTML template suited for digital agencies. This freebie was designed by Skokov Design and coded up by Matthew Hartman - cheers guys. It comes with the standard agencies sections but I really like the unique design of the news section. The PSD of the design is also available free here.

by Rob Hope via One Page Love

Updates to the service worker cache API


by Jake Archibald via HTML5Rocks

Rytter & Rytter

Rytter & Rytter — Art in Architecture & Landscape

Long One Pager showcasing the architecture, landscape, painting and sculpting work of 'Rytter & Rytter'. The course section arrangement feels too "texty" but overall a clear presentation of their work and team. Cheers for the build notes.

by Rob Hope via One Page Love

Mastering WP_Query: Using the Loop

As I outlined in the introduction to this series, the WP_Query class has four main elements:

  • the arguments for the query, using parameters which will be covered in detail in this series
  • the query itself
  • the loop, which will output post content, titles or whatever you want to display
  • finishing off: closing if and while tags and resetting post data

In this tutorial I'll show you how to use the loop with WP_Query, including the two main ways to structure your loop and how to use multiple loops.

Where the Loop Fits

Without a loop, nothing will be displayed on your page. After WordPress has run the query, using the arguments you've defined, it then needs to be told what to output from the data that it's fetched. This is where the loop comes in.

So the loop comes after your query, and it uses three tags:

  • if( $query->have_posts() ) checks if there are any posts.
  • while( $query->have_posts() ) repeats the loop for each post as long as there are posts to retrieve.
  • $query->the_post() accesses that specific post.

So this is where the loop fits in the WP_Query class:

After running the loop, all that's left to do is tidy things up using wp_reset_postdata().

Structure of the Loop

The way your loop is structured will depend on what data you want to display from your post. Here's an example loop which outputs the post title, featured image and an excerpt. You'd use a loop like this on an archive page.

This loop displays exactly what I've described above: the featured image, title and excerpt.

Taking the Loop Further: Checking For Content

But sometimes you might want to add a heading before your list of posts, or you might want to enclose them all in a containing element. If you simply added this before your loop, it would be output regardless of whether the query actually returned any data, meaning you could have a heading with nothing beneath it, or some unnecessary markup.

This is very easy to get around by putting the enclosing element or the heading inside your if tag:

Here you can see I've checked if there are any posts retrieved by my query, and if there are I've opened a containing element and added a heading. 

This is also useful if you want to output the results of your query as a list. Let's say I want to create a list of all posts in a given category. The ul element isn't inside my loop as it doesn't relate to one specific post, but I only want to output it if there are posts. So I use this:

This checks if the query has fetched any posts, and if so, it opens the ul element and then runs the loop.

Running Extra Loops

It's important to be aware that while you can use WP_Query to run more than one loop, you have to reset the post data and start a second instance of WP_Query to do this. This is because each of your loops will be outputting data based on different arguments.

This example displays the excerpt and featured image for the first post and then just the title of each subsequent post:

I've used two key arguments here:

  • 'posts_per_page' => '1', used with the first query, outputs just the most recent post.
  • 'offset' = '1', used with the second query, skips the first post, ensuring it's not repeated in the list below.
  • As you can see from the code above, the loop is slightly different for each query. The first one outputs the featured image, title and excerpt, while the second checks if the query has posts and if so, opens a ul element and encloses each post title in a li element and a link to its page.

You'll also notice that I used wp_reset_postdata() after both loops. If I hadn't done this, the second loop would still output data from the first.

Summary

Without a loop, WP_Query doesn't really do very much. The loop is the code you use to display the data that WordPress has fetched from the database based on your query arguments.

As I've demonstrated, there are a few variations on the loop. A simple loop will just output all posts in the order you've specified in your query arguments (or by date in descending order by default). If you separate if( $query->have_posts() ) and while( $query->have_posts() ), you can insert additional markup outside your loop, but only if your query has returned data. And finally, by specifying alternative arguments and using wp_reset_postdata() after each loop, you can use WP_Query more than once to create multiple loops on your page.


by Rachel McCollin via Tuts+ Code

NBA Face Off

opl-small

Neat load transitions and infographics in this One Pager seeking out who is the Greatest NBA player of all time. Also a smart marketing effort to promote basketball betting on Bwin.

by Rob Hope via One Page Love

Wildflower

opl-small

'Wildflower' is an experimental One Pager by Resn digital agency where you can create and share a virtual flower online.

by Rob Hope via One Page Love