1 / 22

Theming Best Practices Preprocess Hook An Introduction

Theming Best Practices Preprocess Hook An Introduction. Mandakini Ayushi Infotech. Overview. Theme Architecture an Introduction Structure of theme Files Core Template for different regions Template Hierarchy Theme() execution Hierarchy Standard templates available for module

kishi
Télécharger la présentation

Theming Best Practices Preprocess Hook An Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Theming Best Practices Preprocess Hook An Introduction Mandakini AyushiInfotech

  2. Overview • Theme Architecture an Introduction • Structure of theme Files • Core Template for different regions • Template Hierarchy • Theme() execution Hierarchy • Standard templates available for module • Tools for getting started • Exploring for theme • Examples: Step-by-step guide to theme a page and form in a module for D 6/7 .

  3. Structure of Theme Files

  4. Each Template Handles A Region of Your Site • html.tpl.php -Master template file for your site • page.tpl.php - Entire Page • front-page.tpl.php - Just Front Page • block.tpl.php - Blocks • comment.tpl.php - Comments • forum.tpl.php – Forums • field.tpl.php - modules/field/theme

  5. Template Hierarchy

  6. Theme() execution hierarchy theme() execute in below sequence Theme() check first for themename_mycallback() in template.php. If it finds one, it takes precedent. If not it looks, again in template.php, for a phptemplate_mycallback () If it finds nothing else, it uses the theme_mycallback () in your module.

  7. Available templates for module • theme(‘pager’) • theme(‘image’) • theme(‘table’) • theme(‘item_list’) or theme_item_list()

  8. Tool helps in theming Firebug Drupal for Firebug Devel and devel_themer coder Web Developer Toolbar Starter Theme e.g. zen or fusion XDebug & NetBeans

  9. Firebug: To inspect HTML and CSS, and prototype styles(http://getfirebug.com) Web Developer Toolbar: Adds a menu and a toolbar to Firefox with various helpful web developer tools. https://addons.mozilla.org/en-US/firefox/addon/60 Devel: dpm() XDebug & NetBeans

  10. Drupal for Firebug: Install the Drupal for Firebug module

  11. Exploring for theme • theme.inc • template.inc • Mytheme_preprocess_page() • Mytheme_preprocess_node()

  12. Ex 1: How to Call theme() A brief overview of theming in Drupal 6/7. • Create a tpl.php file for your template • Create a hook_theme() implementation to register your template • Create a module preprocess function for any variables you wish to make present in the template • Clear caches • Go to the block/menu callback/other location you have invoked the theme() function from to call your template

  13. Step1) hook_menu() implementation function drupalcamp_menu(){ $items['my-themeing-page'] = array( 'title' => 'General page callback', 'page callback' => 'drupalcamp_page_theming', 'access arguments' => array('access content'), 'type' => MENU_NORMAL_ITEM ); return $items; } Step2) hook_preprocess function drupalcamp_preprocess_drupalcamp_page_theming(&$vars){ $vars['new_variable1'] = 'this is my first variable‘; }

  14. Step3) hook_theme implementation function drupalcamp_theme(){ $items['drupalcamp_page_theming'] = array( 'template' => 'drupalcamp-page-theming', 'arguments' => array(), 'access agruments' => TRUE ); return $items; } Note the underscores in the theme function name are replaced with hyphens when we create our template reference. Note also that, in theory, the template name does not have to match the theme function name, however, we have discovered through testing that if the template name is different from the theme function name then the theme may not recognise the template file when it is coped to the theme's template directory. I'm not sure if this is by design, but for safety's sake make sure the template file and the theme registration are the same in name.

  15. Step 4) call theme() function function drupalcamp_page_theming(){ return theme('drupalcamp_page_theming'); } Step 5) Next job is to create our template file drupalcamp-page-theming.tpl.php <div class="my-theme"> This is my template. <?php print $new_variable1; ?> </div> Now if you clear the Drupal cache (see below) and we visit the page http://yoursite.com/my-themeing-page then we will see the output of our theme function

  16. Ex 2: Page callback & theme This is similar to example 1 only difference is we added page callback => ‘theme’ and deleted step4 Step1) hook_menu() implementation function drupalcamp_menu(){ $items['my-themeing-page'] = array( 'title' => 'General page callback', 'page callback' => ‘theme', ‘page arguments’ => array(‘drupalcamp_page_theming’), 'access arguments' => array('access content'), 'type' => MENU_NORMAL_ITEM ); return $items;}

  17. Step2) hook_theme implementation function drupalcamp_theme(){ $items['drupalcamp_page_theming'] = array( 'template' => 'drupalcamp-page-theming', 'arguments' => array(), 'access agruments' => TRUE ); return $items; } Step3) hook_preprocess function drupalcamp_preprocess_drupalcamp_page_theming(&$vars){ $vars['new_variable1'] = 'this is my first variable'; $vars['goto_back'] = l(‘My Link', ‘my url’, array('attributes' => array('class' => 'orangeBtn'))); }

  18. Ex 3: How to theme a form Step1) hook_menu() implementation function drupalcamp_menu(){ $items['form_theme'] = array( 'title' => 'How to implement theme with a form callback‘, 'page callback' => 'drupal_get_form', 'page arguments' => array('drupalcamp_form'), 'access arguments' => array('access content'), 'type' => MENU_NORMAL_ITEM ); }

  19. Step2) hook_theme implementation function drupalcamp_theme(){ $items['drupalcamp_form'] = array( 'template' => 'drupalcamp-form', 'arguments' => array('form' => NULL), 'access arguments' => TRUE, ); } Step3) hook_preprocess function drupalcamp_preprocess_drupalcamp_page_theming(&$vars){ $vars[‘about_us'] = l(‘About Us', ‘url’, array('attributes' => array('class' => ‘Btn'))); $image_path = drupal_get_path('module', ‘drupalcamp') . '/images/image.png'; $var[‘my_image'] = theme_image($image_path,'',''); $var['setmessages'] = drupal_get_messages(); $vars['firstname'] = drupal_render($vars['form']['firstname']); $var[‘add_temp'] = theme('drupalcamp-page-theming'); // Called a template }

  20. Step4) hook_form implementation function drupalcamp_form(){ $form['firstname'] = array( '#type' => 'textfield', '#title' => 'First Name', '#default_value' => 'test', ); return $form; } Step5) Create drupalcamp-form.tpl.php page <div class="paymentFooterLnks"> <?php print $about_us;?> </div> <div> <?php print $add_temp;?></div> <div class="verisign"><?php print $my_image;?></div> <div class="billing"><?php print $firstname;?> </div> <?php print drupal_render($form);?>

  21. Some Useful Links https://ratatosk.backpackit.com/pub/1836982-debugging-drupal http://drupal.org/project/devel_themer http://mogdesign.eu/blog/performance-tips-for-drupal-theming/ http://www.drupaler.co.uk/blog/theming-drupal-6/96#comment-1504 http://design.acquia.com/content/tools-getting-started http://www.appnovation.com/theming-views-drupal-6-simple-way

  22. Thank you Catch me at Mandakini125 (skype) mandakini@ayushiinfotech.com

More Related