We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25560
    • 16 Posts
    hi


    On my template I need to determine if the document requested has a particular parent.

    I can determine if the document requested has the parent I am looking for with:

    [[UltimateParent? &id=`[*id*]`]]

    this returns the parent of the current document.

    but I am unsure how i would put this information to use. I guess it needs to go in a placeholder for use within the TEMPLATE. I am unsure how to do this.

    then on my template I will have:


    if($currentDocumentsParent==X) { echo ’specific content’; }

    thanks.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      To get the current document’s immediate parent, use [*parent*]. The UltimateParent snippet is for getting the top-level ancestor of a document two or more levels deep in the tree structure.

      In a snippet, you would use $modx->documentObject[’parent’]. You can use a snippet like this:
      if($modx->documentObject['parent'] == X) {return $this;} else {return $that;}
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 25560
        • 16 Posts
        hi

        that makes sense but I get this error:


        Fatal error: Call to a member function on a non-object in manager/includes/document.parser.class.inc.php on line 836

        on my template I have:
        [[OpenBlogDiv]]
        [*content*] 
        [[CloseBlogDiv]]
        

        snippets:

        OpenBlogDiv

        <?php
        if($modx->documentObject[’parent’] == 2) {
        $open=’<div class="start_my_div">’;
        return $open;
        }
        ?>

        CloseBlogDiv

        <?php
        if($modx->documentObject[’parent’] == 2) {
        $close=’</div>’;
        return $close;
        }
        ?>

        I am trying to add an extra div to my content section if the document being requested is a child of a given parent (2 in this case). document 2 is a container. the reason for this is that otherwise my client would need to manually add the <start_my_div> div every time they created a new document INSIDE container#2.

        I had hoped I could stop this by modifying the template:

        if($documentParent=2) { print the open div }
        [*content*]
        if($documentParent=2) { print the close div }

        this is only on certain pages. perhaps there is a better way of doing it?

        hope this is clear enough

        thanks
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Can you post the entire error message? There should have been at least one more line.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 25560
            • 16 Posts
            I thought there should be as well, but no, it is only one line:

            the source of the page is

            <br />
            <b>Fatal error</b>:  Call to a member function on a non-object in <b>/manager/includes/document.parser.class.inc.php</b> on line <b>836</b><br />
            
              • 25560
              • 16 Posts
              intriguing

              looks like using $this as a return variable was messing it up

              serves me right for not using proper variable names. works fine now, thanks.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Ah, sorry; that was a very bad example to use!
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org