Slide 1

Slide 1 text

Test Drive Twig with Sculpin AKA Get Ready for Theming in Drupal 8

Slide 2

Slide 2 text

● What is Sculpin, and why should I use it? ● How to install Sculpin ● How to build a Sculpin site ● How to do cool things with Twig This Talk

Slide 3

Slide 3 text

Oliver Davies (opdavies) ● Drupaler since 2007 ● Senior Developer at Microserve ● Drupal 7 and 8 core contributor ● Contrib project maintainer ● User group, sprint, conference organiser whoami

Slide 4

Slide 4 text

Oliver Davies (opdavies) ● Sculpin hobbyist ● Twig enthusiast ● Former Jekyll user whoami

Slide 5

Slide 5 text

Sculpin

Slide 6

Slide 6 text

● Static site generator ● Open source ● Written in PHP, based on Symfony components ● CLI tool, based on Symfony Console ● Markdown/HTML/Twig > HTML ● Awesome for small sites, HTML prototypes and learning Twig. What is Sculpin?

Slide 7

Slide 7 text

Why use a static site generator at all? ● Reduces duplication / D.R.Y. ● Better performance compared to dynamic sites ● Faster to develop (maybe?) Why?

Slide 8

Slide 8 text

Drupal 8’s little brother ● Both use YAML and Twig ● Both have content types ● Both have taxonomies ● Both have “fieldable entities” Similarities

Slide 9

Slide 9 text

Getting Started Installation and Configuration

Slide 10

Slide 10 text

$ curl -O https://download.sculpin.io/sculpin.phar $ chmod +x sculpin.phar $ mv sculpin.phar /usr/local/bin/sculpin https://sculpin.io/download/ Install

Slide 11

Slide 11 text

Alternatives ● Download via Composer ● Clone from GitHub ● Use Sculpin Blog Skeleton or Sculpin Minimal ● Use the Ansible role Install

Slide 12

Slide 12 text

Basic Site Structure app/ config/ sculpin_kernel.yml sculpin_site.yml source/ _layouts/ default.html index.md

Slide 13

Slide 13 text

Configure app/config/sculpin_site.yml title: Oliver Davies source/_layouts/default.html {{ site.title }}

Slide 14

Slide 14 text

Configure app/config/sculpin_site_prod.yml imports: - sculpin_site.yml url: http://www.oliverdavies.uk google_analytics_tracking_code: UA-XXXXXX-1

Slide 15

Slide 15 text

Configure source/_layouts/default.html {% if site.google_analytics_tracking_code %} ... {% endif %}

Slide 16

Slide 16 text

app/config/sculpin_kernel.yml sculpin_content_types: posts: permalink: blog/:slug_title/ talks: Content Types

Slide 17

Slide 17 text

app/config/sculpin_kernel.yml sculpin_content_types: posts: permalink: blog/:slug_title/ taxonomies: [ tags, categories ] talks: Taxonomies

Slide 18

Slide 18 text

source/_posts/2015-07-24-drupalcamp-north.md --- title: DrupalCamp North tags: - drupalcamp - drupalcamp-north Taxonomies

Slide 19

Slide 19 text

source/_layouts/post.html --- use: [ ‘tags’ ] --- {% for tag in page.tags %} {{ tag }} {% endfor %} Taxonomies

Slide 20

Slide 20 text

app/config/sculpin_kernel.yml sculpin_theme: theme: opdavies/oliverdavies15 Themes (Experimental)

Slide 21

Slide 21 text

Adding custom functionality. They are a thing. Bundles

Slide 22

Slide 22 text

$ sculpin generate $ sculpin generate --server $ sculpin generate --server --watch $ sculpin generate --env=prod Build

Slide 23

Slide 23 text

Adding Content

Slide 24

Slide 24 text

Simplest Source File source/index.md --- --- # Hello World

Slide 25

Slide 25 text

Simplest Source File output_dev/ index.html

Slide 26

Slide 26 text

--- layout: default title: About --- # Hello World YAML Front Matter

Slide 27

Slide 27 text

--- layout: default title: About foo: bar --- # Hello World YAML Front Matter

Slide 28

Slide 28 text

Twig

Slide 29

Slide 29 text

source/_layouts/default.html {% block content %}{% endblock %} Layouts

Slide 30

Slide 30 text

source/about.html --- {% block body_classes ‘page--about’ %} {% block content %}

Content

{% endblock %} Blocks

Slide 31

Slide 31 text

source/about.md --- {% block body_classes ‘page--about’ %} {% block content %} Content {% endblock %} Blocks

Slide 32

Slide 32 text

source/_layouts/post.html {% extends ‘default’ %} {% block content %}{% endblock %} Template Inheritance

Slide 33

Slide 33 text

source/_partials/talk-listing-item.html
  • {% if talk.link -%}{%- endif -%} {{ talk.title }} {% if talk.link %}{% endif %}
  • Partials

    Slide 34

    Slide 34 text

    source/talks.html
      {% for talk in page.talks %} {% include(‘talk-listing-item’) %} {% endfor %}
    Partials

    Slide 35

    Slide 35 text

    source/_partials/head.html {% if page.meta.og %} {% include "og" with { og: page.meta.og } %} {% endif %} Partials

    Slide 36

    Slide 36 text

    Twig Tips & Tricks

    Slide 37

    Slide 37 text

    Filters {{ foo|upper }} {{ bar|lower }} {{ page.excerpt|replace( { ‘h2’: ‘h3’ } ) }} Tips & Tricks

    Slide 38

    Slide 38 text

    Rendering objects and arrays {{ site.drupalorg.url }} Tips & Tricks

    Slide 39

    Slide 39 text

    Loops {% for meetup in site.meetups %} {% if loop.first %} ... {% endif %} {% endfor %} Tips & Tricks

    Slide 40

    Slide 40 text

    Conditional logic {% if site.foo == ‘bar’ %} ... {% else %} ... {% endif %} Tips & Tricks

    Slide 41

    Slide 41 text

    Conditional logic {% set date = ‘now’|date(‘Y’) %} {% for talk in page.talks if talk.date >= now %} ... {% endfor %} Tips & Tricks

    Slide 42

    Slide 42 text

    Shorthand blocks {% block body_classes %}page--blog page--blog__list {% endblock %} {% block body_classes ‘page--blog page--blog__list’ %} Tips & Tricks

    Slide 43

    Slide 43 text

    Block appending {% block sidebar %} {{ parent() }} {% endblock %} Tips & Tricks

    Slide 44

    Slide 44 text

    Whitespace {% if site.foo == ‘bar’ -%} ... {%- endif %} Tips & Tricks

    Slide 45

    Slide 45 text

    Spaceless {% spaceless %} ... {% endspaceless %} Tips & Tricks

    Slide 46

    Slide 46 text

    Raw output {% raw %} {% if site.foo == ‘bar’ %} ... {% endif %} {% endraw %} Tips & Tricks

    Slide 47

    Slide 47 text

    Links Links

    Slide 48

    Slide 48 text

    Useful Links ● http://bit.ly/sculpin-twig-resources

    Slide 49

    Slide 49 text

    Questions @opdavies