theme preprocess functions: an introduction (drupalcamp nyc 10 2011)

42
Theme preprocess functions: an introduction DrupalCampNYC December 10th, 2011

Upload: c4rl

Post on 06-Jul-2015

2.441 views

Category:

Technology


2 download

DESCRIPTION

Slides from http://drupalcampnyc.org/sessions/theme-preprocess-functions-introduction

TRANSCRIPT

Page 1: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Theme preprocess functions: an introduction

DrupalCampNYCDecember 10th, 2011

Page 2: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Carl Wiedemann

Page 3: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

tCarl WiedemannI live here in NYC...

Page 4: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Carl Wiedemann...originally from Denver

Page 5: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)
Page 6: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

RIP

Page 7: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)
Page 8: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)
Page 9: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

c4rl

...on various channels

Page 10: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Preprocess in Theory

Page 11: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Theme Dilemmas

7!7!7!7!

Page 12: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

What do we do if this is insufficient?

<div class="content"> <?php print $content; ?></div>

Page 13: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

How can we create more useful content?

<div class="content"> <?php print $my_content; ?></div>

Page 14: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

<div class="<?php print $my_classes; ?>"> <?php print $my_content; ?></div>

How can we keep our theme contextual?

How can we create more useful content?

Page 15: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Preprocess

Page 16: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Preprocessmakes content

flexible

<?php print $my_content; ?>

Page 17: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Preprocessmakes theming

dynamic

class="<?php print $my_classes; ?>"

Page 18: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Preprocesskeeps templates

clean

if else foreach while switch =

Page 19: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

So, when does preprocess

happen?

Page 20: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

...Server receives requestRequest

Page 21: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Raw data

Processing

Return data

Database

ModulesRequest

Page 22: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Raw data

Processing

Return data

Database

ModulesRequest

Formatted data

HTML!

Theme

Page 23: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)
Page 24: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Raw data

Processing

Return data

Database

ModulesRequest

Formatted data

HTML!

Theme

Page 25: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Raw data

Processing

Return data

Database

ModulesRequest

Formatted data

HTML!

Theme

Page 26: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Preprocess in Practice

Page 27: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Firebug (or similar)

develdrush

Page 28: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Demo

includes/theme.incsites/all/themes/jupiter/template.php

jupiter_preprocess_page()dpm() dd()

jupiter_preprocess_node()

Exploring

Page 29: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Demo

Manually creating$submitted

$myvar

Page 30: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Demo

Specifying additional templates

Page 31: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Demo

Contextual CSS

Page 32: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Demo

Working with CCK

Working with fields

7!7!7!7!

Page 33: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Don't forget :)

Every template has a preprocessor!

Page 34: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Don't forget :)

file-name.tpl.php

jupiter_preprocess_file_name()

Page 35: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Oh yeah

Sexier examples

Page 36: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Demo

Views inbox

Page 37: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Demo

Cheap JSON API

Page 38: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Disclaimer :)

Page 39: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Important things to remember:

dpm() delaysDon't overload dpm()!

Clear cache when adding new tpl files and adding functions to template.php

Use check_markup() and check_plain() on data that may be unsafe

Page 40: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Questions?

Anyone? Anyone?

Page 41: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Thanks

Thanks to Matt Tucker (ultimateboy) for inspiring this talk Demo Photography Credits"Baseballs"http://www.flickr.com/photos/randomacts/2277808469/

"Basketball"http://www.flickr.com/photos/chillihead/3150447792/

"UNF Baseball vs. Florida Gulf Coast University"http://www.flickr.com/photos/8363028@N08/3368813471/

"Thor vs. Superman (49/365)"http://www.flickr.com/photos/jdhancock/4369073183/

Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

http://creativecommons.org/licenses/by-nc-sa/3.0/us/

Page 42: Theme preprocess functions: An Introduction (DrupalCamp NYC 10 2011)

Theme preprocess functions: an introduction

DrupalCampNYCDecember 10th, 2011