We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    Use placeholders to share data across scripts; plugins and snippets are eval()’d currently, and are run in there own variable scope, so you will not have access to the PHP variables directly outside of each script. Besides, globals are bad, ummmmkaaayyyy?!
      • 33372
      • 1,611 Posts
      @KBtechnics: What OpenGeek said. You can use MODx placeholders as you would global PHP vars, and you can also set session vars the same as you would outside of MODx. So your plugin code would be $modx->setPlaceholder(’aaaaa’,1); and you could then use that in any parsed output as [+aaaaa+] or get its value in a snippet using the getPlaceholder method.
        "Things are not what they appear to be; nor are they otherwise." - Buddha

        "Well, gee, Buddha - that wasn't very helpful..." - ZAP

        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
        • 2217
        • 21 Posts
        mmmmkaaaayyy!
        And how about functions? Which method is suggested to define a function in a (plugin?) and let it be usable in other (snippets?)?
        I see that inside many useful snippets, many useful general-purpose functions are defined and used. I mean debug functions, check-permission functions and so on. Wouldn’t it be better if those functions were defined in a xx plugin, and the snippet(s) making use of them were community-shared with the plugin too? Maybe this could lead to a smarter development of new snippets, aided by shared plugins.
        In a more strict sight, I’d like to build some snippets. They’d like to use general-purpose functions that I’d like to define once for all (and maybe share tomorrow, if good).

        As you can see I am a newbie in modX, but I’d like to start in the most polite way of building. Too many times I said "ooh how easier it would have been if I just knew this before!", not talking just about sex.

        thanks
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: KBtechnics at May 17, 2008, 05:25 PM

          And how about functions? Which method is suggested to define a function in a (plugin?) and let it be usable in other (snippets?)?
          I see that inside many useful snippets, many useful general-purpose functions are defined and used. I mean debug functions, check-permission functions and so on. Wouldn’t it be better if those functions were defined in a xx plugin, and the snippet(s) making use of them were community-shared with the plugin too? Maybe this could lead to a smarter development of new snippets, aided by shared plugins.
          In a more strict sight, I’d like to build some snippets. They’d like to use general-purpose functions that I’d like to define once for all (and maybe share tomorrow, if good).

          As you can see I am a newbie in modX, but I’d like to start in the most polite way of building. Too many times I said "ooh how easier it would have been if I just knew this before!", not talking just about sex.
          You can define functions any way you want to. However, reuse is what classes and other object-oriented concepts are meant to help you achieve. You can create instances of a single class you develop in plugins, snippets, modules, etc, and they will each inherit the common behavior and/or properties you define for the class. This, IMHO, is the optimal way to design libraries of reusable functions.
            • 2217
            • 21 Posts
            Ok.

            Let’s assume I define a class XClass in a plugin.
            Shall I

            A - create an XObject and assign it to a placeholder inside the same plugin,
            Class XClass (...etc...)
            $XObject=new XClass;
            modx->setplaceholder('plc_obj', $XObject);

            to be able to use it in other scripts (reusing the object with a getplaceholder)

            OR

            B - a class definition inside a plugin is valid outside too? Is there a "kind of" placeholder to export classes in other scripts?

            If you’ve got an example of a simple reusable function and its use in other scripts, please tell me where I can find it.

            I thank you in advance.