1

残念ながら foreach ループで同じ更新されていないチャンクを返す次のコードがあります。取得されたチャンクには、レコードごとに更新する必要があるいくつかのプレースホルダーが含まれています。誰かが理由を知っていますか?getChunk を使用する代わりに getObject を使用して処理します。

$chunkie = $modx->getObject('modChunk', array('name' => 'thumbTemplate'));
foreach ($items as $item) {

        $itemArray = $item->toArray();
        $itemArray['idx'] = $idx;
        (...)

$output .= $chunkie->process($itemArray);
$idx++;
};
4

2 に答える 2

1

ループ内のチャンクを取得する必要があります...

//$chunkie = $modx->getObject('modChunk', array('name' => 'thumbTemplate'));

foreach ($items as $item) {

    $itemArray = $item->toArray();

    $itemArray['idx'] = $idx;

    (...)

    $output .= $modx->getChunk('thumbTemplate',$itemArray);

    $idx++;

};

getObjectメソッドを使用してチャンクプレースホルダーも設定できるかどうかはわかりません。[実際、私はあなたができないと確信しています]

アップデート

これを試して:

<?php
$output = '';

$items = array(
    'apples'=>'bananas',
    'orange'=>'orange juice',
    'peaches'=>'peach cobbler'
    );

// use a query to retrieve your actual chunk from the db
$tpl = '[[+key]] = [[+value]] <br />';


foreach ($items as $key => $value) {

    $itemArray = array(
        'key'=>$key,
        'value'=>$value
    );

    $chunkie = $modx->newObject('modChunk');
    $chunkie->setContent($tpl);

    $output .= $chunkie->process($itemArray);

};

return $output;

明らかに、私はいくつかの小さな変更を加えたので、切り取って貼り付けて動作を確認し、主要部分をコードに適合させるだけです。

于 2012-10-15T18:53:06.997 に答える
0

これは古い質問であることは知っていますが、これがプレースホルダーを更新する最速の方法だと思います。

$chunkId = 12; // id of the stored chunk
$chunk = $modx->getObject('modchunk', $chunkId);

/* If you use getChunk(), the placeholders will be processed, 
   which you don't want */
$content = $chunk->getContent();

$tempChunk = $modx->newObject('modChunk');

/* This might have to go in the loop */
$tempChunk->setCacheable(false);

foreach ($items as $item) {
   $itemArray = $item->toArray();
   $itemArray['idx'] = $idx;
   // (...)
   $tempChunk->setContent($content);
   $tempChunk->setProperties($itemArray);

   $output .= $tempChunk->process();
   $idx++;
}
于 2014-02-05T07:57:37.003 に答える