CiviCRM Community Forums (archive)

Old sections (read-only, deprecated) => Developer Discussion => Topic started by: lcdweb on July 25, 2009, 05:38:23 pm

Title: Smarty vars in profile pages
Post by: lcdweb on July 25, 2009, 05:38:23 pm
I've got a profile search form that I'm putting together. The detail view needs to be custom laid out, which means I'm referencing each variable individually, instead of through the foreach iteration.

However, the $row array assigns the label value to the key -- and because many of the labels have spaces, the key/value can't be referenced (smarty won't let you reference a key containing spaces). Which basically means I can't reference fields individually.

I'm playing with reassigning the values so they can be referenced. But I think this should be changed in future releases, as it significantly impacts customizeability.

-B
Title: Re: Smarty vars in profile pages
Post by: Dave Greenberg on July 27, 2009, 11:09:46 am
Brian - If you can recommend a "simple" change to the tpl for 3.0, let me know and I'll look at putting it in.
Title: Re: Smarty vars in profile pages
Post by: lcdweb on July 28, 2009, 08:03:16 am
Well, you know I'm a bit of a hack. So this is pretty ungraceful --

templates/CRM/Profile/Page/View.tpl

at the top I've added:

Code: [Select]
{foreach from=$row item=value key=rowName name=profile}
        {assign var=field value='f'}
        {assign var=label value='l'}
        {assign var=fieldname value=$rowName|replace:' ':'_'}
        {assign var=$label|cat:$fieldname value=$rowName}
        {assign var=$field|cat:$fieldname value=$value}
{/foreach}

That gives me a label/field pair for each field, with the format f[name of field with spaces replaced by underscore] and l[same]. So I can reference the labels and fields for each field individually now.

I'm not sure you'd want to add to the tpl file for the release though. Because the variable assignment happens in the tpl file, the list of variables doesn't show up in the smarty debug window. So people won't know they're available unless they look at the tpl file. But I guess those looking to reference variables individually will be looking at the tpl file anyway. For now, it's probably sufficient to just have a solution available on the forums.

Here's my draft modified directory layout, in case you're interested. Still in progress.  :)
http://www.nytransit.org/index.php?option=com_civicrm&task=civicrm/profile/view&reset=1&id=704&gid=3

(this site was my model for the book case study)
Title: Re: Smarty vars in profile pages
Post by: Donald Lobo on July 28, 2009, 09:49:16 am

here is what i did last nite when i wanted to do something similar. I knew the fields in the profile which i wanted to layout

Code: [Select]
<table class="form-layout-compressed">
{assign var=fName value="First Name"}
{assign var=lName value="Last Name"}
  <tr id="contact_name"><td class="label">Name</td><td class="view-value">{$row.$fName}&nbsp;{$row.$lName}</td></tr>
  <tr id="contact_email"><td class="label">Email</td><td class="view-value">{$row.Email}</td></tr>
  <tr id="contact_phone"><td class="label">Phone</td><td class="view-value">{$row.Phone}</td></tr>
</table>

lobo
Title: Re: Smarty vars in profile pages
Post by: Dave Greenberg on July 28, 2009, 02:48:21 pm
Brian - this seemed like an easy "win" for site designers, so I filed and implemented an issue for 3.0 to make this easy:

http://issues.civicrm.org/jira/browse/CRM-4805

With this change... if you're creating want a custom profile view template, you can access field labels and values in $profileFields_N array - where N is profile ID.

EXAMPLE - to access last_name field for profile 1:

{$profileFields_1.last_name.label}
{$profileFields_1.last_name.value}

Would be great if you could try this out (if you have a trunk svn running) OR backport the change in Dynamic.php - should work fine.
To test a backport (caveat - i didn't do this).... insert these lines after line 146 in CRM/Profile/Page/Dynamic.php
Code: [Select]
            // $profileFields_$gid array can be used for customized display of field labels and values in Profile/View.tpl
            $profileFields = array( );
            $labels = array( );

            foreach ( $fields as $name => $field ) {
                $labels[$field['title']] = $name;
            }
            foreach ( $values as $title => $value ) {
                $profileFields[$labels[$title]] = array( 'label' => $title,
                                                    'value' => $value );
            }
           
            $template->assign_by_ref( 'profileFields_' . $this->_gid, $profileFields );

Then assuming it works as you'd hoped, would be awesome if you could add some doc for this in the 3.0 customizing profiles section:
http://wiki.civicrm.org/confluence/display/CRMUPCOMING/Customize+Built-in%2C+Profile%2C+Contribution+and+Event+Registration+Screens
Title: Re: Smarty vars in profile pages
Post by: lcdweb on July 29, 2009, 04:17:14 pm
Dave,
I backported to v2.2 and it works great -- and it's much easier to reference the available fields through smarty debug with this method. I'll modify the wiki. thanks!
-B
Title: Re: Smarty vars in profile pages
Post by: lcdweb on August 05, 2009, 10:44:05 am
Dave,
I'm not sure why, but Smarty didn't like dashes in the array key. This affected the phone and address fields (they contain -Primary). So I altered to replace the dash with an underscore:

Code: [Select]
            // $profileFields_$gid array can be used for customized display of field labels and values in Profile/View.tpl
            $profileFields = array( );
            $labels = array( );

            foreach ( $fields as $name => $field ) {
                $labels[$field['title']] = str_replace("-", "_", $name);
            }
            foreach ( $values as $title => $value ) {
                $profileFields[$labels[$title]] = array( 'label' => $title,
                                                    'value' => $value );
            }
           
            $template->assign_by_ref( 'profileFields_' . $this->_gid, $profileFields );
Title: Re: Smarty vars in profile pages
Post by: Donald Lobo on August 05, 2009, 10:52:29 am

just to be safe, u might want to change that to:

Code: [Select]
                $labels[$field['title']] = preg_replace('/\s+|\W+/', '_', $name);

this replaces all non alpha-numeric characters with a _

lobo
Title: Re: Smarty vars in profile pages
Post by: lcdweb on August 05, 2009, 10:58:37 am
Great -- thx.

(aren't you on a plane right now?)
Title: Re: Smarty vars in profile pages
Post by: Donald Lobo on August 05, 2009, 11:02:25 am

plane delayed a bit. wonders of internet connectivity everywhere :)

lobo
Title: Re: Smarty vars in profile pages
Post by: Dave Greenberg on August 05, 2009, 05:07:45 pm
I've committed lobo's recommended fix to trunk for next alpha release.