JavaScript, Part 3 Creating Content with Templates

Here are some examples with a bit of annotation that I hope are helpful to you in your JavaScript adventures!
— David

A review of iteration/loops

Iterating over an Array and iterating through keys in an Object are illustrated at:

Creating Content with DOM and innerHTML Examples

Here, we get started by iterating through data structures, and using either DOM methods to create content or using "string" values for our content and using innerHTML to add those to the page.

Handlebars Template Examples

These examples use the Handlebars template system, and I am loading the Handlebars library via the cdnjs, a free and open source CDN (content delivery network).

{
    "apples" : [
                  "Granny Smith",
                  "Macoun",
                  "Empire",
                  "Honey Crisp",
                  "Albemarle Pippin"
                ],
    "oranges" : [
                  "Naval Orange",
                  "Tangelo",
                  "Clementine",
                  "Valencia Orange"
                ]
}

Other Template Systems

Let's see how these examples look with other template systems, such as Nunjucks templates.

Nunjucks Template Examples

Nunjucks is another popular JavaScript template system.

{
"apples" : [
                "Granny Smith",
                "Macoun",
                "Empire",
                "Honey Crisp",
                "Albemarle Pippin"
            ],
"oranges" : [
                "Naval Orange",
                "Tangelo",
                "Clementine",
                "Valencia Orange"
            ]
}

Liquid Template Examples

Liquid templates are similar in syntax to Nunjucks. Liquid is an open source project that created and used by the Shopify, a popular e-commerce platform.

Below are the same examples as above, but implemented with Liquid templates.