We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Just to whet your appetites for Revolution a little more, I thought I’d try to explain the new, and almost too cool, custom sets of properties in MODx Revolution. The properties can be used with any element (as OpenGeek keeps reminding me), but I’m going to explain their use with snippets, which will probably be the most common use for them.

    Snippet properties are basically the things with the ampersands you’re used to putting as parameters in the snippet call. They control the behavior of the snippet.  In 0.9.6 and before, the snippet programmer usually had to check to see if you sent them and, if not, set them to their default values.  As a user, you had to either look at the code or find documentation somewhere on what each property was for. You had to get the case right, remember the ampersand, and use back-ticks.

    In MODx Revolution, the snippet properties and their default values appear in a grid on the properties tab of the create/edit snippet screen. Each one has a drop-down box that explains what it’s for. For the snippet developer, they come to the snippet automatically with their values already set (but with a twist -- more on that later).

    You can override the default values in the grid by using &parameters in the snippet call, just like always. But, and here’s where it gets really cool, you can also create named custom sets of properties and edit the values right on the screen in a grid, then just send the name of the cusom property set to the snippet.

    Here’s an example:

    I’ve installed SPForm, which automatically sets up the default values and a working contact form on your site. The install is completely automatic and, if mail() is enabled, there’s nothing left to do but some styling. But on my XAMPP install, there’s no mail() server so I have to use SMTP. There are also a number of properties I’d like to tweak for a local install. I could type all the SMTP parameters and other defaults I want to override into the snippet call, but then, when I move the site to the remote server, they’ll all be there mucking things up.

    Instead, I go to the SPFom edit screen, click on the Properties tab, and select "create new property set." I give it the name "Local".  It’s immediately there on the screen in grid form. I edit the SMTP and other options in about 20 seconds and save the set.

    I change the snippet call to:
    [[!SPForm@Local]]
      I’m done.  When I move the site to the remote location, I remove the "@Local" (or create a new set and change it to @Remote). I can also update or reinstall the snippet without losing any of my custom properties and I can change them easily in the grid at any time -- no ampersands, no back-ticks, no worrying about whether it’s "startID" or "startId" and often, no typing at all (drop-down lists).  wink

    If I use a snippet (e.g. Wayfinder or Ditto) on different pages or more than once on a page and want to have different parameter values for each call, in a flash I can create as many custom property sets as I want.

    For the snippet programmer, all the values come automatically in an array ($scriptProperties). So they’re all available with $scriptProperties[’someParameter’]. Properties set in any custom property set specified in the snippet call override the defaults. In the custom property set, you can set as few or as many properties as you like. It could be just a few, or you could end up setting all of them. If, for some reason, you want to override the value of a parameter in a custom property set, you can still do that because the old-fashioned &parameter in the snippet call, will override anything else.  All this is done automatically behind the scenes. The $scriptProperties array available in the snippet already has the final set of values.

    Here’s a quick crib sheet on how the properties are parsed.


    • The default property values are all available to the snippet unless overridden.
    • A value set in a custom property set named in the snippet call will override the default value.
    • A value set with &parameter in the snippet call will override both the default and any custom property set setting.


    I also want to take this opportunity to confess that I argued strongly for a different way of handling snippet properties and boy am I glad that I wasn’t successful.  This is the cat’s pajamas (and I’ve got a really gigantic cat).

    If you haven’t yet installed MODx Revolution, trust me, it’s worth doing just to see this feature (and there’re no flies on the package installation system).  I’m not sure which one will "revolutionize" MODx more. Once you get used to the new features (and honestly, it takes very little time) you’ll be holding your breath waiting until you can use all this on a live site.

      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 26903
      • 1,336 Posts
      Yes, this is powerful, I just used this to customise my SPForm install to use SMTP not mail() but I just changed the snippet properties, I didn’t realize you could have different sets as you’ve described above, this is even better. good stuff.
        Use MODx, or the cat gets it!
        • 7231
        • 4,205 Posts
        Nice write up, sounds very interesting indeed. Now you’ve opened the can of worms:

        How does this work from the snippet developers POV? Is there a process needed to ’feed’ the properties information into the manager interface and provide data for the drop menus and other options? Are using properties required for snippets or is it optional? Can property sets be exported and shared (xml, txt or php)? Are there ’display widgets’ for different types of properties like TVs (if a parameter is date will it be a text filed or have a calender widget)? Is there a method for validating property parameter settings? Is there an api for snippet parameters to make it easier to interface with the properties and other forms of parameter input?

        One of the things that I find so friendly about modx is the simple snippet developing process. How do these new features affect the average simple ’utility’ snippet for stuff like date formatting. Or is this an advanced feature for super snippets like Ditto and play no role in simple everyday snippets?
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don't know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: dev_cw at Dec 20, 2008, 06:31 AM

          How does this work from the snippet developers POV? Is there a process needed to ’feed’ the properties information into the manager interface and provide data for the drop menus and other options? Are using properties required for snippets or is it optional?
          Every element has default "string" properties that can be set when editing an element. All properties are strings and represent properties that should be available to the element when it is processed with no additional tag properties provided by the user. Additional sets can be defined stand-alone and attached to one or more elements (even of different types, i.e. chunks and snippets and tvs can share a single property set), or directly created and edited with the element.

          Quote from: dev_cw at Dec 20, 2008, 06:31 AM

          Can property sets be exported and shared (xml, txt or php)?
          They can be packaged and shared like any data object in MODx, and as simple arrays of properties, they can be serialized and re-used in just about any format imaginable.

          Quote from: dev_cw at Dec 20, 2008, 06:31 AM

          Are there ’display widgets’ for different types of properties like TVs (if a parameter is date will it be a text filed or have a calender widget)? Is there a method for validating property parameter settings? Is there an api for snippet parameters to make it easier to interface with the properties and other forms of parameter input?
          No, properties do not have widgets; they are always simple key/value pairs, though developers can define simple option lists for expected values which are presented as drop downs. Snippet parameters are just another name for element properties, and yes, they are used throughout the API. The parser provides methods for parsing and transforming properties, property sets themselves are objects, and they are related to (or made available to) elements via a link table which is also an accessible MODx data object. Remember in Revo, all tags can now have properties (aka tag parameters) and likewise can use property sets for convenient access to predefined configurations:
          [[!Snippet@SnippetPropSet1]]
          [[*TemplateVar@TemplatePropSet1? &additionalProperty=`additional value`]]
          [[$Chunk@ChunkPropSet1? &overridden=`I overrode this property from ChunkPropSet1`]]
          

          As for validation, well, that’s still the responsibility of scripts themselves IMO, so no, there is not currently any feature available for validating the property values against defined options or otherwise.

          Quote from: dev_cw at Dec 20, 2008, 06:31 AM

          One of the things that I find so friendly about modx is the simple snippet developing process. How do these new features affect the average simple ’utility’ snippet for stuff like date formatting. Or is this an advanced feature for super snippets like Ditto and play no role in simple everyday snippets?
          They affect everything by making these features available to the developer, nothing more. You can still create all kinds of elements in the manager without any default properties or property sets. Obviously, more complex components will have more use for the facilities, but otherwise, it’s business as usual.
            • 25663 MODX Staff
            • 12,272 Posts
            Let me elaborate on a usage example for the chunk sample Jason listed above. Two pieces are at play here. Well maybe three. The first is named property set: ChunkPropSet1:
            overridden -> This is some text that's the value for ChunkPropSet1


            The possible/optional second would be the optional Default property set:
            overridden -> This is some default text.


            Then there’s the hypothetical chunk itself:
            This demonstrates parsing placeholders in the new Revo core. [[+overridden]]


            So the following calls create the outputs below:
            [[$Chunk@ChunkPropSet1? &overridden=`I overrode this property from ChunkPropSet1`]]
            
            This demonstrates parsing placeholders in the new Revo core. I overrode this property from ChunkPropSet1
            


            [[$Chunk@ChunkPropSet1]]
            
            This demonstrates parsing placeholders in the new Revo core. This is some text that's the value for ChunkPropSet1
            


            [[$Chunk]]
            
            This demonstrates parsing placeholders in the new Revo core. This is some default text.
            



            If the Chunk didn’t have a default value set (the optional bit) it would have worked as follows:
            [[$Chunk]]
            
            This demonstrates parsing placeholders in the new Revo core.
            


            The utility that this adds to the core is really staggering. Now mix it in with the ability to format output using the same syntax as PHx, or with a custom formatting snippet, no more worries about mixed cached/uncached snippets (they all just work now), and the ability to put snippet calls and html as parameters/properties and we’ve got a pretty cool system that’s going to revolutionize the heck out of MODx. Amongst other things of course. wink
              Ryan Thrash, MODX Co-Founder
              Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • 7231
              • 4,205 Posts
              Thanks for the answers to so many questions. grin
              Remember in Revo, all tags can now have properties (aka tag parameters) and likewise can use property sets for convenient access to predefined configurations:
              It is not a matter of forgetting, it is just that this is so unbelievably cool that it is beyond simple comprehension.

              They affect everything by making these features available to the developer, nothing more. You can still create all kinds of elements in the manager without any default properties or property sets. Obviously, more complex components will have more use for the facilities, but otherwise, it’s business as usual.
              So in the case of a date formatting snippet, we can either leave the parameters to the snippet call as usual, but we could also have a method to set the default format if we choose to. In relation to snippets, this like an extension of the 096x Properties tab (which provides an interface for default parameter configuration) combined with the config file principle (which allow for multiple setups).

              The utility that this adds to the core is really staggering. Now mix it in with the ability to format output using the same syntax as PHx, or with a custom formatting snippet, no more worries about mixed cached/uncached snippets (they all just work now), and the ability to put snippet calls and html as parameters/properties and we’ve got a pretty cool system that’s going to revolutionize the heck out of MODx. Amongst other things of course.
              Simply incredible, can’t wait to try this stuff out. shocked
                [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                Something is happening here, but you don't know what it is.
                Do you, Mr. Jones? - [bob dylan]
                • 3749
                • 24,544 Posts
                Quote from: dev_cw at Dec 20, 2008, 06:31 AM

                One of the things that I find so friendly about modx is the simple snippet developing process. How do these new features affect the average simple ’utility’ snippet for stuff like date formatting. Or is this an advanced feature for super snippets like Ditto and play no role in simple everyday snippets?

                You can still develop snippets exactly as you did before. If you want to make them available via the WTF, though, you’ll have to develop a build script to make a transport package. Once you take that step, it’s a very short step further to add in the snippet properties (which are simply a standard array of values). Here’s an example from the snippet that does the "Thank you" page of SPForm:

                $properties = array (
                    array(
                        'name'=>'takeMeBack',
                        'desc'=>'Put a "take me back" link on response page',
                        'type'=>'combo-boolean',
                        'options'=>'',
                        'value'=>'1'
                    ),
                
                    array(
                        'name'=>'spfresponseTpl',
                        'desc'=>'SPF Response Template chunk name',
                        'type'=>'textfield',
                        'options'=>'',
                        'value'=>'spfresponseTpl'
                    ),
                array(
                    'name'=>'spfDebug',
                    'desc'=>'Print debugging info - Do not leave this on for a working site',
                    'type'=>'combo-boolean',
                    'options'=>'',
                    'value'=>'0'
                )
                );


                I keep them in a separate file (and the above is the entire file contents). Setting them is done like this:

                $props_file = "path_to_props_file";
                require_once $props_file;
                $c->setProperties($properties, true);


                Now users can set the properties in the grid and call the snippet with just [[!snippetName]]. They’ll also see the descriptions of what each paramater does as they set them.

                "options" BTW, is where you’d put the possible values for a drop-down list. For combo-booleans, you automatically get a "Yes/No" dropdown so I could have ommited that field.

                Trust me, none of this is as hard as it looks and the benefits are definitely worth it. laugh
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting