|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
ananthbtechit Smarty n00b
Joined: 25 Mar 2016 Posts: 3
|
Posted: Tue Nov 15, 2016 10:32 am Post subject: Unable to access registered object's property in a template |
|
|
In the Smarty2 code base, I was using register_object() function as (in businessLogic.php) follows:
class myObject {
public $name;
.....
}
$obj = new myObject();
$smarty->register_object("regobj", $obj);
Then in my template file: mytemplate.tpl, the attribute of the myObject class is as follows:
<table>
<tr>
<td>{regobj->name}</td>
</tr>
<table>
With the above template, the $name value of the $obj is correctly getting rendered in Smarty2.
As the register_object() function is replaced by registerObject() in Smarty3, I have made the
code change (in businessLogic.php) as follows when I migrated to Smarty3:
class myObject {
public $name;
.....
}
$obj = new myObject();
$smarty->registerObject("regobj", $obj);
I tried to render my template file: mytemplate.tpl (without making any changes), but
the $name value of the $obj is did not get rendered in Smarty3. The rendered table cell was empty.
I have read the Smarty3 documentation at URL: http://www.smarty.net/docs/en/advanced.features.objects.tpl, but it did not help as
it gives examples for accessing the methods of the registered object: $obj.
Can someone tell me if there is anything wrong in using of registerObject() function?
Have anyone able to register an object and able to access the attribute of the registered object successfully in a Smarty3 template?
To temporarily overcome the problem, I have used the assign() function in place of Smarty3 registerObject() and it works.
Here is the code that works!
businessLogic.php file:
class myObject {
public $name;
.....
}
$obj = new myObject();
$smarty->assign("regobj", $obj);
mytemplate.tpl file:
<table>
<tr>
<td>{$regobj->name}</td>
</tr>
<table>
The above code worked as expected for me, however, it is not using the same pure-object syntax as it is intended by the use of Smarty3 registerObject() function.
Please let me if you have any better solution. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Nov 15, 2016 1:02 pm Post subject: |
|
|
Code: | <?php
require_once 'smarty-config.php';
class MyClass {
public function myFunction()
{
print 'My function.';
}
}
$smarty->registerObject('MyClass', new MyClass);
$smarty->display(__FILE__);
__HALT_COMPILER();
?>{$smarty.version}
{MyClass->myFunction}
|
Code: | $ registerObject.php
3.1.30
My function. |
Works as documented in http://www.smarty.net/docs/en/advanced.features.objects.tpl
If you want to access object PROPERTIES, ASSIGN the object. REGISTER works for registering the object as plugin to access its METHODS. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|