在树枝模板中显示 symfony2 对象值

displaying symfony2 object values in twig template

我在将对象值传递到我的 Twig 模板时遇到问题。

这是我的一些显示对象内容的控制器代码:

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

这是我的 Arraylist 代码。

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)
    {
    .....
    }
    }

这是我的一些主题 class 代码

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;
    }
   ......

然后在我的树枝模板中,我想显示 'tema' 的值,但结果是空值

</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>

当我执行 var_dump($temas) 时,结果是:

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 => 

您似乎正在尝试迭代一个 $temas 变量,它是一个没有 public 属性的 ArrayList 对象。此外,我认为预期的行为是迭代其内部 $list 属性 。 要使您的 ArrayList 对象在 foreach 上下文中可用,请尝试实现 Traversable 及其具体子接口。 更好的是,对于简单的要求,您可以:

  • 只需使用简单的原生 php array
  • 使用ArrayObject