We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25663 MODX Staff
    • 12,272 Posts
    Contexts can be used to accomplish a variety of end results in Revolution, including multiple language sites, Manager access, etc. Let’s talk about "contexts" here.
      Ryan Thrash, MODX Co-Founder
      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
      • 16239
      • 49 Posts
      Quote from: rthrash at Oct 06, 2008, 08:20 AM

      Contexts can be used to accomplish a variety of end results in Revolution, including multiple language sites, Manager access, etc. Let’s talk about "contexts" here.
      Great!
      I would be very appriciated if someone told, HOW EXACTLY one can do multiple language sites using contexts.
      Kinda step-by-step guide laugh
        • 22303 MODX Staff
        • 10,725 Posts
        There is no "HOW EXACTLY" to describe with regard to L10n in MODx Revolution. That’s up to the requirements and details of the site being developed, and the developer writing the snippets and plugins to control it. Contexts simply provide a way to organize a set of pages, isolate cache files and plugins, and override system settings. That’s it.

        So you could create a context per language or culture your site is to support, and then you can switch contexts using a plugin that detects users language preferences (how ever you want to determine that).
          • 7231
          • 4,205 Posts
          I am a little fuzzy on contexts as well. I understand the principal, and have seen code examples working with them. But not sure when to use them and why.

          For example in 09x we can use containers to group language files and some snippets to identity the language and go to the correct container. Would using contexts for this be any more efficient?

          If there is more than one context is there a system setting to select the default context? Can documents be shared between contexts? Why is there a context for ajax and would ajax work without a context? I can create a context but I cant create a document in a context, how do I assign a resource to a context? Should there be a Select Context option when creating a document like there is for templates? Do contexts have independent caches? Should average users be concerned about contexts?

          As you can see I don’t quite get it rolleyes
            [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
            Yeah, I am aware that folks don’t understand all about it yet. There are a number of things to cover when working with contexts, but the most important is that you limit the cache files to a single section of the site. For beta, it will be imperative that we have some sample packages that will install a multi-lingual site demonstration, including all the snippets, plugins, and content separated into contexts.

            A couple of notes though to answer your specific questions:
            Quote from: dev_cw at Oct 09, 2008, 11:25 AM

            For example in 09x we can use containers to group language files and some snippets to identity the language and go to the correct container. Would using contexts for this be any more efficient?
            Yes, because you would only load information about the specific context the document is being loaded in. It also allows cultural specific sections of the site to be organized into separate subdomains, either by serving them all from a single virtual host and single index.php with a plugin to decide which context to load, or by having multiple virtual hosts and index.php’s in each that load a specific context. Examples of both approaches should be prepared and discussed.

            Quote from: dev_cw at Oct 09, 2008, 11:25 AM

            If there is more than one context is there a system setting to select the default context?
            The index.php initializes the ’web’ context by default, so it is the default context. You can have other index.php’s (also known as gateways or front-controllers) that specifically initialize a context by name.

            Quote from: dev_cw at Oct 09, 2008, 11:25 AM

            Can documents be shared between contexts?
            In Revolution 2.0.0, no; each Resource will be confined to a single context. 2.1 will likely include features to allow Resources (formerly Documents) to exist in a primary context and any number of additional contexts.

            However, you can use the new SymLink Resource type to create Resources in other contexts which forward requests to pages in another context, displaying that content as if it existed at the URL location defined by the SymLink.

            Quote from: dev_cw at Oct 09, 2008, 11:25 AM

            Why is there a context for ajax and would ajax work without a context?
            Contexts, since they allow overriding system settings, allow you to redefine almost all aspects of MODx request and response handling. The mgr and connector contexts each define their own custom Request handler class for routing and processing requests. Since the mgr uses Smarty, the mgr context defines a Request class (modManagerRequest) that takes of everything needed to render the Smarty templates. Similarly, the connectors (or Ajax context) has modConnectorRequest to handle Ajax requests and responses most efficiently. Users can define their own custom Request handlers as well, so you can create a context to render a set of Smarty templates, or integrate with another software application that would not generally work with the regular front-end (web context) Request handler.

            Quote from: dev_cw at Oct 09, 2008, 11:25 AM

            I can create a context but I cant create a document in a context, how do I assign a resource to a context?
            You should be able to use the right-click context menu in the tree to create a document in a specific context. If that doesn’t work, it’s a bug.

            Quote from: dev_cw at Oct 09, 2008, 11:25 AM

            Should there be a Select Context option when creating a document like there is for templates?
            Yes, that is a good idea, though you should be able to drag and drop them between contexts as well.

            Quote from: dev_cw at Oct 09, 2008, 11:25 AM

            Do contexts have independent caches?
            Yes, all the cache files, except the system settings cache, are divided by Context. This means all the Resources (and Plugins) defined for a specific context are loaded only when needed in a specific Context. MODx loads Contexts on demand, for instance, if you create a Wayfinder instance and wanted to list Resources from multiple contexts, those would be loaded dynamically as needed.

            Quote from: dev_cw at Oct 09, 2008, 11:25 AM

            Should average users be concerned about contexts?
            Average users is hard to define, but in general, probably not. It is an additional organizational feature, but generally would be involved in some kind of specific site requirement, or be used by a specific add-on or core extension package automatically. Users interested in multi-domain, sub-domain, multi-site, and/or multi-lingual deployments will likely need to understand what it means however, as well as users with large numbers of Resources that would benefit from isolation into contexts (for caching purposes).
              • 7231
              • 4,205 Posts
              Thanks for clearing all those points up, you must be getting tired of hamering these points over and over. I am getting it slowly and will benefit from the examples for sure. grin

              I was unable to save a document (resource) when trying to create one in a new context. The create screen opens but it will not save, the button is active but it does not save (could be a local bug, i am using alpha-4).

              Also when selecting the rte editor during the creation of a document in the new context (going from no editor to tiny) all the data that was filed in previously (page title etc.) gets erased and it jumps to the web context.
                [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
                I’m feeling dense here because I can’t figure out any easy way to create a new context in the Resource tree. Shouldn’t "Create new context" be an option on the right-click menu for the current context (and maybe on the top Site menu)?

                  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 09, 2008, 01:48 PM

                  I’m feeling dense here because I can’t figure out any easy way to create a new context in the Resource tree. Shouldn’t "Create new context" be an option on the right-click menu for the current context (and maybe on the top Site menu)?
                  It’s under Tools --> Contexts.
                    • 3749
                    • 24,544 Posts
                    Quote from: OpenGeek at Oct 09, 2008, 02:31 PM

                    Quote from: BobRay at Oct 09, 2008, 01:48 PM

                    I’m feeling dense here because I can’t figure out any easy way to create a new context in the Resource tree. Shouldn’t "Create new context" be an option on the right-click menu for the current context (and maybe on the top Site menu)?
                    It’s under Tools --> Contexts.

                    Duh! embarrassed

                    Now that I know where it is, let me ask this question. You mentioned overriding system settings above. Suppose I want to override a system setting for snippets that run on a particular web page or two, without changing the setting for the rest of the site. Can you get me started on how to do that with contexts?



                      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 09, 2008, 03:48 PM

                      Now that I know where it is, let me ask this question. You mentioned overriding system settings above. Suppose I want to override a system setting for snippets that run on a particular web page or two, without changing the setting for the rest of the site. Can you get me started on how to do that with contexts?
                      Sure.

                      Create your context, we’ll call it test.
                      Now create context settings with the same key as the system setting you want to override in the context. We’ll use an example for setting up some pages on a separate hostname that is pointing to the site (i.e. virtual host). Simplest example is localhost and 127.0.0.1. Here are my context settings for the test context configured to serve pages when the client requests something from 127.0.0.1:
                      [table]
                      [tr][td]key[/td][td]value[/td][/tr]
                      [tr][td]http_host[/td][td]127.0.0.1[/td][/tr]
                      [tr][td]site_start[/td][td]3[/td][/tr]
                      [tr][td]site_url[/td][td]http://127.0.0.1{base_url}[/td][/tr]
                      [/table]
                      The site_start is different when requests come from 127.0.0.1, as is the http_host and site_url. Notice you can refer to settings that are already available from the system settings using the setting key surrounded by braces, e.g. {base_url}.

                      The by simply installing this plugin and registering it for the OnHandleRequest event, your site will respond appropriately based on the HTTP_HOST sent by the client, switching contexts if necessary with the help of the $modx->switchContext() function:
                      <?php
                      switch ($modx->config['http_host']) {
                          case '127.0.0.1:80':
                          case '127.0.0.1':
                              // if the http_host is of a specific domain, switch the context
                              $modx->switchContext('test');
                              break;
                          default:
                              // by default, don't do anything
                              break;
                      }
                      ?>