Upcoming Events

NYC CiviCRM Meetup - September 7th
September 7th, 2010
This next NYC meetup will feature a case study or 2, a look at what's new in (more...)

Configuring, Customizing and Extending CiviCRM - New York
September 16th, 2010
This comprehensive two-day hands on training course is targeted at (more...)

CiviCRM User and Administrator Training - New York
September 16th, 2010
A comprehensive two day hands on training course covering the configuration, (more...)

CiviCRM Code and Test Sprint - New York
September 18th, 2010
This code and test sprint is targeted at experienced developers who want to (more...)

CiviCRM Toronto Meetup
September 21st, 2010
Come meet others from the Toronto Area who are interested in, using or (more...)

CiviCRM Philly Meetup – September 2010
September 23rd, 2010
Come meet others from the Philadelphia Area who are interested in, using or (more...)

CiviCRM Seminar - Dublin
September 28th, 2010
NfP Services are hosting a free seminar at The IBOA, Stephen St Upper, Dublin 8 (more...)

London developer and implementer training
September 30th, 2010
This comprehensive two-day hands on training course is targeted at implementers, (more...)

London user and administrator training
September 30th, 2010
A comprehensive two day hands on training course covering the configuration, (more...)

Berlin user and administrator training
October 6th, 2010
A comprehensive one day hands on training course covering the configuration, (more...)

Berlin developer and implementer training
October 7th, 2010
This comprehensive one-day hands on training course is targeted at implementers, (more...)

Benelux meetup in Brussels: Connect, communicate and activate your supporters and constituents
October 11th, 2010
Come meet others who are interested in, using or developing for CiviCRM. For (more...)

CiviCRM Toronto Meetup
October 19th, 2010
Come meet others from the Toronto Area who are interested in, using or (more...)

CiviCRM Toronto Meetup
November 16th, 2010
Come meet others from the Toronto Area who are interested in, using or (more...)

CiviCRM Components

Tools for engaging your supporters...

CiviContribute


CiviEvent


CiviMail


CiviMember


CiviReport


CiviCRM Architecture - Templating System

Not Just a Contact Database

These optional components give you more power to connect and engage your supporters.

  • civiEVENT

  • Online event registration and participant tracking.

  • civiMEMBER

  • Online signup and membership management.

  • civiMAIL

  • Personalized email blasts and newsletters.

  • civiREPORT

  • Report generation and template management.

November 14, 2006 - 10:36 — CiviCRM Team

This installment of our architecture series will introduce the templating system used by CiviCRM as the presentation layer (e.g. to actually render forms and pages). Every CiviCRM screen is "composed" from one or more template files. These files contain a combination of HTML tags, text, variables and (often) some code to control presentation logic.

CiviCRM uses an open-source templating engine called Smarty. If you are planning on examining, debugging and/or and modifying CiviCRM screens - you'll want to spend some time reviewing Smarty's online documentation.

Finding the Template(s) Used by a Form or Page

The easiest way to find the base template used to render a particular CiviCRM screen is to do a "View Source" while on that screen. Then search for the string '.tpl file'. This will take you to a commented HTML line that looks like this (this is the Edit Contact form):

<!-- .tpl file invoked: CRM/Contact/Form/Edit.tpl.
Call via form.tpl if we have a form in the page. -->

All templates are in the /templates directory just below your CiviCRM root - so in this case the Edit.tpl will be located in [civicrm_root]/templates/CRM/Contact/Form/Edit.tpl.

You can also derive the location of a template from the corresponding PHP class file. For the example above - the base PHP file for the contact edit form is [civicrm_root]/CRM/Contact/Form/Edit.php. In general the path to the file will be a logical combination of the object ("Contact"), and either Form or Page depending on the action (add/edit vs. display). If this doesn't get you to the correct file - CRM/Core/Invoke.php and the corresponding Invoke.php files for the Contribute, Mail, and Membership components contain the logic which parses each URL path and invokes the corresponding PHP class file.

How are Variable Values Assigned to the Template

The PHP class files for "forms" and "pages" assign any variable values that need to be included to the corresponding Smarty template using the assign function. The function parameters are variable name and variable value. This example assigns the value of the contact's "display name" to the Contact Summary template:

 $this->assign( 'displayName', $displayName ); 

This variable is then evaluated in the template using the Smarty variable syntax (dollar sign inside of curly brackets) .

Contact Display Name is: {$displayName}

Smarty Control Structures

Smarty includes basic control structures (if, else, foreach...) which are used for conditional presentation logic. One common example is looping through rows of table data in CiviCRM "selectors". The PHP file assigns an array of "rows" to the template - and the template loops over the rows array and evaluates each row's cells in the table layout:

{foreach from=$rows item=row}
  
     {$row.name}	
     {$row.description} 
     {$row.action}
   
{/foreach}

Refer to the Smarty documentation for more info and examples of control structures and functions.

CiviCRM Smarty Plugins

Smarty has a plugin architecture which we use to add CiviCRM-specific presentation-related functionality. For example, we needed a way to make sure that monetary values were rendered properly for a given CiviCRM localisation / currency format - so we added a variable modifier - crmMoney - which can be used in any template to add formatting to a monetary value:

Contribution Amount: {$amount|crmMoney}

The plugin definitions are located in CRM/Core/Smarty/plugins.

Debugging Templates

If you need to debug a template - you can ask Smarty to display a pop-up window which lists all variables and values in it's scope. There are two ways to trigger this:

  • Enable the global CiviCRM debug setting. For 1.5 and earlier, you do this by setting CIVICRM_DEBUG to 1 in civicrm.settings.php. Then, add &smartyDebug=1 to the end of the query string for the screen you want to debug. ( http://www.example.org/civicrm/contact/view/basic?reset=1&cid=162&smartyDebug=1 ). Make sure you have turned off pop-up blocking for the site.
  • Add the Smarty debug command to the top of the template file you are debugging: {debug} and browse to the screen.

Either method should cause a pop-up window to display with ALL variables and values in the current template scope.