tags:

views:

226

answers:

2

These are two of the options I have for exposing a PHP object to a Smarty template. I know that there are syntax differences between the two, but I can't find any information on why you would use one over the other. Can anyone explain the differences?

Thanks,

James.

+1  A: 

If you use register_object() you can restrict the methods which can be called and also it means that you call the methods with a different (more Smarty-like) syntax:

<?php
// registering the object (will be by reference)
$smarty->register_object('foobar',$myobj);

// if we want to restrict access to certain methods or properties, list them
$smarty->register_object('foobar',$myobj,array('meth1','meth2','prop1'));

Template:

{* access our registered object *}
{foobar->meth1 p1='foo' p2=$bar}

From http://www.smarty.net/manual/en/advanced.features.php

Tom Haigh
A: 

Hi,

I think this page of smarty's documentation can help you : http://www.smarty.net/manual/en/advanced.features.php#advanced.features.objects

I don't think I could say much more than what's said there ; basically :

  • assign allows you to send data to the template ; some of these data can be objects
  • regiter_object allows more than that ; most important thing seems to be that you can specify which methods of the object can be used from the template file

The second possibility also gives a more "object-like" syntax, which looks like this (examples copied from the doc) :

{* access our registered object *}
{foobar->meth1 p1='foo' p2=$bar}

{* you can also assign the output *}
{foobar->meth1 p1='foo' p2=$bar assign='output'}
the output was {$output}

{* access our assigned object *}
{$myobj->meth1('foo',$bar)}


So, I'd say : choose the one that best fits your needs ;-)

Pascal MARTIN