0

I'm using the HybridAuth library to display a list of user contacts from Gmail. The object member is displayName and I'm using it like this:

{foreach from=$contacts key=k item=contact}
    <span class="name">{$contact->displayName}</span>
{/foreach}

I'd like to use it like this:

{foreach from=$contacts key=k item=contact}
    <span class="first-name">{$contact-firstName}</span>
    <span class="last-name">{$contact->lastName}</span>
{/foreach}

I imagine I could do this with regex without much trouble but I know regex is not the preferred solution. I've been trying to figure out something like..

{$contact->strtok(displayName," ")}

which returns

Fatal error :  Call to undefined method Hybrid_User_Contact::strtok()

I'm now trying to do something like this..

{assign var=$contact value=" "|explode:displayName}

but I haven't figured out how to do it correctly yet.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
davidcondrey
  • 34,416
  • 17
  • 114
  • 136

1 Answers1

1

I think that you wanted to achieve the result and not use object syntax in Smarty (and in case you want to use object syntax you should rather do assignment in PHP not in Smarty).

So you need to do it this way:

{foreach from=$contacts key=k item=contact}
    {assign var="names" value=" "|explode:$contact->displayName}
    <span class="first-name">{$names[0]}</span>
    <span class="last-name">{$names[1]}</span>
{/foreach}

When in PHP file I have:

$contacts = array();

$contact = new stdClass();
$contact->displayName = 'Name Surname';
$contacts[] = $contact;
$contact = new stdClass();
$contact->displayName = 'Name2 Surname2';
$contacts[] = $contact;
$smarty->assign('contacts',$contacts);

html source will be as you expect:

<span class="first-name">Name</span>
<span class="last-name">Surname</span>
<span class="first-name">Name2</span>
<span class="last-name">Surname2</span>

EDIT

In case if some records don't contain name and surname (as you mentioned in comment) you could do it this way:

in PHP file:

$contacts = array();

$contact = new stdClass();
$contact->displayName = 'Name Surname';
$contacts[] = $contact;
$contact = new stdClass();
$contact->displayName = 'Name2Surname2';
$contacts[] = $contact;
$smarty->assign('contacts',$contacts);

In Smarty file:

{foreach from=$contacts key=k item=contact}
    {assign var="names" value=" "|explode:$contact->displayName}

    {if $names|@count eq 2}
    <span class="first-name">{$names[0]}</span>
    <span class="last-name">{$names[1]}</span>
    {else}
        <span class="name-first-last">{$contact->displayName}</span>
    {/if}
{/foreach}

Output for this will be:

<span class="first-name">Name</span>
<span class="last-name">Surname</span>        
<span class="name-first-last">Name2Surname2</span>
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • I keep getting an undefined offset error because some of them do not have a first and last name. I tried {if (array_key_exists($names[1],$contact->displayName))} but this isn't correct. Do you know how I could check for it to avoid the error in cases where there are not a first and last name? – davidcondrey Sep 16 '14 at 21:58