Developing Page TemplatesCiviCRM uses page template files to display all the pages. This makes it possible for a person to customize any CiviCRM screen to suit a special requirement or preference. The HTML for CiviCRM pages is not embedded in PHP code, instead a template engine (Smarty) sends the correct page to the web browser.
A person should be familiar with HTML and CSS syntax to be comfortable editing page templates. Some page templates additionally make use of JavaScript and an Ajax utility, JQuery. Changing page templates is the wrong choice when ...If it is possible to make the needed changes by updating the CSS styles. For example, if a requirement is to hide o⁞r move some information or form fields on a screen, a CSS style for that HTML element can be changed to display: none, or position: absolute within the CSS file. If there is a CiviCRM hook available that can control the page. For example, there is a hook that can modify the information on the Contact Summary screen. If there is no process in place to update the page templates after an upgrade to a new version of CiviCRM. Page templates are stored in a separate folder and are not touched during an upgrade, However new versions of CiviCRM often change which placeholder elements are available in various templates. Proper source control procedures are needed to simplify upgrades to new versions.
Smarty templates introductionCiviCRM uses a page template engine called Smarty. This documentation is focused on how Smarty is used within the CiviCRM environment. Every Smarty element is enclosed between braces like these: "{}". All the other text is going to be displayed directly as HTML in the rendered page. Each page template is stored in a file with the extension .tpl. The PHP code assigns variables for content that needs to be displayed, and then lets the template engine take care of presenting it. The Smarty template engine always does this process :
These are the most commonly used Smarty template elements:
Please read the Smarty documentation for more information. Tip: To see what variables have been assigned to the template, enable debug (Administer -> Configure -> Global Settings -> Debugging) and on any URL, add &smartyDebug=1. It opens a new browser window listing all the variables and values.
CiviCRM introduces some extra features to Smarty:
How to find and modify the templates ?All the templates are under the folder templates/CRM in your CiviCRM installation. Finding which template is used on a given page can be difficult, but the easiest way to find out the answer is to view the source of the page from a web browser and search for ".tpl". For example, for the Contact Summary page, use the web browser to open the Contact Summary page, then click "View Source" in the browser. You should find an HTML comment such as:
<!-- .tpl file invoked: CRM/Contact/Page/View/Summary.tpl. Call via form.tpl if we have a form in the page. --> You can then view the file at templates/CRM/Contact/Page/View/Summary.tpl to see how the HTML is generated. If you want to modify the layout; for instance to reorder the content, do not modify directly these files, as all the modifications will be lost on the next upgrade of CiviCRM. The proper way is to create a new folder outside of your CiviCRM folder, then navigate to "Administer -> Configure -> Global settings -> Directories" in the navigation menu, and set the complete path of the folder that is going to contain your custom templates in the field Custom Templates. Scenario: The Contact Summary screen needs to be changedIf you want to alter the Contact Summary page template for Acme organization, perform these steps:
Tip: Say you want to modify the template for a specific profile form, or a specific event. Instead of copying the Form template to its default place (templates/CRM/Profile/Form/Edit.tpl), you can create a subfolder with the ID of the profile and put the template file you want to change in the subfolder (templates/CRM/Profile/Form/42/Edit.tpl to modify only the form for ProfileID 42).
You might be willing to modify a template that isn't directly the page you load, but added later via Ajax. For instance, perhaps you want to change all the tabs beside the Content Summary (Activities, Groups, etc.). The easiest way to do this is to install a development oriented plug-in to your web browser. If using Mozilla Firefox, the Firebug plug-in is indispensable. Open the Firebug console (or equivalent in your browser) and click the tab. You will see what URL has been loaded for the tab (e.g., for the notes tab: http://example.org/civicrm/contact/view/note?reset=1&snippet=1&cid=1). Open it in a new window or new tab of the web browser, and view the source. It also contains a comment identifying the template used (CRM/Contact/Page/View/Note.tpl). Keep in mind that when you modify a template, you might have a template that doesn't work properly anymore after an upgrade of CiviCRM, because the layout has changed or the name of variables assigned to the template was modified. In our experience, the easiest is to use a source code management system (SCM) to keep track of the changes you have made. Before doing any modification of the template you copied, add it to your SCM, and obviously also commit the template after having modified it. That way, you can easily generate a patch of your changes, and see how to apply them to the latest version of the template. Semantically meaningful HTML attributesTo make it as easy as possible for you to style any element in the page (e.g. put a yellow background on all the contacts of the subtype "members"), or add Ajax (clicking on the status of the activity changes it to complete), we strive to have a consistent and coherent schema for class names and ids for the generated HTML. This makes it easier to isolate the elements you want to alter from a custom style or from JavaScript:
At the time of the writing, some of the templates don't follow these conventions. Please update them and submit a bug tracking issue with a patch if you need to use a template that isn't yet complying. For more information about submitting a bug or issue, read About the CiviCRM community.
Displaying more content and adding Ajax featuresIf your modifications go further than "simple" modifications of the layout, but need to display more content than the one assigned to the template by default, or to add Ajax functionality, use the CiviCRM API. Please read more information about using the CiviCRM API from Ajax to pursue this approach. In most cases, using the CiviCRM APIs should be simple and only takes a few extra lines of modifications.
Some useful variables and examplesOn each page template, you have extra Smarty variables populated by CiviCRM.
{$config} Contains a lot of useful information about your environment (including the URL, if it's Drupal or Joomla!, etc.) {$session} Contains information about the user. If you want to modify the template only for a logged-in user but leave it identical for anonymous users, do the following:
{if $session->get('userID') > 0}
Insert your modifications here
{/if}
|