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

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.

Mustache Template Examples

{
    "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.