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

    I have an xpdo set up to handle data in an external db.
    That works fine

    but now as I am coding my site,
    I find that I reuse some code quite a lot.

    for example getting categories from my external db and putting them in an array.
    Instead of letting each snippet execute that code over and over,
    I would like to put it in a snippet of his own and call that from within the other snippets.
    that way I can call my categories cached and I don't need to recode it each time!
    But I can't seem to get that working: I always get the error "unexpected [" that is the [ from the snippet call in my snippet

    can you still follow?
    example [[blabla]] works fine
    [[getcategories]]also works fine
    
    but if I use [[getcategories]] in [[blabla]] I get the error above


    can anyone help? [ed. note: djemmers last edited this post 12 years, 2 months ago.]
      www.tornooi.net online database for (sport) events in dutch.
      To be redesigned, rewritten to modx with multiple languages...
      ANY help appreciated, I cannot offer money but I can offer free advertising on the site. just message me.
    • You can't use snippet tags in PHP code. Use the API function instead.
      http://rtfm.modx.com/display/revolution20/modX.runSnippet
        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
        • 32316
        • 387 Posts
        You need to use code tags in your post - otherwise we the readers do not see anything
        code goes inside  code tags
        any code not inside code tags gets eaten by the code monster
        good idea to click the view button to confirm your post content


        If you want to run a snippet from within a snippet you can use modX.runSnippet - see here for information and syntax: http://rtfm.modx.com/display/revolution20/modX.runSnippet
          • 4172
          • 5,888 Posts
          Quote from: djemmers at Feb 28, 2012, 11:28 AM
          hi

          I have an xpdo set up to handle data in an external db.
          That works fine

          but now as I am coding my site,
          I find that I reuse some code quite a lot.

          for example getting categories from my external db and putting them in an array.
          Instead of letting each snippet execute that code over and over,
          I would like to put it in a snippet of his own and call that from within the other snippets.
          that way I can call my categories cached and I don't need to recode it each time!
          But I can't seem to get that working: I always get the error "unexpected [" that is the [ from the snippet call in my snippet

          can you still follow?
          example
          [[blabla]] 
          works fine
          [[getcategories]]
          also works fine

          but if I use
          [[getcategories]] in [[blabla]]
          I get the error above

          can anyone help?

          you can try

          [[blabla? &categories=`[[getcategories]]`]]


          [NOTE]

          you can't return an array by snippets, only string can be returned
          but you can return an json-string by your getcategories-snippet

          return $modx->toJson($mycategories);


          and get it back as array in your blabla-snippet by

          $mycategories = $modx->fromJson($categories);


            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 32316
            • 387 Posts
            Cross posted again - Have to stop getting up for coffee while posting!
              • 7575
              • 85 Posts
              tnx guys, I got it to work now!
                www.tornooi.net online database for (sport) events in dutch.
                To be redesigned, rewritten to modx with multiple languages...
                ANY help appreciated, I cannot offer money but I can offer free advertising on the site. just message me.
                • 7575
                • 85 Posts
                apparently I did not get it working:
                my inner snippet:
                getcategories ends with:
                return $modx->toJson($categories);


                outer snippet:
                blabla
                $mycategories=$modx->fromJson($modx->runSnippet('getcategories'));
                print_r($mycategories);


                but the blabla snippet shows 2 arrays:
                first the array I want ( the print_r command)
                but after that also the same array in another format ( I presume the Json format)

                how can I get only the print_r one?
                  www.tornooi.net online database for (sport) events in dutch.
                  To be redesigned, rewritten to modx with multiple languages...
                  ANY help appreciated, I cannot offer money but I can offer free advertising on the site. just message me.
                  • 7575
                  • 85 Posts
                  actulaly
                  above code works.
                  I had another call to getcategories somewhere else...

                  so tnx guys,

                  solved
                    www.tornooi.net online database for (sport) events in dutch.
                    To be redesigned, rewritten to modx with multiple languages...
                    ANY help appreciated, I cannot offer money but I can offer free advertising on the site. just message me.