tags:

views:

256

answers:

2

I have an issue with passing an object to smarty tag. I have the following code:

$contact = new Contacts;
$smarty = new Smarty;
$smarty->assign('contact',$contact);

In test.htpl :

<html>
<head>
    <title>{$title}</title>
</head>
<body>
    id: {$contact->id} <br/>
    name: {$contact->name} <br/>
    email: {$contact->email} <br/>
    phone: {$contact->phone} <br/>
</body>
</html>

this leads to an warning of invalid character '>'. How can I solve this?

I used this class for testing:

class Contacts
{
 public $id = 1;
 public $name = 'Mada';
 public $email = '[email protected]';
 public $phone = 123456;
}
A: 

By doing the following should work

$smarty->register_object('contact',$contact);

Calling it this way should then work

<html>
<head>
    <title>{$title}</title>
</head>
<body>
    id: {$contact->id} <br/>
    name: {$contact->name} <br/>
    email: {$contact->email} <br/>
    phone: {$contact->phone} <br/>
</body>
</html>

Also then you don't need to call this method

$smarty->assign('contact',$contact);
Roland
+1  A: 

Use

$smarty->assign_by_ref('contact',$contact);

This will allow you to access in the way you expect.

Using register_object() is also an option, and allows you to restrict what can be used from the template, but this means a different template format (no initial $).

Deebster