We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51292
    • 18 Posts
    I just started using modx, and what I am effectively trying to do is load a different page based off of a cookie value. I have the two following template variables in one template, and on three pages:

    • persona
    • page
    Basically what I am going to do is the "persona" is a string, which will be stored in a cookie (still not sure how this will get into a cookie, but let's assume it is there). Say I am trying to hit /home, and there are three pages that could possibly show. Each one of these pages has the page template variable set to "home", and each has a different persona value. I want to load the page that matches the persona in this user's cookie. Needing to write PHP is no problem for me, I just have no idea how to hook into the page load process, query pages based on template variables, and deal with dynamic page aliases.

    If there is a better way to solve this problem, I'm all ears.

    This question has been answered by multiple community members. See the first response.

      • 3749
      • 24,544 Posts
      I'm not completely clear on what you're trying to do, but it might be easier to use a single page and adjust its content in a plugin connected to OnWebPagePrerender that checks the TV, cookie, or $_SESSION variable and replaces a placeholder in the content with the desired content from a chunk.

      In fact, if you can use a TV to determine the content, you might be able to do it without a plugin by naming the chunks after the possible TV values and putting this tag where you want the content plugged in:

      [[$[[*TvName]]]]
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Are you talking about a logged-in user getting one content while anonymous users get something else? If you are, that's what the Login and Personalize snippets are for.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 42562
          • 1,145 Posts
          By "same alias", do you mean "common suffix"
            TinymceWrapper: Complete back/frontend content solution.
            Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
            5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
            • 51292
            • 18 Posts
            There are no logged in users in this system. It's a pretty basic idea: Here is a likely scenario:

            Say we have two users.

            • User A comes to the site via link in Facebook, and requests the homepage url (/home). This link includes a campaign code in the URL: ?campaign=facebook for example.
            • User B comes to the site naturally (no campaign code), and request the homepage url (/home).

            For User A, I want to show the page resource "homepage_facebook", but for User B, I want to show the page resource "homepage". What I've done is given the homepage template TVs of page and persona (campaign), and assigned values for these two page resources. The page I want to display to User A would have values of page = "home" and persona = "facebook", where the page User A requests would have values of page ="home" and persona = null (which is the default page that should be loaded, because there is no persona (campaign code set).

            Essentially I need to dynamically load a different page resource at the same URL given a cookie setting/query parameter. It would be okay to set a default page "homepage" to be loaded at the /homepage URL, but I need to be able to hook into the page load event (or maybe OnWebPagePrerender that BobRay mentioned), and query a resource based on those TVs to load up a different page if this user should see a different version. [ed. note: dallen last edited this post 10 years, 11 months ago.]
              • 51292
              • 18 Posts
              So looking at other forum posts https://forums.modx.com/thread/95261/how-to-filter-and-get-resource-based-on-its-template-variable-value It looks like the better option is to use ClassExtender plugin, which I've installed, configured, and created my two fields on the resource itself, rather than as template variables. Now I just need to get querying by these fields, and displaying that content working.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Ok, first you need to think in MODX terms. A "page" is not necessarily content. You would use the same resource to generate the "page" that the user has requested. What you want to do is to show different content to users from different places.

                To do this, use chunks containing your different content, and use a snippet where you want the content to show on the same "home page" structure to select between chunks, something like this:

                <?php
                $from = $_SERVER['HTTP_REFERER'];
                $chunk = 'home_chunk';
                
                If (strstr($from, 'facebook') !== false) {
                    $chunk = 'facebook_chunk';
                }
                
                $output = $modx->getChunk($chunk);
                return $output;
                
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 51292
                  • 18 Posts
                  I appreciate your response, but that is not what I am trying to do. There may be rare occasions where I only need to replace a specific piece of content on a page (in which case your suggestion would work fine), but for the most part, the page could be entirely different.

                  I want to load a different web resource entirely (or a page), not just a content "chunk". The reason for this is that the page a user might see could be entirely different insofar that it might be a totally different template, have a different menu, have a different header and footer, but it might not have any of these things. Rather than replacing literally every piece of content that could be displayed on the page with chunks, it's much easier to just create an entirely different page. It's also easy for my non-technical content managers to handle, rather than have to deal with creating 1,000 different chunks without understanding how they get displayed on the pages.

                  I think what I'm going to do after trying to use ClassExtender and not finding it of much use, is just querying modResources by querying their modTemplateVarResource that match what I am trying to match.

                  Question now is, can I easily render a modResource object with a method like
                  $modResource->render()
                  ? I see that templateVariables have a method
                  $templateVariable->renderOutput()
                  , but I don't see that on the resource itself.
                    • 3749
                    • 24,544 Posts
                    For this one, you have to get the resource object first:

                    $resource->GetTVValue($tvId);


                    or, for the current resource:

                    $modx->resource->GetTVValue($tvId);
                      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
                    • discuss.answer
                      • 42562
                      • 1,145 Posts
                      Place this snippet in your default homepage
                      <?php
                      $from = $_SERVER['HTTP_REFERER']; // note, $_SERVER anything can be forged
                      
                      if (strpos($from, 'facebook.com') !== false) {
                        $id = 12; // id of campaign page to be displayed
                        $url = $modx->makeUrl($id); 
                        $modx->sendRedirect($url);
                      }
                      
                      if (strpos($from, 'google.com') !== false) {
                        $id = 14; // id of campaign page to be displayed
                        $url = $modx->makeUrl($id); 
                        $modx->sendRedirect($url);
                      }


                      updated $id
                      see suggestions below [ed. note: donshakespeare last edited this post 10 years, 11 months ago.]
                        TinymceWrapper: Complete back/frontend content solution.
                        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
                        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.