|
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 |
marcelaeloisa Smarty n00b
Joined: 25 Aug 2010 Posts: 1
|
Posted: Wed Aug 25, 2010 1:39 pm Post subject: Smarty + O.O. |
|
|
Tendo a seguinte estrutura:
Code: |
Pessoa Object
(
[codigo:private] => 1
[nome:private] => Fulano
[codProfissao:private] => 10
[profissao:private] => Profissao Object
(
[codProfissao:private] => 10
[descricao:private] => Analista
)
)
|
Como recupero a informação "descricao" da classe Profissao() no meu smarty?
Uma das tentativas foi:
Code: | {$objPessoa[i]->getProfissao()->getDescricao()} |
Mas com isso tenho o seguinte erro:
Code: | syntax error: unrecognized tag: $objPessoa[i]->getProfissao()->getDescricao() |
Obrigada |
|
Back to top |
|
trevieri Smarty n00b
Joined: 26 Apr 2007 Posts: 4
|
Posted: Thu Nov 10, 2011 7:41 pm Post subject: |
|
|
hm, nao sei se vc chegou a fazer isso entao vamos la, um exemplo:
comecando pelo index.php no raiz:
Code: |
<?php
class Pessoa {
private $id;
private $nome;
private $Profissao;
function __construct($Profissao) {
$this->Profissao = $Profissao; // aki repassei 1 objeto pra dentro deste
}
function Set($id,$nome) { // seta td q to com preguica de seta 1 por vez
$this->id = $id;
$this->nome = $nome;
}
function getId() {
return $this->id;
}
function getNome() {
return $this->nome;
}
function getProfissao() {
return $this->Profissao;
}
function __destruct() {
}
}
class Profissao {
private $id;
private $descricao;
function __construct($id, $descricao) {
$this->id = $id;
$this->descricao = $descricao;
}
function getId() {
return $this->id;
}
function getDescricao() {
return $this->descricao;
}
function __destruct() {
}
}
$anao = new Smarty;
$profissao = Profissao('1','Office Boy');
$pessoa = new Pessoa($profissao);
$pessoa->Set('1','Joao Carlos');
// sendo assim ID=1 de pessoa eh Joao Carlos e sua profissao no momento eh Office Boy
$anao->assign("obj", $pessoa);
$anao->display("index.tpl");
?>
|
agora o index.tpl q deve estar dentro da pasta templates interna a pasta onde esta o index.php:
Code: |
<html>
<head><title>teste</title></head>
<body>
<table border=0>
<tr>
<td>ID Pessoa</td>
<td>Nome</td>
<td>ID Profissao</td>
<td>Profissao</td>
</tr>
<tr>
<td>{$obj->getId()}</td>
<td>{$obj->getNome()}</td>
<td>{$obj->getProfissao()->getId()}</td>
<td>{$obj->getProfissao()->getDescricao()}</td>
</tr>
</table>
</body>
</html>
|
o motivo de ter de utilizar se dos gets eh devido ao metodo privado que foi aplicado nos objetos das classes. |
|
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
|