eZ Community » Forums » Developer » How to display an object in a template
expandshrink

How to display an object in a template

How to display an object in a template

Thursday 19 April 2012 4:15:55 pm - 6 replies

How do i get this functions output from an object that we fetch from a custom module, as shown below.

{def $person = fetch( 'vCard', 'get_vcard')}

We would like to display using an object method

{$person->getFirstName()}

 

or a propertie of the object:

{$person->firstname}

 

Not one of them worked.

Thursday 19 April 2012 4:35:31 pm

is the Object an instance of eZPersistenceObject ?

if yes: only declare function_attributes key in your method: Person::definition().

if no you just have to declare the same beahaviour on your class:

 Class Personne
{
.....
 
 function attributes()
 {
  return array('name', 'firstname', ...);// array of attributes you want
 }
 function hasAttribute($name)
 {
  return in_array($name, $this->attributes())
 }
 
 function attribute($name)
 {
  $return = false;
  switch($name)
  {
   case 'name':
    $return = $this->getName()
    break;
   case 'firstname':
    $return = $this->firstame;
    break;
  }
  return $return;
 }
}

Notice: in current Template Language, $object.something means in PHP $myObject->attribute('something') or ($object['something'] for hash)

attributes are some kind of magic functions in eZ blunk.gif Emoticon

Edit: by doing that,

You can call in your template {$person.name}{$person.firstname}

 

I usually declaring a function_attributes array('template_var_name' => methodName, ...) and what i call 'static_Attributes'...

Modified on Thursday 19 April 2012 4:56:56 pm by Ousmane KANTE

Friday 20 April 2012 9:31:32 am

Nice, thank you for the fast response happy.gif Emoticon

Friday 20 April 2012 10:28:06 am

Thanks a lot Ousmane, it worked great for me.

Do you know if we can extend a class so those methods are already there? 

I was thinking to make a superclass that reads his own attributes and generates the methods you suggested automatically. 

Friday 20 April 2012 10:55:45 am

EDIT:

Sorry, didn't see that Ousmane gave the exact same answer as me happy.gif Emoticon

Modified on Friday 20 April 2012 10:58:42 am by Edi Modrić

Friday 20 April 2012 2:38:48 pm

Quote from Daniel Gracia :

Thanks a lot Ousmane, it worked great for me.

Do you know if we can extend a class so those methods are already there? 

I was thinking to make a superclass that reads his own attributes and generates the methods you suggested automatically. 

Hum I don't know such class.

I only know eZPersistentObject that is used for object persistence in db blunk.gif Emoticon as the indicated in name blunk.gif Emoticon

I usually try to use it (eZPersistentObject) as possible in my projects, it help... so much. but can say if it's a good or bad idea.

but of course, I think you can declare an interface (UsableInTable happy.gif Emoticon ) definining these methods. And your classes could implements that interface.

Friday 20 April 2012 4:17:44 pm

Hei

I created a superclass with those 3 methods you suggested and then extenden my Person class so it would be transparent. This can be use for other classes that I want to use in eZ templates

Maybe I m doing something that could be done in another way but it works for me and i can focus on my logic.

 

class object_to_ezTemplate {

  function  __construct(  )    { }

function attributes() {
$ret = array();
foreach ($this as $key => $val ) {
array_push($ret, $key);
} return $ret;
}

function attribute ($name) { return $this->$name; }

function hasAttribute($name) { return in_array($name, $this->attributes()); }

}

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from