Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Templating: Eleventy's Superpower

Templating: Eleventy's Superpower

Presented at the first Eleventy Meetup, July 15, 2021.

One of the many cool features of Eleventy is its support of multiple template languages. But what the heck are template languages and how can you use them to make your static Eleventy sites feel like the dynamic, server-rendered sites of yore?

Video: https://www.youtube.com/watch?v=rZyNBd1WgVM

6389097e6acf134606d06217a3c41e47?s=128

Mike Aparicio

July 15, 2021
Tweet

More Decks by Mike Aparicio

Other Decks in Programming

Transcript

  1. TEMPLATING Mike Aparicio 
 @peruvianidol Eleventy's Superpower

  2. None
  3. <html> <head> <title>Mike Aparicio < / title> < / head>

    <body> <div class="header"> <h1><a href="/">Mike Aparicio < / a> < / h1> <ul> <li><b><a href="/">Home < / a> < / b> < / li> <li><a href="/about.html">About < / a> < / li> <li><a href="/blog.html">Blog < / a> < / li> < / ul> < / div> <div class="main"> . . . < / div> <div class="footer"> <p>&copy; 1999 All your rights are belong to us < / p> < / div> < / body> < / html>
  4. <?php include("externals/header.php"); ?> <div class="main"> . . . < /

    div> <?php include("externals/footer.php"); ?>
  5. Markdown (2004) Liquid (2006) HAML (2006) Mustache (2009) EJS (2010)

    Handlebars (2011) Pug (2010) Nunjucks (2012)
  6. Objects/Variables Data storage {{ page.title }} Filters Apply functions to/manipulate

    data {{ page.title | capitalize }} Tags Iterate over/conditionally display data {% if page.title %} 
 ... {% endif %} {% for pages as page %} 
 ... {% endfor %}
  7. Markdown Liquid HAML Mustache EJS Handlebars Pug Nunjucks JavaScript (as

    11ty.js) HTML (as anything!) Eleventy Template Languages
  8. <html> <head> <title>Mike Aparicio < / title> <meta name="description" content="An

    Eleventy demo site."> < / head> <body> <header> <h1><a href="/">Mike Aparicio < / a> < / h1> < / header> <nav> <ul> <li><a href="/">Home < / a> < / li> <li><a href="/about.html">About < / a> < / li> <li><a href="/blog.html">Blog < / a> < / li> < / ul> < / nav> <main> . . . < / main> <footer> <p>&copy; 2021 All your rights are belong to us < / p> < / footer> < / body> < / html>
  9. <html> <head> <title>Mike Aparicio < / title> <meta name="description" content="An

    Eleventy demo site."> < / head> <body> <header> <h1><a href="/">Mike Aparicio < / a> < / h1> < / header> <nav> <ul> <li><a href="/">Home < / a> < / li> <li><a href="/about.html">About < / a> < / li> <li><a href="/blog.html">Blog < / a> < / li> < / ul> < / nav> <main> {{ content | safe }} < / main> <footer> <p>&copy; 2021 All your rights are belong to us < / p> < / footer> < / body> < / html> 📁 _site 📁 _src 📁 _data 📁 _includes base.njk 📁 assets 📁 pages .eleventy.js .gitignore package.json README.md Layouts
  10. 📁 _site 📁 _src 📁 _data 📁 _includes base.njk 📁

    assets 📁 pages home.html .eleventy.js .gitignore package.json README.md Front Matter - - - layout: base - - - <p>Here's the content of our homepage. < / p>
  11. 📁 _site 📁 pages 📁 home index.html 📁 _src 📁

    _data 📁 _includes base.njk 📁 assets 📁 pages home.html .eleventy.js .gitignore package.json README.md <html> <head> <title>Mike Aparicio < / title> <meta name="description" content="An Eleventy demo site."> < / head> <body> <header> <h1><a href="/">Mike Aparicio < / a> < / h1> < / header> <nav> <ul> <li><a href="/">Home < / a> < / li> <li><a href="/about.html">About < / a> < / li> <li><a href="/blog.html">Blog < / a> < / li> < / ul> < / nav> <main> <p>Here's the content of our homepage. < / p> < / main> <footer> <p>&copy; 2021 All your rights are belong to us < / p> < / footer> < / body> < / html>
  12. 📁 _site 📁 _src 📁 _data 📁 _includes base.njk 📁

    assets 📁 pages home.html .eleventy.js .gitignore package.json README.md Permalink - - - layout: base permalink: / - - - <p>Here's the content of our homepage. < / p>
  13. 📁 _site index.html 📁 _src 📁 _data 📁 _includes base.njk

    📁 assets 📁 pages home.html .eleventy.js .gitignore package.json README.md <html> <head> <title>Mike Aparicio < / title> <meta name="description" content="An Eleventy demo site."> < / head> <body> <header> <h1><a href="/">Mike Aparicio < / a> < / h1> < / header> <nav> <ul> <li><a href="/">Home < / a> < / li> <li><a href="/about.html">About < / a> < / li> <li><a href="/blog.html">Blog < / a> < / li> < / ul> < / nav> <main> <p>Here's the content of our homepage. < / p> < / main> <footer> <p>&copy; 2021 All your rights are belong to us < / p> < / footer> < / body> < / html>
  14. 📁 _site 📁 _src 📁 _data 📁 _includes base.njk 📁

    assets 📁 pages about.html home.html .eleventy.js .gitignore package.json README.md - - - layout: base permalink: /about/ title: "About" - - - <p>Here's the content of our About page. < / p>
  15. 📁 _site 📁 _src 📁 _data 📁 _includes base.njk 📁

    assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md Directory Data { "layout": "base", "permalink": "/{{ title | slug }}/" }
  16. 📁 _site 📁 _src 📁 _data 📁 _includes base.njk 📁

    assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md - - - title: "About" - - - <p>Here's the content of our About page. < / p>
  17. <html> <head> <title>Mike Aparicio < / title> <meta name="description" content="An

    Eleventy demo site."> < / head> <body> <header> <h1><a href="/">Mike Aparicio < / a> < / h1> < / header> <nav> <ul> <li><a href="/">Home < / a> < / li> <li><a href="/about.html">About < / a> < / li> <li><a href="/blog.html">Blog < / a> < / li> < / ul> < / nav> <main> <p>Here's the content of our About page. < / p> < / main> <footer> <p>&copy; 2021 All your rights are belong to us < / p> < / footer> < / body> < / html> 📁 _site 📁 about index.html 📁 _src 📁 _data 📁 _includes base.njk 📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md
  18. 📁 _site 📁 _src 📁 _data site.json 📁 _includes base.njk

    📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md Global Data { "name": "Mike Aparicio", "description": "An Eleventy demo site." }
  19. 📁 _site 📁 _src 📁 _data site.json 📁 _includes base.njk

    📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md Tags <html> <head> <title> {{ site.name }}{% if title %} - {{ title }}{% endif %} < / title> <meta name="description" content=" {% if description %} {{ description }} {% else %} {{ site.description }} {% endif %}"> < / head> <body> . . . < / body> < / html>
  20. 📁 _site 📁 _src 📁 _data site.json 📁 _includes base.njk

    📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md - - - title: "About" description: "The About page." - - - <p>Here's the content of our About page. < / p>
  21. 📁 _site 📁 about index.html 📁 _src 📁 _data site.json

    📁 _includes base.njk 📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md <html> <head> <title>Mike Aparicio - About < / title> <meta name="description" content="The About page."> < / head> <body> <header> <h1><a href="/">Mike Aparicio < / a> < / h1> < / header> <nav> <ul> <li><a href="/">Home < / a> < / li> <li><a href="/about.html">About < / a> < / li> <li><a href="/blog.html">Blog < / a> < / li> < / ul> < / nav> <main> <p>Here's the content of our About page. < / p> < / main> <footer> <p>&copy; 2021 All your rights are belong to us < / p> < / footer> < / body> < / html>
  22. 📁 _site 📁 _src 📁 _data nav.json site.json 📁 _includes

    base.njk 📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md Navigation { "items": [ { "url": "/", "name": "Home" }, "url": "/about/", "name": "About" }, "url": "/blog/", "name": "Blog" } ] }
  23. 📁 _site 📁 _src 📁 _data nav.json site.json 📁 _includes

    base.njk 📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md Iteration <html> <head> . . . < / head> <body> <header> <h1>{{ site.name }} < / h1> < / header> <nav> <ul> {% for item in nav.items %} <li><a href="{{ item.url }}" {% if item.url = = = page.url %} aria - current="page" data - state="active" {% endif %} >{{ item.name }} < / a> < / li> {% endfor %} < / ul> < / nav> . . . < / body> < / html>
  24. 📁 _site 📁 about index.html 📁 _src 📁 _data nav.json

    site.json 📁 _includes base.njk 📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md <html> <head> <title>Mike Aparicio - About < / title> <meta name="description" content="The About page."> < / head> <body> <header> <h1><a href="/">Mike Aparicio < / a> < / h1> < / header> <nav> <ul> <li><a href="/">Home < / a> < / li> <li><a href="/about.html" aria - current="page" data - state="active">About < / a> < / li> <li><a href="/blog.html">Blog < / a> < / li> < / ul> < / nav> <main> <p>Here's the content of our About page. < / p> < / main> <footer> <p>&copy; 2021 All your rights are belong to us < / p> < / footer> < / body> < / html>
  25. None
  26. 📁 _site 📁 _src 📁 _data nav.json site.json 📁 _includes

    base.njk 📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md https://11ty.rocks/eleventyjs/dates/#year-shortcode Shortcodes module.exports = function(eleventyConf i g) { 
 eleventyConf i g.addShortcode("year", () = > `${new Date().getFullYear()}` ); };
  27. <html> <head> . . . < / head> <body> <header>

    <h1><a href="/">{{ site.name }} < / a> < / h1> < / header> <nav> <ul> {% for item in nav.items %} . . . {% endfor %} < / ul> < / nav> <main> {{ content | safe }} < / main> <footer> <p>&copy; {{ year }} All your rights are belong to us < / p> < / footer> < / body> < / html> 📁 _site 📁 _src 📁 _data nav.json site.json 📁 _includes base.njk 📁 assets 📁 pages about.html home.html pages.json .eleventy.js .gitignore package.json README.md
  28. 📁 _site 📁 _src 📁 _data 📁 _includes 📁 assets

    📁 blog f i rst - entry.md 📁 pages .eleventy.js .gitignore package.json README.md Markdown - - - title: "Blog Entry #1" layout: base permalink: /blog/{{ title | slug }}/ description: "This is my f i rst blog post." - - - # # {{ title }} Lorem ipsum dolor sit * * amet consectetur * * adipisicing elit. Voluptatibus facilis reiciendis dolores molestias, perferendis dolore odio, quia ratione aliquam vero quo praesentium! Ut quo, [fugiat maiores](https: / / 11ty.dev) ipsum voluptate commodi neque! - Markdown - Liquid - HAML - Mustache - Pug - EJS - Handlebars - Nunjucks Lorem ipsum dolor sit amet consectetur adipisicing elit. Architecto ullam off i ciis sit quo reprehenderit expedita, inventore enim cum consectetur?
  29. 📁 _site 📁 _src 📁 _data 📁 _includes 📁 assets

    📁 blog blog.json f i rst - entry.md 📁 pages .eleventy.js .gitignore package.json README.md Collections { "layout": "blog", "permalink": "/blog/{{ title | slug }}/", "tags": "posts" }
  30. 📁 _site 📁 _src 📁 _data 📁 _includes base.njk blog.njk

    📁 assets 📁 blog blog.json f i rst - entry.md 📁 pages .eleventy.js .gitignore package.json README.md Layout Chaining - - - layout: base - - - <article> {{ content | safe }} < / article>
  31. 📁 _site 📁 _src 📁 _data 📁 _includes base.njk blog.njk

    📁 assets 📁 blog blog.json f i rst - entry.md second - entry.md third - entry.md 📁 pages .eleventy.js .gitignore package.json README.md - - - title: "Blog Entry #3" description: "This is my third blog post." - - - Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cumque animi delectus laboriosam, dicta exercitationem magni vel hic perferendis consequuntur nisi, quasi ut commodi corrupti praesentium ad consequatur at! Dignissimos, nam?
  32. 📁 _site 📁 _src 📁 _data 📁 _includes 📁 assets

    📁 blog 📁 pages about.html blog.html home.html pages.json .eleventy.js .gitignore package.json README.md - - - title: "Blog" description: "The Blog page." - - - <ul> {% for post in collections.posts %} <li><a href="{{ post.url }}">{{ post.data.title }} < / a> < / li> {% endfor %} < / ul>
  33. 📁 _site 📁 blog index.html 📁 _src 📁 _data 📁

    _includes 📁 assets 📁 blog 📁 pages about.html blog.html home.html pages.json .eleventy.js .gitignore package.json README.md <html> <head> <title>Mike Aparicio - Blog < / title> <meta name="description" content="The Blog page."> < / head> <body> . . . <nav> <ul> <li><a href="/">Home < / a> < / li> <li><a href="/about.html">About < / a> < / li> <li><a href="/blog.html" aria - current="page" data - state="active">Blog < / a> < / li> < / ul> < / nav> <main> <h2>Blog < / h2> <ul> <li><a href="/blog/blog - entry-1">Blog Entry #1 < / a> < / li> <li><a href="/blog/blog - entry-2">Blog Entry #2 < / a> < / li> <li><a href="/blog/blog - entry-3">Blog Entry #3 < / a> < / li> < / ul> < / main> . . . < / body> < / html>
  34. Agility CMS Bloomreach Bold (Quintype) Butter CMS Cockpit CMS Contentful*

    Contentstack Core dna CoreMedia Content Cloud Cosmic JS Craft CMS* Crownpeak Dato CMS Directus DNN Evoq Content dotCMS Episerver eZ Platform FirstSpirit (e-Spirit) GraphCMS Ingeniux CMS Kontent.ai (Kentico) Liferay Magnolia CMS Mura Netlify CMS* Prismic.io* Sanity* Scrivito Sitecore Sitefinity Solodev Strapi* Zesty.io An Incomplete List of Headless CMSs https://www.cmswire.com/web-cms/13-headless-cmss-to-put-on-your-radar/ * services Mike actually heard of before preparing this talk, fwiw
  35. Eleventy Resources 11ty.dev 11ty.recipes 11ty.rocks piccalil.li/course/learn-eleventy-from-scratch/ github.com/peruvianidol/11ty-starter github.com/peruvianidol/template-talk Code idol.pe/template-talk

    Slides THANK YOU! Mike Aparicio @peruvianidol mikeaparicio.com