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
    I’ve been playing with the new snippet properties and have to say the whole concept is fantastic. I love it. The user interface is a little rough around the edges and I assume that it’s not ready for bug reports yet, but the proof-of-concept seems to be pretty solid. After looking at how the properties are shoehorned into a single DB field, I’m both *very* impressed and glad I didn’t have to do the coding. wink

    In Revolution, snippets can still be controlled as they were in 0.9.6, with parameters, but developers now have the option to have the default values set via a grid in the Manager. For snippets meant for use by naive users, you have the option to let some users set the options in the grid and use no parameters at all in the snippet call, while others use parameters to override the default options. Another plus is that the grid can serve as a complete parameter list that the user doesn’t have to hunt down -- complete with descriptions, default values already set, and, optionally, a drop-down list of possible values. No backticks, no ampersands, no worries about typos, case-sensitivity, or formatting.

    In your snippet, you can just do:
    $obj = $modx->getObject('modSnippet',array('name'=>'snippet-name-here'));
    $myConfig= $obj->getProperties();


    Then in your code, any property value set in the grid is available like so:

    if ($myConfig['property-name-here']) {
       doSomething();
    }

    As usual, I have questions and a thought or two.

    First, when creating properties from a snippet or build script, what should go in the "options" field? I’m guessing "0,1" for a boolean and maybe "" for a textfield. Can this field be omitted in the array that goes to setProperties()?

    Second, I see combo-boolean as an option but not just boolean. In the current implementation, users can type anything at all for the value of one of these. Will there be a way to have a straight boolean where they can only pick yes and no and have the values set to 1 and 0?

    Wishlist:

    Since these are default properties, I’d like to override them with values set in the snippet call. An overrideProperties() method that would automatically get the parameters and the properties of the current snippet, do the replacements, and return the merged array would be very cool. If it would let me optionally name the snippet to get the properties from, that would be even cooler (yeah, I actually have a use for that wink ).

    A popup or js expansion for the description field in the grid so it can contain more information about the property and how to use it.

    Import and export would also be nice, but not critical.

    Here’s some working code that sets and gets properties in case people are curious. I had to guess at how to do it so don’t take this as the proper way.

      global $modx;
    /* get the snippet object */
      $obj = $modx->getObject('modSnippet',array('name'=>'TestSnippet'));
    
      if (!$obj) {
          return("Object not found<br>");
      }
    
    /* set and save some properties */
    
      $props = array(
        array(
            'name'=>'isCool',
            'desc'=>'Is this thing cool, or what?',
            'type'=>'combo-boolean',
            'options'=>"",
            'value'=> true
        ),
        array(
            'name'=>'howCool',
            'desc'=>'Coolness description here',
            'type'=>'textfield',
            'options'=>"",
            'value'=> 'The best thing since sliced bread'
        )
      );
    
      $obj->setProperties($props);
      $obj->save();
    
    /* get and display the properties */
    
      $propArray = $obj->getProperties();
    
      foreach ($propArray as $key=>$value) {
          $output .= $key . ": " . $value . "<br>";
      }
    
      return $output;


    Of course, you can also set the properties in the GUI grid on the properties tab of the snippet create/edit page (you may need the latest SVN version).
      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
      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: BobRay at Oct 24, 2008, 12:22 AM

      In your snippet, you can just do:
      $obj = $modx->getObject('modSnippet',array('name'=>'snippet-name-here'));
      $myConfig= $obj->getProperties();


      Then in your code, any property value set in the grid is available like so:

      if ($myConfig['property-name-here']) {
         doSomething();
      }

      Actually, in snippets, plugins, and modules, these properties are already merged with tag or passed parameters and available in the scope of the function that is generated to represent the element. You can access them altogether as $scriptProperties or individually as PHP vars (they are extracted before the script is executed).
      <?php
      // via $snippetProperties
      if (isset($scriptProperties['property_name'])) doSomething($scriptProperties['property_name']);
      
      // directly as PHP var
      if (isset($property_name)) doSomething($property_name);
      ?>


      Quote from: BobRay at Oct 24, 2008, 12:22 AM

      First, when creating properties from a snippet or build script, what should go in the "options" field? I’m guessing "0,1" for a boolean and maybe "" for a textfield. Can this field be omitted in the array that goes to setProperties()?

      Second, I see combo-boolean as an option but not just boolean. In the current implementation, users can type anything at all for the value of one of these. Will there be a way to have a straight boolean where they can only pick yes and no and have the values set to 1 and 0?
      Yes, the ’type’ and ’options’ fields can be omitted and the result is they get a type of string with no options. The ’desc’ is also optional. All that is required is name and value, and they can be submitted as a simple associative array or as nested arrays with name, value, and any of the optional details. And yes, you can create a type ’combo’ with any options you want, including names (to serve as labels, i.e. Yes/No), and values submitted for those labels (i.e. 1/0).

      Quote from: BobRay at Oct 24, 2008, 12:22 AM

      Since these are default properties, I’d like to override them with values set in the snippet call. An overrideProperties() method that would automatically get the parameters and the properties of the current snippet, do the replacements, and return the merged array would be very cool. If it would let me optionally name the snippet to get the properties from, that would be even cooler (yeah, I actually have a use for that wink ).
      They are automatically merged and available this way through $scriptProperties as I described.

      Quote from: BobRay at Oct 24, 2008, 12:22 AM

      A popup or js expansion for the description field in the grid so it can contain more information about the property and how to use it.
      Fair enough.

      Quote from: BobRay at Oct 24, 2008, 12:22 AM

      Import and export would also be nice, but not critical.
      How would this be different than distributing via a transport package?
        • 28215
        • 4,149 Posts
        It’s definitely ready for bug reports. If you have em, send em. tongue
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 3749
          • 24,544 Posts
          Quote from: OpenGeek at Oct 24, 2008, 10:49 AM

          Quote from: BobRay at Oct 24, 2008, 12:22 AM

          First, when creating properties from a snippet or build script, what should go in the "options" field? I’m guessing "0,1" for a boolean and maybe "" for a textfield. Can this field be omitted in the array that goes to setProperties()?

          Second, I see combo-boolean as an option but not just boolean. In the current implementation, users can type anything at all for the value of one of these. Will there be a way to have a straight boolean where they can only pick yes and no and have the values set to 1 and 0?
          And yes, you can create a type ’combo’ with any options you want, including names (to serve as labels, i.e. Yes/No), and values submitted for those labels (i.e. 1/0).

          I just want a straight boolean like the yes/no options in system settings, not a combo (unless combo means something different than what I’m used to -- the combos I’m familiar with let the user type in a value). So far, I haven’t been able to make it work.

          Quote from: OpenGeek

          Quote from: BobRay at Oct 24, 2008, 12:22 AM

          Import and export would also be nice, but not critical.
          How would this be different than distributing via a transport package?

          I wouldn’t want it for distribution. I just thought it might be a handy tool for development -- archiving, testing, SVN, diffs, etc. It could be "included" in the build file if the format is right. I can definitely live without it.

          Quote from: OpenGeek at Oct 24, 2008, 10:49 AM

          Actually, in snippets, plugins, and modules, these properties are already merged with tag or passed parameters and available in the scope of the function that is generated to represent the element. You can access them altogether as $scriptProperties or individually as PHP vars (they are extracted before the script is executed).

          Sweet. As usual, it’s a real challenge to think up stuff I want that’s not already there. wink
            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
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: BobRay at Oct 25, 2008, 12:21 AM

            I just want a straight boolean like the yes/no options in system settings, not a combo (unless combo means something different than what I’m used to -- the combos I’m familiar with let the user type in a value). So far, I haven’t been able to make it work.
            Right, sorry, a combo in ExtJS terms is a select with options (i.e. a drop-down box). I don’t think combo-boolean (the Yes/No option in type) is supposed to be there. I’ll remove that. Use type "list" and it will allow you to define the options with name and value.
              • 3749
              • 24,544 Posts
              Got it. Us old-time Windows programmers are used to distinguishing between a drop-down box (in which the choices are limited to what’s there) and a "drop-down combo box" where there are some choices available but also a textfield for the user to enter something.
                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
                • 3749
                • 24,544 Posts
                @OpenGeek,

                In your post above, I think your code example shows $snippetProperties in a couple of places where it should be $scriptProperties.
                  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
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: BobRay at Nov 01, 2008, 02:57 AM

                  @OpenGeek,

                  In your post above, I think your code example shows $snippetProperties in a couple of places where it should be $scriptProperties.
                  Corrected; thanks BobRay.
                    • 3749
                    • 24,544 Posts
                    Quick question -- should the "value" items for setProperties() below be in quotes or not?


                    array(
                        'name'=>'useTimer',
                        'desc'=>'Use max and min time limits for the form',
                        'type'=>'combo-boolean',
                        'options'=>'',
                        'value'=>'0'
                    ),
                    
                    array(
                        'name'=>'useTimerMin',
                        'desc'=>'Minimum time allowed (seconds)',
                        'type'=>'integer',
                        'options'=>'',
                        'value'=>'10'
                    ),
                      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
                      • 22303 MODX Staff
                      • 10,725 Posts
                      For consistency when developing snippets that use properties, yes, it’s better to treat all values as strings since that is how they are treated in tag form.