Zend partialLoop Görünüm Yardımcısı üzere ek parametreler Passing

4 Cevap php

Aşağıdaki gibi bir Zend görünümde ben bir iterable elemanı kısmi bir şablon uygulayabilirsiniz:

$this->partialLoop('template.phtml', $iterable);

Ancak şablon içinde, iterable mevcut $ sadece elemanları, kısmi ekstra verileri geçirerek başka bir yolu var mı?

4 Cevap

Ben kullanmak

$this->partialLoop('template.phtml', array(
    'data' => $iterable, 
    'otherVariable' => $otherVariable
);

Warning & Edit:

Tamamen dürüst olmak gerekirse, ben bir hata yaptım. Ben teklif kod çalışmaz sanırım. Ben kısmi () yardımcısı için yanlış. Çünkü verecek kişinin sınıfının bu kısmı çalışmaz:

foreach ($model as $item) {
    // increment the counter variable
    $this->partialCounter++;
    $content .= $this->partial($name, $module, $item);
}

Yerine "veri" tuşu tüm dizi üzerinde yineleme olacaktır. Nikolaus Dulgeridis D Teşekkür olduğuna işaret için: Ben cevap kabul edilebilir nasıl alamadım.

Atanan değişkenler mevcut değişkenleri ile çarpışır olmaz - böylece kısmi noktası görünümü örneği "temiz" oluşturur olduğundan bile $ this-> görünümü sayesinde herhangi bir ekstra veriyi açamazsınız.

Possible options

- Extend the view helper with methods to set custom variables

- Iterate the array and reformat it to

array(
    array('data' => $item1, 'id' => 1, 'totalCount' => 10) ,
    array('data' => $item2, 'id' => 2, 'totalCount' => 10) ,
    array('data' => $item3, 'id' => 3, 'totalCount' => 10) ,
)

- Use Registry to store the values.

Zend_Registry::set('partialLoopCount', $count);
$this->partialLoop($viewScript, $data);

- Dump partialLoop and use partial() instead

Ben bu çözümü tercih ederim.

$count = count($data);
foreach ($data as $key => $value) {
    echo $this->partial($viewScript, array('item' => $value, 'position' => $key, 'count' => $count));
}

Kısmi içinde, sizinle görünüm değişkenlerin tüm erişebilirsiniz:

$this->partialLoop()->view->myVariable

where myVariable is a normal view variable ($this->view->myVariable in the controller or $this->myVariable in the view, which it's actually the same thing). Basically, you retrieve the PartialLoop() object, then the view which called it, and then the variable itself.

This, though, will probably impact performance (and I don't think it's really MVC friendly...) But, hey: it works. :)

An example here: Hardcode.nl == Joris Osterhaus

Daha sonra (kısmi insert) bulundu:

$this->getHelper('PartialLoop')->view->otherVariable; 

Kontrol birimi

$this->view->otherVariable = 'otherVariable';

Template.phtml - "kısmi dosyada"

$this->otherVariable

(ZendFramework-1.11.4-minimal)