Answers.Body: <p>You can use CiviCRM api through smarty to retrieve if membership is active for a contact. You can achieve this by</p>
  
  <ol>
  <li>Turn on CIVICRM_MAIL_SMARTY (set to TRUE or 1) in civicrm.settings.php</li>
  <li>add below api snippet in your message template</li>
  </ol>
  
  <p>($result will hold result of api call)</p>
  
  <pre><code>{capture assign=contactId}{contact.contact_id}{/capture}
  {crmAPI var='result' entity='Membership' action='getcount' sequential=0 contact_id=$contactId membership_type_id="General" active_only=1}
  {if $result gt 0} 
    'active membership'
  {else}
    'not active'
  {/if}
  </code></pre>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-10T05:06:44.000Z
Answers.LastActivityDate: 2018-01-10T05:06:44.000Z
Answers.UserDisplayName: Pradeep Nayak
Answers.Body: <p>Don't think you have a direct variable to use in the template. Worth trying an extension to provide this? It can be done by simply writing something like below in your hook.</p>
  
  <pre><code>function yourextension_civicrm_postProcess($formName, &amp;$form) {
    if ($formName == "your_form") {
  
      //Check if user has an active membership of type "General".
      $activeMembership = civicrm_api3('Membership', 'getcount', array(
        'contact_id' =&gt; &lt;contact_id in $form&gt;,
        'active_only' =&gt; 1,
        'membership_type_id' =&gt; "General",
      ));
  
      //Assign a new variable to the template.
      $form-&gt;assign('activeMembership', $activeMembership);
    }
  }
  </code></pre>
  
  <p>You can now use this variable as <code>{$activeMembership}</code> in the template to check if user has a membership of the req type.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-10T03:39:19.160Z
Answers.LastActivityDate: 2018-01-10T04:07:26.457Z
Answers.LastEditDate: 2018-01-10T04:07:26.457Z
Answers.UserDisplayName: jitendra - fuzion
Body: <p>Is there a variable that one can use in smarty in a message template to simply determine if a contact has an active membership?</p>
  
Tags: <email><message-templates><smarty>
Title: How to check if a contact has an active membership in a smarty template?
ThreadText: # How to check if a contact has an active membership in a smarty template?
  
  (Asked by: username_0 on 2018-01-10)
  
  Is there a variable that one can use in smarty in a message template to simply determine if a contact has an active membership?
  
  **Tags:** email, message-templates, smarty
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-01-10. Score: 4
  
  You can use CiviCRM api through smarty to retrieve if membership is active for a contact. You can achieve this by
  
  1. Turn on CIVICRM\_MAIL\_SMARTY (set to TRUE or 1) in civicrm.settings.php
  2. add below api snippet in your message template
  
  ($result will hold result of api call)
  
  ```
  {capture assign=contactId}{contact.contact_id}{/capture}
  {crmAPI var='result' entity='Membership' action='getcount' sequential=0 contact_id=$contactId membership_type_id="General" active_only=1}
  {if $result gt 0} 
    'active membership'
  {else}
    'not active'
  {/if}
  
  ```
  
  HTH
  
  username_1
  
  
  ---
  
  ### Answer by username_2 on 2018-01-10. Score: 4
  
  Don't think you have a direct variable to use in the template. Worth trying an extension to provide this? It can be done by simply writing something like below in your hook.
  
  ```
  function yourextension_civicrm_postProcess($formName, &$form) {
    if ($formName == "your_form") {
  
      //Check if user has an active membership of type "General".
      $activeMembership = civicrm_api3('Membership', 'getcount', array(
        'contact_id' => <contact_id in $form>,
        'active_only' => 1,
        'membership_type_id' => "General",
      ));
  
      //Assign a new variable to the template.
      $form->assign('activeMembership', $activeMembership);
    }
  }
  
  ```
  
  You can now use this variable as `{$activeMembership}` in the template to check if user has a membership of the req type.
  
  
  ---
  
  