<phonebooks>
  <contacts group_name="Default" id="0">
    {foreach $contacts as $row}
      <contact
        id="{$row@index}"
        prefix=""
        first_name="{$row.contact_name_given}"
        second_name=""
        last_name="{$row.contact_name_family}"
        suffix=""
        contact_type="sip"
        organization="{$row.contact_organization}"
        job_title=""
        location=""
        notes=""
        account_id="{if $row.phone_number != ""}{$row.phone_number}{else}{$row.phone_extension}{/if}"
      >
        <actions>
          {if $row.phone_number != ""}
          <action id="primary" dial="{$row.phone_extension}" label="Extension" name="Dial" />
          <action id="send_to_vm" dial="*99{$row.phone_extension}" label="Voicemail" name="Dial Voicemail"></action>
          {else}
          <action id="primary" dial="{$row.phone_number}" label="Extension" name="Dial" />
          {/if}
        </actions>
      </contact>
    {/foreach}
    {foreach $keys["line"] as $row}
      <contact
        id="sd-{$row.device_key_id}"
        prefix=""
        first_name=""
        second_name=""
        last_name="{$row.device_key_label}"
        contact_type="sip"
        account_id="{$row.device_key_value}"
        {if $row.device_key_type == "blf"}
        subscribe_to="sip:{$row.device_key_value}@{foreach $lines as $l}{$row.server_address}{break}{/foreach}"
        {/if}
      >
        <actions>
          <action id="primary" dial="{$row.device_key_value}" dial_prefix="" label="Extension" name="Dial" />
        </actions>
      </contact>
    {/foreach}
  </contacts>
</phonebooks>
