We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21978
    • 59 Posts
    Hello again,

    Next problem occured with my site. I’d like to show some information from my own database table for each page. To take the data from the table I need a key which is "page id".
    Let’s say I have a template variable [*tvprice*] which contains a call to a non-param snippet called [!getBookPrice!]. Inside the snippet I’d like to automatically get the id of the current page. I can use $modx->documentIdentifier but it works OK only if I’m on this page.
    When I use ditto to display all subpages on a parent page, I get the id of a parent page.

    I thought that if I have "tv..." variable and set tagData="tvprice" in ditto call then my snippet will be executed within the context of the child page. But it is not, unfortunately.

    I know I can define a parameter for the snippet and set its value each time I call a snippet but I’d prefer to build a non-non-param snippet which takes the proper ID automatically.

    Is there a way to do it?

    Thanks
    Greg
      • 18397
      • 3,250 Posts
      You could skip the tvprice altogether in the Ditto call and just do [!getBookPrice?id=`[+id+]`!] and code in that $id is the current document unless $id is already set (IE from the snippet call).
        • 21978
        • 59 Posts
        Hi,

        Maybe I do something wrong, but when I change my call to [!getBookPrice?id=`[+id+]`!], I get an empty value and my query finds nothing.
          • 18397
          • 3,250 Posts
          Did you modify the snippet to handle the $id variable.
            • 21978
            • 59 Posts
            I’m not sure if I know how to do that.
            Could You please write me a simple example - just to display the id of the page for which the snippet is called.

            Thanks
            Greg
              • 18397
              • 3,250 Posts
              Snippet code:
              return $id;
              


              Snippet call:
              [[SnippetName?id=`#`]
              


              This will output whatever id is passed to it.
                • 21978
                • 59 Posts
                Hi,
                I don’t think we understand each other.
                My aim is to have a non-param snippet which will automatically determine the id of the page for which it is executed.
                Using documentIdentifier work good when I execute each page separately but when I use ditto, I get the id of the page on which I called ditto.

                Thx
                Greg
                  • 18397
                  • 3,250 Posts
                  You have to pass the ID of the document being parsed by Ditto on to your page specific snippet or it will continue to use the document Ditto is being called from.

                  You can have a simple line like this that will say if an ID is passed use that, otherwise use the current page:

                  $id = isset($id) ? $id : $modx->documentIdentifier;
                  
                    • 21978
                    • 59 Posts
                    Thanks !!!
                    Greg
                      • 30481
                      • 33 Posts
                      I have the same kind of problem.... and it’s not working for me....

                      If i am using [+id+] nothing happen on each page separtly..
                      In fact, i’m not sure that [+id+] is undertood wihout ditto context... (but [*id*] yes no problem)
                        -------------------------------
                        www.fabienletort.com