theme preprocess functions: an introduction (drupalcamp nyc 10 2011)
DESCRIPTION
Slides from http://drupalcampnyc.org/sessions/theme-preprocess-functions-introductionTRANSCRIPT
Theme preprocess functions: an introduction
DrupalCampNYCDecember 10th, 2011
Carl Wiedemann
tCarl WiedemannI live here in NYC...
Carl Wiedemann...originally from Denver
RIP
c4rl
...on various channels
Preprocess in Theory
Theme Dilemmas
7!7!7!7!
What do we do if this is insufficient?
<div class="content"> <?php print $content; ?></div>
How can we create more useful content?
<div class="content"> <?php print $my_content; ?></div>
<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?
Preprocess
Preprocessmakes content
flexible
<?php print $my_content; ?>
Preprocessmakes theming
dynamic
class="<?php print $my_classes; ?>"
Preprocesskeeps templates
clean
if else foreach while switch =
So, when does preprocess
happen?
...Server receives requestRequest
Raw data
Processing
Return data
Database
ModulesRequest
Raw data
Processing
Return data
Database
ModulesRequest
Formatted data
HTML!
Theme
Raw data
Processing
Return data
Database
ModulesRequest
Formatted data
HTML!
Theme
Raw data
Processing
Return data
Database
ModulesRequest
Formatted data
HTML!
Theme
Preprocess in Practice
Firebug (or similar)
develdrush
Demo
includes/theme.incsites/all/themes/jupiter/template.php
jupiter_preprocess_page()dpm() dd()
jupiter_preprocess_node()
Exploring
Demo
Manually creating$submitted
$myvar
Demo
Specifying additional templates
Demo
Contextual CSS
Demo
Working with CCK
Working with fields
7!7!7!7!
Don't forget :)
Every template has a preprocessor!
Don't forget :)
file-name.tpl.php
jupiter_preprocess_file_name()
Oh yeah
Sexier examples
Demo
Views inbox
Demo
Cheap JSON API
Disclaimer :)
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
Questions?
Anyone? Anyone?
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/
Theme preprocess functions: an introduction
DrupalCampNYCDecember 10th, 2011