We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46886
    • 1,154 Posts
    I have an interesting question. I have a template which outputs a value from the db, and I want to use that value to do additional things.

    In Discuss, I am trying to modify the post view, in order to put some icons in the user panel of a post, under the avatar and user name.

    In the template, I get a value which is the number of the user's posts, which appears below the user avatar:

    [[+author.posts]]

    What this is is the number of posts that the current user (author here) has made. I think I can say that its a db call happening in the backend, which basically says for each post go get user data, find current user, (output avatar and user name), then output number of posts.

    What we want to do is take that value and use it. If the posts <50, then output basic_user_icon, and if >50, output advanced_user_icon, and so on. Basically a graphic, but could be anything.

    How to grab this value? It seems like its right there, we could just grab it and use it...but maybe I am wrong.

    Can we do it in this template, which seems the most direct and simple way to do it, or should we think in terms of a stand-alone tool? Any advice? It seems we can't put code freely in this place...


    Here is the relevant section of the template.
        <div class="dis-post-left">
            <ul>
                <li class="dis-usr-icon">
                    <a href="[[DiscussUrlMaker? &action=`user`
    &params=`{"type":"username", "user":"[[+author.username]]"}`]]"
    class="auth-avatar" title="[[%discuss.view_author_profile]]">
                        [[+author.avatar]]
                        [[+author.title:notempty=`<span
    class="dis-usr-title">[[+author.title]]</span>`]]
                    </a>
                </li>
                <li class="dis-usr-post-count">[[+author.posts]]
    [[%discuss.posts]]</li>
                <a href="[[DiscussUrlMaker? &action=`messages/new`
    &params=`{"user":"[[+author.username]]"}`]]" class="dis-pm-btn">Send
    PM</a>
            </ul>
        </div>

    This question has been answered by BobRay. See the first response.

      • 46886
      • 1,154 Posts
      Here's the problem in the words of my tech guy:

      to try to figured out what need to be used to make it work with modx lang like [[+post_count:greater:10:then=`Master`]] or something like that ... i shoot in dark to find the exact command if he even exist ...

      Can someone point to these modx values?
      • discuss.answer
        • 3749
        • 24,544 Posts
        What you want is here: http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29

        Specifically (I think):

        [[+author.posts:greaterthan=`50`:then=`Master`:else=`NonMaster`]]



        You could also do it with the "If" snippet, but I would do it with a custom snippet, which would be significantly faster:

        [[!GetLevel? &count=`[[+author.posts]]`]]


        <?php
        /* GetLevel snippet */
        
        $defaultValue = 0;
        $count = $modx->getOption('count', $scriptProperties, $defaultValue); 
        
        if ($count > 50) {
           return 'Master';
        } else {
           return 'NonMaster';
        }
        
        
        [ed. note: BobRay last edited this post 12 years, 2 months ago.]
          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
          • 46886
          • 1,154 Posts
          Ah right, of course the snippet lets us put php in places where we can't put php, brilliant!

          But one question, why does this change to +post_count rather than +author.posts? Is this just an example?

          That seems strange to me, and relies on an understanding that post_count is user post count and not, for example, number of posts in that thread...

          Anyway, thanks so much, this will guide him quite a bit and save his time.
            • 3749
            • 24,544 Posts
            Yes, it's just an example. I took it from the post of your tech guy. You should use +author.posts if that's what the placeholder is.
              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
              • 46886
              • 1,154 Posts
              Right, of course, it only makes sense.

              Wow, 3 ways to do it, but absolutely the snippet is the way to go. I need to see that more clearly in the future.

              So just as I thought, the data is there in the placeholder and can be grabbed and manipulated pretty freely.
                • 3749
                • 24,544 Posts
                Right, technically, it's being sent to the snippet as a property. In a snippet, all properties (the things preceded by & in the tag) are in the $scriptProperties array.

                You could get it with $scriptProperties['count'], but using $modx->getOption() is more convenient because it lets you set a default value and won't throw a PHP error if the property is not set.

                  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
                  • 46886
                  • 1,154 Posts
                  right, and then any output I can style either by making a chunk or using a div and applying css to that div, with the chunk being the scalable option.
                    • 3749
                    • 24,544 Posts
                    That's right. For simple cases, you can choose to put the HTML in the snippet.
                    For anything more complex, you can just create a Tpl chunk with placeholders and then do this in the snippet:

                    $fields = array(
                       'placeholder1' => 'someValue',
                       'placeholder2' => 'someOtherValue',
                    );
                    
                    return $modx->getChunk('myChunk', $fields);


                    All the placeholders will be replaced during the getChunk() call.
                      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
                      • 46886
                      • 1,154 Posts
                      Yeah I had some confusion about this before, will be looking into it.

                      Thanks again!