We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29264
    • 2 Posts
    This is my first posting. i’ve being play around with modx try to find out if it suitable for redeveloping my site. I’m not an expert at php but, with difficult I can put bits together.

    I’ve try to output a snippet using a chuck as a template. The chuck was being outputted within a loop, the problem was that it only replaces the placeholder with the last in the loop. I know it has something to do with the loop finishing before the snippet is parse. I found the posting:
    http://modxcms.com/forums/index.php/topic,12127.msg80789.html#msg80789

    but can not get it to work: this is what I have

    Chunk Code
    -------------------------------------------------------------------
    <div class="uv_listings2">
    <img src="[+ls_img_src+]" width="216" height="144" alt="" border="0">
    <h4>[+ls_page_title+]</h4>
    <p>
    [+ls_page__intro+]
    <strong>More info on: <a href="#">[+ls_page_title+]</a></strong>
    </p>
    <h6>PRICE:</h6>
    </div>
    -------------------------------------------------------------------

    Snippet code (top half borrowed from getChildIds snippets) and has been inserted as [[snippetName]]
    -------------------------------------------------------------------
    $docid = (isset($docid))? $docid: $modx->documentIdentifier;
    $children= $modx->getActiveChildren($docid, ’menuindex’, ’ASC’,’id’);
    if (!$children === false) {
    $idArray = array();
    foreach ($children as $c)
    {
    $idArray[] = $c[’id’];
    }
    //$output = implode(’,’,$idArray);
    $output = $idArray;
    } else {
    $output= $docid;
    }

    $nmOutput.= "";

    foreach ($output as $id)
    {
    $tvPagetitleArray = $modx->getTemplateVarOutput(’pagetitle’, $id, 1 );
    $tvIntrotextArray = $modx->getTemplateVarOutput(’introtext’, $id, 1 );

    $nmImageSrc = "assets/images/costa-rica/listings-pholder2.jpg";

    $chunkArr = array(
    ’ls_page_title’ => $tvPagetitleArray[’pagetitle’],
    ’ls_page__intro’ => $tvIntrotextArray[’introtext’],
    ’ls_img_src’ => $nmImageSrc
    );

    $nmOutput .= $modx->parseChunk(’recordList’, $modx->placeholders, ’[+’, ’+]’);

    } // end for loop

    echo $nmOutput;
    -------------------------------------------------------------------

    Nothing gets outputted! I’m new to this stuff so do not know where start looking.
    does ’$modx->parseChunk’ output an array or string? and if so how do I access it!

    Is there an easier way to do this or am i missing something. Basically i’m try to layout the children of a current page as a listing with title, link and image to the actual page.


    Sorry for such a long post, any help appreciated
    using version 0.9.6.1
    Thanks
      • 33372
      • 1,611 Posts
      Quote from: tammy at Mar 20, 2008, 08:49 PM

      Is there an easier way to do this or am i missing something. Basically i’m try to layout the children of a current page as a listing with title, link and image to the actual page.
      I was starting to go through your code and thinking of suggestions, and then I got to the line above at the end of your post. It seems like there’s a much easier to do what you need to do: Ditto! Is there some reason you haven’t tried to do this using the Ditto snippet, or were you just unaware of that option?
        "Things are not what they appear to be; nor are they otherwise." - Buddha

        "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
        • 22303 MODX Staff
        • 10,725 Posts
        You’re on the right track, except, no need for the getTemplateVarOutput -- you should have the variables you need from getActiveChildren() if you change it to include pagetitle and introtext fields. Those aren’t TV’s, but fields from the site_content or "Documents" table itself.

        Also, use the chunkArr in the parseChunk call (not $modx->placeholders), and rather than echo the output, just return it from the snippet.

        <?php
        $docid = (isset($docid))? $docid: $modx->documentIdentifier; 
        $children= $modx->getActiveChildren($docid, 'menuindex', 'ASC', 'id,pagetitle,introtext');
        $nmOutput.= "";
        if ($children) {
            foreach ($children as $child) {
                $nmImageSrc = "assets/images/costa-rica/listings-pholder2.jpg";
                $chunkArr = array(
                    'ls_page_title' => $child['pagetitle'],
                    'ls_page__intro' => $child['introtext'],
                    'ls_img_src' => $nmImageSrc
                );
                $nmOutput .= $modx->parseChunk('recordList', $chunkArr, '[+', '+]');
            } // end for loop
        } // end if
        return $nmOutput;
        ?>


        Finally, make sure the page isn’t cacheable.
          • 29264
          • 2 Posts
          Zap,

          Thanks for taking the time to point a newbie in the right direction. Ditto seem to do just what I wanted, but I’m learning php/modx so wanted to get my hands dirty. Any way I guess there is not need to invent the wheel twice!!

          Open Geek

          Cheers for the refined code and tips, I now have a better idea to what I can do from with snippets/modx. I still need to access other TV Data from the document so could I also pull it out from the child? like the example below?


          $children= $modx->getActiveChildren($docid, ’menuindex’, ’ASC’, ’id,pagetitle,introtext,listing_img_src’);

          or do I now need something like : $tvListingImgSrc = $modx->getTemplateVarOutput(’listing_img_src’, $child 1 );

          Cheers
          Tammy
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: tammy at Mar 21, 2008, 04:13 AM

            I still need to access other TV Data from the document so could I also pull it out from the child? like the example below?

            $children= $modx->getActiveChildren($docid, ’menuindex’, ’ASC’, ’id,pagetitle,introtext,listing_img_src’);

            or do I now need something like : $tvListingImgSrc = $modx->getTemplateVarOutput(’listing_img_src’, $child 1 );
            TV data must be retrieved with getTemplateVarOutput (this will be formatted for output, not the raw values) and multiple values can be gotten in one call; pagetitle and introtext are not TV’s, but are included in the current version of this function (for reasons I don’t understand).