Search code examples
phpparameter-passingmodxmodx-revolutionchunks

How to pass variables to a sub-chunk in modx


I have a snippet in ModX that looks something like this:

$array = array(
    'id' => 1,
    'title' => 'Title of Story',
    'content' => 'Content of story...'
);

echo $modx->getChunk('chunk_story_page', $array);

My story page HTML looks something like this:

<div class="story">
    <h1>[[+title]]</h1>
    <div class="content">
        [[+content]]
    </div>
</div>

Now I want to be able to call another chunk from within that chunk and pass my data through it. I placed the following below the above HTML.

[[$chunk_story_page_extra &title=`[[+title]]`&content=`[[+content]]`]]

It goes without saying anything, but the line above is not producing any output.

Any clue on what I might have done wrong on that line? I'm sure it has something to do with the syntax.


Solution

  • You're missing a question mark after the chunk name:

    [[$chunk_story_page_extra? &title=`[[+title]]` &content=`[[+content]]`]]
    

    You could also do this, might be slightly more efficient too:

    $array = array(
        'id' => 1,
        'title' => 'Title of Story',
        'content' => 'Content of story...'
    );
    $array['chunk_story_page_extra'] = $modx->getChunk('chunk_story_page_extra', $array);
    
    echo $modx->getChunk('chunk_story_page', $array);
    

    And in your chunk:

    [[+chunk_story_page_extra]]
    
    <div class="story">
        <h1>[[+title]]</h1>
        <div class="content">
            [[+content]]
        </div>
    </div>