This documentation relates to CiviCRM version 3.2. It's not maintained anymore.
Current version of documentation.

Smarty interface

Skip to end of metadata
Go to start of metadata

This page refers to outdated version of CiviCRM. Check current version of documentation.


Documentation Search


CiviCRM 3.2 Documentation

Support and Participation

Developer Resources


CiviCRM book!

Make sure to check out Understanding CiviCRM as well! You can also support this project by ordering a hard copy.

You might want to display more information that those that are assigned by default in the template.

My goal was to have a simple way of retrieving more information from civicrm than the ones available in the template, without having to modify the php code .

For instance, I used it to display all the employees of an organisation from the profile that displays the organisation, or to display a list ofother contacts that live in the same country than the contact you're viewing.

The general idea is to be able to use any api method to fetch the content and assign it to a regular smarty variable, so it can then be templated and display how you want it.

It simply introduce a new function in the template, "crmAPI". I'm assuming in the rest of this post that you know already where are stored the templates, how to identify the one you want to modify and have a basic knowledge of smarty.

If you are familiar with civicrm api v2, it will be fairly straightforward to use and fetch about any type of content existing in civicrm and display it where you want. From anywhere within your template, simply call {crmAPI entity="nameobject" method="namemethod" var="namevariable" extraparam1="aa" extraparam2="bb"}

  • entity is the content you want to fetch, eg. "contact", "activity", "contribution"...
  • method is get, search, search_count (it shouldn't be a method that modify the entity, only to fetch data)
  • var is the name of the smarty variable you want to assign the result to (eg the list of contacts)
  • extraparams (optional) all the other parameters (as many as you want) are simply used directly as the "params" to the api. cf. the example below
  • return (optional). The convention to define the return attributes (return.sort_name return.country...) doesn't work with smarty and is replaced by return="attribute1,attribute2,attribute3.."

For example, if you want to display a list of contacts\

{crmAPI entity='contact' action="search" var="contacts"}
<ul>
{foreach from=$contacts item=contact}
<li id="contact_{$contact.contact_id}">{$contact.sort_name}</li>
{/foreach}</ul>

The extra params allow to filter, for instance only the individual contacts in France, and return only the sort_name and the email. {crmAPI entity='contact' action="search" var="contacts" country="France" contact_type="Individual" return ="sort_name,email"}

Another example passing the contact ID smarty variable from the page as a parameter. For this particular API call, the results are in a sub-array called 'result', so to get at them, we loop through "$examplevar.result" instead of "$examplevar". See the api docs - most have an example of the kind of array you will recieve for your call. (If you need more info, use smarty's version of 'print_r' to see what you've been given, e.g. {$activities|@print_r} )

{crmAPI entity="activity" action="get_contact" var="activities" contact_id=$contactId }
{foreach from=$activities.result item=array}
<p>Activity { $activity.activity_name } is { $activity.status } </p>
{/foreach}

Numerical array entries: Some API functions presently throw a bunch of results at you without giving them a named key. E.g., get notes gives you an array of body text from your notes and one associative value for the error_key. If you need to loop through numerical array entries like this, Smarty uses a different function, 'Section'. Example:

{ crmAPI entity="note" action="get" var="notes" entity_table="civicrm_contact" entity_id=$contactId }
{section loop=$notes name=notesrow start=0 }
<p> This note says "{$notes[notesrow]}". Who wrote it?</p>
{/section}

Labels
  • None

Creative Commons License
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-Share Alike 3.0 United States Licence.