We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10525
    • 247 Posts
    Hi folks.

    there is probably a simple answer to this but I can’t find anything in the wiki or forum. Please advise if this should be posted elsewhere.

    I am building a multilingual site. Each doc is populated with language-specific content from a table in the MODx db, using a snippet which is called from the template: [!LanguageContent?!].
    The snippet displays the language links, using [!LanguageContent? &type=`choice`!], and clicking one of these re-calls the current page but using the selected language to fetch the content from the db, using [!LanguageContent? &type=`content`!]. (ie, using the same snippet twice on the page).

    The links are created as:
    echo'<a href="[~[*id*]~]&do=change&lang='.$language_id.'">'.$language'."</a>\n";

    (I’ve simplified this by taking out some html attributes).

    and end up as:
    <div id='flags'>
        <a href="index.php?id=1&do=change&lang=1">English</a>
        <a href="index.php?id=1&do=change&lang=2">Spanish</a>
        <a href="index.php?id=1&do=change&lang=4">German</a>
    </div>


    This works fine when changing language on the same page, just getting the language from $_REQUEST[’lang’] and fetching the appropriate content from the db, but it is when I want to go to another page in the menu that this system comes unstuck, as I have not added the "&do=change&lang=2" etc to the menu links (created using Wayfinder).

    I tried using the value of $language_id to set a placeholder, which I could then pick up in the snippet on the next page, but the placeholder seems to expire by the time I get to the next page. Does this mean that placeholders only last as long as the current page exists and are then disposed of?

    (If not, then should I be able to pick up the placeholder in Wayfinder and add the language ID to the menu links, as above?)

    In summary: Is there any way of persisting a variable on the server, for the current session, which can then be used when the next browser request is made?

    There are may be snippets available to do this sort of thing, which might be useful, but I would love to know exactly what happens to placeholders; how long do they last and can they be used by other snippets and between page requests?

    Very much obliged if anyone can answer this for me, thanks smiley


      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Yes, placeholders are stored in an array that is only good for the document being generated.

      You can add a query string to links in your Wayfinder row tpl chunk. Just keep in mind that you have to use a leading ? if you’re using Friendly URLs, and a & if you’re not (the index.php?id=xx uses the leading ? in that case).

      You can also use the SESSION, which is what I do. $_SESSION[’lang’] can be set and retrieved with snippets. I also like to use cookies, since the session can expire at varying intervals depending on your server configuration.
        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
        • 10525
        • 247 Posts
        Thanks for that Susan, I like your $_SESSION suggestion, I’ll try that.

        I presume that this array is available on the server to any snippet and any page for as long as the session lasts?

        Is there a way to print out all of the placeholders and $_SESSION variables, so I can observe what is available at any point in time? Would print_r be the way to go?

        Edit: found the answer to the above Q for $_SESSION variables - from Breezer at http://modxcms.com/forums/index.php?topic=27196.0

        Cheers,

        Gav
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          It’s a regular PHP session. http://sottwell.com/article-sessions.html

          Indeed, I use this code a lot:
          echo "<pre>";
          print_r($whatever);
          echo "</pre>";

          sometimes with a following exit(); (especially in plugins). In fact, when I’m developing, I make a function out of it that takes the variable I want to view as a parameter.

            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
            • 10525
            • 247 Posts
            Thanks Susan,

            $_SESSION works a treat! I hadn’t explored sessions in MODx before, so looking forward to seeing what I can do with this.

            I do use the same 3-liner a lot myself for arrays. But what about placeholders - are they stored in an array that I can view (ie all placeholders for all snippets etc)?

            Cheers,

            Gav
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              $modx->placeholders. Keep in mind that this can vary as snippets, TVs and plugins are processed. The parser will make up to 10 passes over the document, so every pass could add to the array.

              In a function, you would need to declare $modx global. To set a placeholder, it’s $modx->setPlaceholder(’name’, ’content’) and to get a placeholder it’s $modx->getPlaceholder(’name’). You can also use $modx->mergePlaceholderContent() to do the placeholder/content replacement (it actually ends up using the PHP str_replace() function). There are also a couple of API functions to set multiple placeholders using an array or an object.
                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
                • 10525
                • 247 Posts
                Brilliant!

                Thanks a lot for all that info Susan, and for your fast responses too.

                Gav