We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32963
    • 1,732 Posts
    Hi Everyone,

    Here goes my first tip of the day. By using MODx2 or higher you can create powerful template variables (TVs) to do all kinds of things.

    Today we will look at how we can create a TV and use the @EVAL binding to display the name of the user who?s currently logged into you web site.

    1. Create a new TV and called Username
    2. Select the Textarea Mini or Textarea input type
    3. Inside the Default Value textbox, enter the following code:
    @EVAL
    global $modx;
    return $modx->getLoginUserName();
    

    4. Save your TV

    That?s it for the Username TV. You can now add the TV to your documents or template by using [*Username*]. For example:


    Welcome back [*Username*]


    Best regards,
      xWisdom
      www.xwisdomhtml.com
      The fear of the Lord is the beginning of wisdom:
      MODx Co-Founder - Create and do more with less.
      • 33337
      • 3,975 Posts
      Great work xWisdom,

      Thats really great for newbies...... wink

      Thanks for sharing.

      Warm regards,

      zi
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        I used this to come up with this:
        @EVAL
        if(isset($_GET['lang'])) {$lang=$_GET['lang'];}
        else if(isset($_COOKIE['Language'])) {$lang=$_COOKIE['Language'];}
        else {$lang=$modx->config['site_start'];}
        return $lang;
        

        Now I can put a [*lang*] wherever I need it! It’s working great...except for one oddity. When logging out of the weblogin, I get this:

        ? `4 ` is not numeric and may not be passed to makeUrl() ?

        Any idea what’s going on here? I tried typecasting the return value to (int), but that didn’t make any difference.

        (two minutes later) Great self-feedback going on here! Figured out that I need to use `[*lang*]` for the logoutid when calling the web login snippet! Now everything is working great! I only need one snippet to call from each language’s home page to set the cookie, and one simple list menu snippet to make a languages menu to set the language variable to begin with! No hacks, no fuss multilanguage support!
          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
          • 21191
          • 236 Posts
          For the life of me, this is not working on 9.6.3

          I have a TV with the name of [*username*] and your code in the default value.

          I change the default value to just text it works.. fine.. even if I change it to a plain print or echo statement it works, well sort of.. it spits out the code at the top of the page in those cases.

          I’ve done this before too, any help anyone?
            ~Shawn Himmelberger
            Check out my MODx Development and MODx Design Company - Himmelberger Design