简体   繁体   中英

displaying symfony2 object values in twig template

i'm having problems passing object values to my twig templates.

Here is some of my controller code that shows the content of the object:

if (!$request->isXmlHttpRequest()) {
            $manejador = new Manejador();
            $temas=new ArrayList();
            $temas=$manejador->scrollingAjax();
            return $this->render(
                    'UsuarioBundle:Default:index.html.twig',
              array(
                    'temas'=>$temas));
        } 

Here, my Arraylist code.

class ArrayList {
    private $list = array();
    public function Add($obj)
    {
    ....
    }
    public function Remove($key)
    {
       ...
    }

    public function Size()
    {
     ....
    }

    public function IsEmpty()
    {
    ....
    }

    public function GetObj($key)
    {
    .....
    }

    public function GetKey($obj)
    {
    .....
    }
    }

Here is some of my Tema class code

class Tema {
    private $texto;
    private $titulo;
    private $usuario;
    private $fecha;
    private $numeroRespuesta;

    function getnumeroRespuesta(){
        return $this->numeroRespuesta;
    }

    function getUsuario(){
        return $this->usuario;
    }
    function getTitulo(){
        return $this->titulo;
    }
    function getTexto(){
        return $this->texto;
    }
   ......

Then in my twig template I want to display the value of 'tema' but the results are empty values

</thead>
    <tbody id="cuerpo-tabla">
        <tr>
        {% for tema in temas %}
           <th width="10%">{{ tema.fecha  }}</th>
           <th width="70%">{{ tema.titulo }}</th>
           <th width="10%">{{ tema.usuario }}</th>
           <th width="10%">{{ tema.numeroRespuesta }}</th>

            {% endfor %}
        </tr>
    </tbody>

when i do var_dump($temas), the result is :

object(people\UsuarioBundle\Modelo\Tema)[287]
      private 'texto' => string '' (length=0)
      private 'titulo' => string 'titulo1?' (length=37)
      private 'usuario' => string 'PlayBackWow' (length=11)
      private 'fecha' => string '21:27' (length=5)
      private 'numeroRespuesta' => string '0' (length=1)
  1 => 
    object(people\UsuarioBundle\Modelo\Tema)[286]
      private 'texto' => string '' (length=0)
      private 'titulo' => string 'titulo2' (length=25)
      private 'usuario' => string 'OsoMiltro' (length=9)
      private 'fecha' => string '21:31' (length=5)
      private 'numeroRespuesta' => string '0' (length=1)
  2 => 

It seems you are trying to iterate a $temas variable that's an ArrayList object with no public properties. Furthermore the intended behaviour is to iterate over its internal $list property I think. To make your ArrayList object usable in a foreach context , try implementing Traversable and its concrete subinterfaces. Better yet, for simple requirements you could:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM