Friday, October 2, 2015

The Genius of Template Strings in ES6

ES6 is the future of JavaScript and it is already here. It is a finished specification, and it brings a lot of features a language requires to stay competitive with the needs of the web of now. Not everything in ES6 is for you, and in this little series of posts I will show features that are very handy and already usable.

If you look at JavaScript code I’ve written, you will find that I always use single quotes to define strings instead of double quotes. JavaScript is OK with either—the following two examples do exactly the same thing:

The reason why I prefer single quotes is that, first of all, it makes it easier to assemble HTML strings with properly quoted attributes that way:

The only time you need to escape now is when you use a single quote in your HTML, which should be a very rare occasion. The only thing I can think of is inline JavaScript or CSS, which means you are very likely to do something shady or desperate to your markup. Even in your texts, you are probably better off to not use a single quote but the typographically more pleasing ‘.

Aside: Of course, HTML is forgiving enough to omit the quotes or to use single quotes around an attribute, but I prefer to create readable markup for humans rather than relying on the forgiveness of a parser. We made the HTML5 parser forgiving because people wrote terrible markup in the past, not as an excuse to keep doing so.

I’ve suffered enough in the DHTML days of document.write, creating a document inside a frameset in a new popup window and other abominations, to not want to use the escape character ever again. At times, we needed triple ones, and that was even before we had colour coding in our editors. It was a mess.

Expression Substitution in Strings?

Another reason why I prefer single quotes is that I wrote a lot of PHP in my time for very large web sites where performance mattered a lot. In PHP, there is a difference between single and double quotes. Single-quoted strings don’t have any substitution in them, whereas double-quoted ones do. That meant back in the days of PHP 3 and 4 that using single quotes was much faster, as the parser didn’t have to go through the string to substitute values. Here is an example of what that means:

JavaScript didn’t have this substitution, which is why we had to concatenate strings to achieve the same result. This is pretty unwieldy, as you need to jump in and out of quotes all the time.

Multi-Line Mess

This gets really messy with longer and more complex strings and especially when we assemble a lot of HTML. And most likely you will sooner or later end up with your linting tool complaining about trailing whitespace after a + at the end of a line. This is based on the issue that JavaScript has no multi-line strings:

Client-Side Templating Solutions

In order to work around the mess that is string handling and concatenation in JavaScript, we did what we always do—we wrote a library. There are many HTML templating libraries, with Mustache.js probably having been the seminal one. All of these follow their own, non-standardized syntax and work in that frame of mind. It’s a bit like saying that you write your content in Markdown and then realizing that there are many different ideas of what “Markdown” means.

Enter Template Strings

With the advent of ES6 and its standardization, we can rejoice as JavaScript now has a new kid on the block when it comes to handling strings: Template Strings. The support of template strings in current browsers is encouraging: Chrome 44+, Firefox 38+, Microsoft Edge and WebKit are all on board. Safari, sadly enough, is not, but it’ll get there.

The genius of template strings is that it uses a new string delimiter, which isn’t in use either in HTML nor in normal texts: the backtick (`).

Using this one we now have string expression substitution in JavaScript:

The ${} construct can take any JavaScript expression that returns a value. You can, for example, do calculations, or access properties of an object:

That last example also shows you that multi-line strings are not an issue at all any longer.

Tagged Templates

Another thing you can do with template strings is prepend them with a tag, which is the name of a function that is called and gets the string as a parameter. For example, you could encode the resulting string for URLs without having to resort to the horridly named encodeURIComponent all the time.

This works, but relies on implicit array-to-string coercion. The parameter sent to the function is not a string, but an array of strings and values. If used the way I show here, it gets converted to a string for convenience, but the correct way is to access the array members directly.

Retrieving Strings and Values From a Template String

Inside the tag function you can not only get the full string but also its parts.

There is also an array of the raw strings provided to you, which means that you get all the characters in the string, including control characters. Say, for example, you add a line break with \n. You will get the double whitespace in the string, but the \n characters in the raw strings:

Conclusion

Template strings are one of those nifty little wins in ES6 that can be used right now. If you have to support older browsers, you can of course transpile your ES6 to ES5; you can do a feature test for template string support using a library like featuretests.io or with the following code:

Here are some more articles on template strings:

More Hands-On With JavaScript

This article is part of the web development series from Microsoft tech evangelists on practical JavaScript learning, open-source projects, and interoperability best practices, including Microsoft Edge browser and the new EdgeHTML rendering engine

We encourage you to test across browsers and devices including Microsoft Edge—the default browser for Windows 10—with free tools on dev.modern.IE:

In-depth tech learning on Microsoft Edge and the Web Platform from our engineers and evangelists:

More free cross-platform tools and resources for the Web Platform:


by Chris Heilmann via Tuts+ Code

Thursday, October 1, 2015

Snackable Satisfaction: Bite-Sized Content Tastes Even Better On Mobile - #infographic

Snackable Satisfaction: Bite-Sized Content Tastes Even Better On Mobile - #infographic

The instant gratification of a snake is undeniable, and it's no difference in content marketing. Snackable content is perfect for on-the-go consumption: Readers can see it, scan it and share it quickly. As social app adoption increases, so do opportunities for brands to use bite-sized content formats to feed large audiences.

As a digital marketer in today’s “24/7 connected,” mobile, and social media world, you need to make your content available, easy to absorb, and totally shareable. And this infographic from Marketo illustrates how to do this:

by Irfan Ahmad via Digital Information World

Introduction to Rendr

Isomorphic JavaScript frameworks and libraries have gained a lot of attention recently. Isomorphic JavaScript applications are applications written in JavaScript that can run both on the client and on the server. Because of this, you can write the code once and then execute it on the server to render static pages and on the client to allow for fast interactions.

In this article, we’ll be exploring and getting started with [Rendr](http://ift.tt/1CnxoaH), an open-source library developed by the [Airbnb](https://www.airbnb.com/) team. The library was initially built with the goal of powering the company’s mobile apps running on [Backbone.js](http://backbonejs.org/) and [Node.js](https://nodejs.org/en/). Later the company made it an open source project and this let the project gain a lot of traction.

## What's Rendr

The basic idea behind Rendr is to allow rendering Backbone.js applications on the client and the server by writing common code for both the ends. This allows web page content to be rendered via the server, way before JavaScript code gets executed in the browser. Once the initial page load is done and the browser has all the client side framework loaded, further routing of the application will be handled by Backbone.js on the client side. Rendr is not intended to be a complete framework and it has the following design goals:

* Writing an application logic which is irrespective of the environment
* Reducing the `if(server) { ... } else { ... }` structures
* Communicating with RESTful APIs as any other Backbone application would do
* Hiding library complexities
* Excluding a server-side DOM

In this tutorial we’ll explore a simple Rendr application, with a GitHub browser that fetches data of repositories and users via the GitHub REST API. This small application is provided as part of Rendr's sample examples. [You can download these examples from this GitHub repository](http://ift.tt/1M4BZMZ). Here, we’ll be exploring the basic "00_simple" application.

[author_more]

Before deepening the topic, let's understand what our application would look like. It would have the following sections:

* **Repos List View:** The repositories' list section would fetch and list some GitHub repositories
* **Repo View**: Clicking on a specific repository, it opens its information page
* **Users List View**: The users' list section fetches and lists the GitHub users
* **User View**: Clicking on a specific user, it opens up the user profile view with some basic user details and their repositories

Continue reading %Introduction to Rendr%


by Ashish Trivedi via SitePoint

All You Need to Know About the New WordPress Site Icon API

With the release of version 4.3, WordPress introduced Site Icons, which allows users to define an icon that represents their website. As often occurs, this new concept has an associated API.

This article will delve into Site Icons, beginning by defining what they are. Then, we will play with this brand new API, which is essentially composed of four functions and two filters. This doesn’t sound like much, but you'll see that it is definitely enough!

What Is a Site Icon?

Good news: if you've worked with the web for a while, then you probably already know what a Site Icon is. In fact, it is just the name WordPress has given to an icon we all know well: the favicon.

A favicon is the image displayed on the browser's tab when your website is opened. In addition, when you save a shortcut to a webpage on the home screen of your smartphone, your mobile OS uses your favicon so that the same generic icon is not displayed for every website.

Once set up, the Site Icon will also be displayed on your administration panel, which was, until now didn't have a favicon.

By defining a Site Icon, you therefore define your website's favicon. The good news is that, as a user or a theme developer, you don't have to do anything to activate the feature. In fact, the display of the Site Icon is activated by default in WordPress 4.3: the call for the right function is done when the wp_head action occurs and every theme must call this action.

This means one thing: if you want to try what we do in this article, you can do it without changing your current theme. The only thing you must do before following this tutorial is update your WordPress installation to version 4.3 (see our article about updating your WordPress installation if you haven’t already done so.

How To Define a Site Icon

Defining a new Site Icon can be achieved by using the Theme Customizer. You have two ways to access this customizer:

  • By using the administration menu, the customizer is located in the ‘Appearance’ menu, with the second entry named ‘Customize’
  • By hitting the ‘Customize’ button in the administration bar, which appears at the top of every page of your website once you are logged in (this button is new to WordPress 4.3)

theme customizer

The Theme Customizer consists of a preview of your website, with a sidebar on the left allowing you to customize some details. For the purposes of this tutorial, we're interested in the ‘Site Identity’ menu option.

Continue reading %All You Need to Know About the New WordPress Site Icon API%


by Jérémy Heleine via SitePoint

Super Easy Activity Feeds with Stream

height="546" class="aligncenter size-full wp-image-115876" />

Previously I have written about how to create Activity Feeds in Rails using the public_activity gem. Today, I am going to introduce you to Stream, a platform providing an API to build complex scalable feeds with ease. Together, we will create a demo application and integrate it with Stream.

Stream is free up to 3 million updates per month. It has multi-region support and real-time updates. There are clients for Ruby, Python, Javascript, PHP, Java, C#, Scala, and Go, as well as integrations for the Rails, Django, and Laravel frameworks.

Currently this platform is actively evolving ($1.75 million was raised recently) and you certainly should give it a try.

A working demo is available at sitepoint-stream.herokuapp.com.

The source code for this post can be found on GitHub.

Continue reading %Super Easy Activity Feeds with Stream%


by Ilya Bodrov-Krukowski via SitePoint

Watch: Using Webpack to Transform JSX

JavaScript processing can be cumbersome and confusing. One of the major objections people have with React is JSX' need to be pre-processed and packaged up for the browser. Fear not! The Webpack module builder and Babel.js were created for exactly this purpose.

In this screencast we'll setup a simple Webpack project and send you on your way to making the next big thing!

Loading the player...

Continue reading %Watch: Using Webpack to Transform JSX%


by Michael Chan via SitePoint

Histography

Histography is an Interactive timeline of all of history (spans across 14 Billion years) From the big bang to 2015, while constantly updating with new events from wikipedia.


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery