We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16942
    • 33 Posts
    So I’ve tried searching in the forums to little or no avail. Can someone point me to a decent quality tutorial on how to use properties and property sets as the help documentation is unhelpful.

    I’m trying to assign a property set to a template with a bunch of default values and then have a snippet to read one of those values. I’ve interrogated the $scriptProperties array in the snippet and it’s empty. Furthermore whenever I edit the template and try to attach a property set, it says it’s saved but you go back into to edit the template again and the settings are lost. So it’s either a bug or user incompetence...and I’m happy to concede the latter for now.

    Thanks!
      • 3749
      • 24,544 Posts
      Try creating the property set on the Properties tab of the template (click on the "Create New Property Set" button). Add properties to the set and be sure to save the template itself when you’re finished.

      When you go back to the template to edit the property set, you have to select it in the drop-down list on the Properties tab.

      To get the properties in code, you need to get the property set, then get the property set’s properties. That second step gives you the equivalent of the $scriptProperties array.

      <?php
      $set = $modx->getObject('modPropertySet',array('name'=>'MyPropertySet'));
      $props = $set->getProperties();
      
      return print_r($props, true);
      


      Note that you don’t need the template object to do this because property sets are independent objects. You can get the property set via the template, but it’s usually not practical because there can be multiple property sets attached to the template and you’d have to know the name of the property set you wanted anyway.

      If there will only be one property set attached to the template, you could use the template’s default properties instead (also on the Properties tab -- click on the "Unlock Default Properties" button). To get those in code, you’d have to have the template object and then use $template->getProperties();

      I’m pretty sure that using a property set would be faster and more efficient (and more convenient if you want to change the property values in your code).

      Speaking of efficiency, it would also be slightly faster to look up the property set’s ID and use that in the getObject() call above:

      $set = $modx->getObject('modPropertySet', $id);


      I think you might have to look in the DB to get the ID, though. I don’t think it appears in the Manager.



        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
        • 16942
        • 33 Posts
        OK so experimenting with the first approach and I can’t get it to work.

        I have a template, with a new property set called ’Navigation’ and a property called ’ActiveSection’ (see here: http://www.mediafire.com/i/?aqa7rdgjdnxxaz8 )

        In the template I’m calling a snippet called Navigation - see:
        	<div class="indent">
        [[!Navigation]]
                </div>
        


        The Navigation snippet has:
        <?php
        $set = modx->getObject('modPropertySet',array('name'=>'Navigation'));
        $props = $set->getProperties();
        
        return print_r($props, true);
        


        And the resulting page based on this template returns a white page of nothingness.

        Where might this be going wrong?

        Thanks
        • You should never need to access a PropertySet object directly. This is part of Elements and manually reading this data is not the intention. PropertySets cannot be applied to Templates since there is no tag to call a Template—it is the "base" Element of a Resource. If you want to use the default properties set for a Template in a Snippet, you’ll need to pass them to the Snippet explicitly.
            • 16942
            • 33 Posts
            Can I ask you for some more detail, including code examples - both for my own understanding and for those to tread this path behind me as the documentation is so light?

            As a starter for 10, how can we set a property value for a template and then make it available in a snippet? Right from the basics please! You can probably sense the use case below is that we want to tag some templates as being part of a particular section of the site and be able to read that section in a snippet.

            Thanks!
            • Quote from: leoandrews at Mar 18, 2011, 10:51 PM

              Can I ask you for some more detail, including code examples - both for my own understanding and for those to tread this path behind me as the documentation is so light?
              Template property values, just like Chunk property values, or Template Variable property values, are set as placeholders for the scope of execution of that Element. So, if you have a Template default property for the key food set to the value beer, then you can pass that to a Snippet, e.g.
              [[Snippet? &food=`[[+food]]`]]
              or you can grab it via API with
              $modx->getPlaceholder('food');
                • 3749
                • 24,544 Posts
                Now I’m confused. How could that code not work? The property set is an object, so getObject() should get it. And it has properties, so it seems that it should have a getProperties() method.

                And, if you wanted to access a property set’s properties in a snippet or plugin, independent of any element (which I can imagine some uses for), how else would you do it?

                I’m 97% sure that I tested that code at one point and it worked. huh
                  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
                  • 16942
                  • 33 Posts
                  Quote from: BobRay at Mar 19, 2011, 03:08 AM

                  Now I’m confused. How could that code not work?

                  Umm well you’re the coding team ;-)

                  It killed the page - got a big fat load of nothing
                    • 3749
                    • 24,544 Posts
                    Maybe my credentials should be revoked. wink

                    I believe you. My question was meant for OpenGeek.

                    [Update]
                    I’ve confirmed that the code works fine. I think your problem is that you used the name of the snippet rather than the name of the property set.

                    I suspect that a property set is not what you want, but I beg to differ on the need to ever get a property set object directly in code. How else would you send its properties in a call to getChunk() or runSnippet()?

                    It wouldn’t happen very often, but I can imagine cases where you might want to select a particular property set in code or with the value of a TV and use its properties with getChunk() or runSnippet().

                    You might also want to let users update a property set’s properties using a form in the front end.
                      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
                    • I am so confused by this discussion BobRay. PropertySets are used by Elements automatically, plainly and simply, e.g. [[Element@PropertySet]]. I’m not saying you can’t get the object from the database, but it was never intended to be used that way is what I’m saying. Feel free to find use cases and innovate with it, but you do not ever need to do that to work with Elements and property sets as they were originally designed to be used.