MODX Context Settings
Today we will be talking about Context Settings and how you can use these to your advantage in MODX.
Have you ever wanted to create a Template Variable that you would update in one place and it automatically updates throughout your site? If you have tried to do this with TVs you would probably have to create a hidden resource and assign some TVs to it then use a getResources call on all the pages you would need to use the values from this resource to accomplish this. This works but it's not practical and you also have some unnecessary getResources calls on all your templates which can affect your sites performance. If this is how you've done this in the past I'm going to show you how to achieve the same thing in a much easier way with Context Settings.
What are Context Settings?
Context Settings are essentially System Settings but for the Context, these are very handy when hosting multiple sites in a single MODX install because they allow you to specify different settings for each Context and share Chunks and Snippets across them all or any other MODX installations.
How to create a Context Setting
To create a Context Settings all you gotta do is click on the Context (Web) then click on the Context Settings Tab
Click on Create New button and the Create New Settings pop-up will come into view with the options for the new Context Setting. The Key field is how you want to access this setting when using it in MODX. The Field Type allows you to choose the type of for this setting, option include: Textfield, Textarea, Yes/No, Password, Category, Charset, Country, Context, Namespace, Template, User, User Group and Language. The Name field is the name this setting will display in the Context Settings table. The Namespace is used to associate this setting to a namespace (this can be left at core). The Description can be used to provide a brief description of what this setting is used for. The Area Lexicon Entry field allows you to create section in the Context Settings table, so if you want more than one setting grouped in the same are on the table make sure the Area Lexicon Entry for them are the same. The Value is the value the Context Setting will return when used in MODX.
How to use Context Settings in your site
So we went ahead and created some more Context Settings to hold some of our clients information.
Now let's say you have a footer chunk where you would like to display these Context Settings, the code for this chunk would look something like this.
Client Name: [[++client-name]]<br/> Client Email: [[++client-email]]<br/> Client Phone: [[++client-phone]] <br/> Find us on Facebook: <a href="http://www.facebook.com/[[++client-facebook]]" target="_blank"><img src="assets/facebook-logo.png" alt="[[++client-facebook]]" /></a><br/> Follow Us on Twitter: <a href="http://www.twitter.com/[[++client-twitter]]" target="_blank"><img src="assets/twitter-logo.png" alt="[[++client-twitter]]" /></a><br/>
As you can see we are accessing our Context Settings by their Key wrapped with a MODX Sytem Settings tag, which in turn displays the Context Settings value in our chunk.
Keep in mind we can use MODX Output modifiers on these Context Settings as well so if one of the settings is empty it will not render a piece of code if you wish. You can also create a FormIt form where you can use a Context Setting for the emailTo and redirectTo fields allowing you to reuse the form and export it as part of a transport package which you could then install on all of your new sites then all you would have to do is create the Context Settings and have a working form.
Context Settings are a hidden secret of MODX that a lot of people don't know how to use or didn't know they existed, hopefully this article has changed that.
As always remember to practice and try to get a little creative with these settings, you can do some pretty awesome and powerful stuff with this so don't be afraid to think outside the box when using them.
Good luck and happy MODXing