56
ENHANCE your WordPress development With TWIG and CLARKSON Jaime Martínez

Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Embed Size (px)

Citation preview

Page 1: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

ENHANCE yourWordPress developmentWith TWIG and CLARKSON

Jaime Martínez

Page 2: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016
Page 3: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Do you know

TWIG?

Page 4: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

TWIG?

Page 5: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

TWIG?Have you worked with

Page 6: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

TWIG?

Page 7: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

So what isTWIG?

Page 8: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

TWIG?Twig is a modern template engine for PHP.

Page 9: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

TWIG?So what isCLARKSON?

Page 10: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Clarkson is a WordPress plugin which encourages writing object-oriented code.

Page 11: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Clarkson is a WordPress plugin which encourages writing object-oriented code.

Page 12: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Clarkson is a WordPress plugin which encourages writing object-oriented code.

Page 13: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016
Page 14: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

What PROBLEMAre we trying to SOLVE?

Page 15: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016
Page 16: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Better (modern) templating

Separation of Concern

Page 17: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Presentation -

View

Logic -

Controller

Data access -

Model

Page 18: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Presentation -

View

Logic -

Controller

Data access -

Model

Page 19: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

How do I magically register variables?

Page 20: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

How do I magically register variables?

Menu item a Menu item b

©WCNL Just some footer text - 2015

Page 21: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

<footer class="content-info">

<?php if ( has_nav_menu( 'footer-menu' ) ) { ?> <nav class="footer-nav"> <?php wp_nav_menu(['theme_location' => 'footer-menu']); ?> </nav> <?php } ?> <?php echo get_option('ll-footer-text') .' - Copyright ' . date('Y');?>

</footer>

get_template_part(‘footer’)

Page 22: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

<footer class="content-info">

{% if ( footer.menu is empty ) %} <nav class="footer-nav"> {{ footer.menu }} </nav> {% endif %} {{ footer.text | raw }} </footer>

footer.twig

Page 23: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

class Footer {

function __construct() { add_filter( 'clarkson_context_args', array( $this, 'add_context_args' ) ); }

function get_text(){ return get_option('ll-footer-text') .' - Copyright ' . date('Y'); }

function add_context_args( $objects ) { $objects['footer']['menu'] = wp_nav_menu(['theme_location'=>'footer-menu']); $objects['footer']['text'] = $this->get_text(); return $objects; } }

footer.php

Page 24: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

How do I set up my TEMPLATES?

Page 25: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016
Page 26: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

single.php > single.twig

Page 27: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

{% include 'defaults/head.twig' %}

<main class="main"> {% block content %} {% include 'partials/content.twig' %} {% endblock %}</main> <sidebar> {% block sidebar %} {% include 'partials/sidebar.twig' %} {% endblock %} </sidebar>

{% include 'defaults/footer.twig' %}

Set up a layout file (layouts/2-columns.twig)

Page 28: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

{% extend 'layouts/2-columns.twig' %}{% block content %} {% include 'post/content.twig' %}

{# Or some other custom HTML #}

{% endblock %}

Extend this layout file [single.twig]

Page 29: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Autotaalglas live! Jaime’s talk on WCNL 2016

Prefix - Level Level

WCNL JAIMEThis is a link and this is regular text

Page 30: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Autotaalglas live! Jaime’s talk on WCNL 2016

Prefix - Level Level

WCNL JAIMEThis is a link and this is regular text

Page 31: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

<div class="grid-cell">

{% block content %} Default fallback content {% endblock %}

</div>

Global partial file [partials/teaser.twig]

Page 32: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

{% extend 'partials/teaser.twig' %}

{% block content %} <script type="text/javascript"> // Some Javascript to load Advertisement </script> {% endblock %}

Extend this partial [internal-ad/teaser.twig]

Page 33: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

How did MODULARCSS help us?

SMACCS / OOCSS / BEM / AtomCSS

Page 34: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Autotaalglas live! Jaime’s talk on WCNL 2016

Prefix - Level Level

WCNL JAIMEThis is a link and this is regular text

Page 35: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Autotaalglas live! Jaime’s talk on WCNL 2016

Prefix - Level Level

WCNL This is a link

Page 36: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Autotaalglas live! Jaime’s talk on WCNL 2016

Prefix - Level Level

WCNL This is a link

grid-cell

1x2

grid-cell

1x1grid-cell

1x1

grid-cell

2x1

Page 37: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Prefix - Level Level

WCNL JAIMEThis is a link and this is regular text

Jaime’s talk on WCNL 2016 Autotaalglas live!

Page 38: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Prefix - Level Level

WCNL This is a link

Jaime’s talk on WCNL 2016 Autotaalglas live!

Page 39: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Prefix - Level Level

WCNL This is a link

Jaime’s talk on WCNL 2016 Autotaalglas live!

grid-cell

1x2grid-cell

1x1grid-cell

1x1

grid-cell

2x1

Page 40: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

<div class="grid-cell grid-cell-1x2 grid-cell-post"> <div class="grid-cell grid-cell-2x1 grid-cell-post"> <div class="grid-cell grid-cell-1x2 grid-cell-partner-post"> <div class="grid-cell grid-cell-1x1 grid-cell-internal-ad">

<div class="grid-cell grid-cell-1x1 grid-cell-post"> <div class="grid-cell grid-cell-1x1 grid-cell-post"> <div class="grid-cell grid-cell-1x2 grid-cell-partner-post"> <div class="grid-cell grid-cell-2x1 grid-cell-internal-ad">

Page 41: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

How to work withCUSTOM POST TYPES

Page 42: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Autotaalglas live! Jaime’s talk on WCNL 2016

Prefix - Level Level

WCNL JAIMEThis is a link and this is regular text

Page 43: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Autotaalglas live! Jaime’s talk on WCNL 2016

Prefix - Level Level

WCNL This is a link

Page 44: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

Autotaalglas live! Jaime’s talk on WCNL 2016

Prefix - Level Level

WCNL This is a link

POST

POSTPARTNER

POST

AD

Page 45: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

POSTCLARKSON

POST

POSTPARTNER AD

Page 46: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

class post extends Clarkson_Post { public function get_grid_size(){ return get_post_meta( $this->get_id(), 'll-grid-size'); } }

themes/wcnl2016/wordpress-objects/post.php

Page 47: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

class ll_partner_post extends post {

public function get_title() { $title = parent::get_title(); $title ="Prefix - " . $title; return $title; } }

themes/wcnl2016/wordpress-objects/ll-partner-post.php

Page 48: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

How do I get myPOST DATA?

Page 49: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

<?php

$post = new Post( $post );

echo $post->get_grid_size();

PHP

Page 50: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

<div class="grid-cell grid-cell-{{ object.get_grid_size() }}"> <a href="{{ object.get_permalink() }}">

{{ object.get_title() | upper }}

</a>

</div>

Twig

Page 51: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

SO:

Page 52: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

SO: Twig templates (include, extend, embed, overwrite blocks)

Page 53: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

SO: Twig templates (include, extend, embed, overwrite blocks)

Magically register variables

Page 54: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

SO: Twig templates (include, extend, embed, overwrite blocks)

Magically register variables

Encourage separation of concern & modularity

Page 55: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

SO: Twig templates (include, extend, embed, overwrite blocks)

Magically register variables

Encourage separation of concern & modularity

OO WordPress Objects

Page 56: Enhance your WordPress development with Twig and Clarkson - WordCamp Nederland 2016

ADIOS! Visit wp—clarkson.com/core by Level Level.

Jaime Martínez

@jmslbam

level-level.com