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

    How can i display only a certain variable from a snippet??

    Say I have the following:

    <?php
    
    $result1 = 343;
    $result2 = 222;
    
    ?>
    


    And I just want to display $result1 in one location in my template, and $result2 i want displayed elsewhere in my template. How would I? Would I need to use Chunks perhaps?

    Thanks.
      • 33337
      • 3,975 Posts
      Are you familiar with [+placeholders+] ? I guess you need to use them.

      http://modxcms.com/setplaceholder.html

      update: Check the Ditto code (for example), and for more extensive [+ph+] management/output, check out PHx http://wiki.modxcms.com/index.php/PHx

      regards.
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 10395
        • 96 Posts
        Hmmm, there is no other way to do it without incorporating something external from Modx? What exactly are Template Variables used for then?
          • 33337
          • 3,975 Posts
          [+placeholders+] are not external to MODx smiley ... but PHx is.

          a little example of setting a placeholder for your snippet variables:
          $my_var = "56";
          modx->setPlaceholder('age', $my_var);
          return;
          


          Use [+age+] in template/document anywhere.
            Zaigham R - MODX Professional | Skype | Email | Twitter

            Digging the interwebs for #MODX gems and bringing it to you. modx.link
            • 10395
            • 96 Posts
            Oh, gotcha Zi, thanks ^_^.
              • 10395
              • 96 Posts
              Hey, I am still having a problem trying to get this working correctly.

              I created a snippet.

              
              <?php
              
              //Get the global.php file from the vB install
              chdir('xxx/xxx/xxx/xx/xx');
              require_once('./global.php'); 
              
              //Fetch the total number of members.
              $tmp1 = $vbulletin->db->query_read("SELECT * FROM users");
              $members = $vbulletin->db->num_rows($tmp1);
              modx->setPlaceholder('members', $members);
              
              //Fetch the total number of topics
              $tmp2 = $vbulletin->db->query_read("SELECT * FROM thread");
              $topics = $vbulletin->db->num_rows($tmp2);
              modx->setPlaceholder('topics', $topics);
              
              ?>
              
              


              Now what? I tried just putting [+members+] inside my template but it just shows up nothing. Don’t I have to call the Snippet somewhere? Or is it just returning nothing because my snippet isn’t correct?

              Thanks.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                You need to call the snippet somewhere, otherwise it never gets run.
                  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
                  • 10395
                  • 96 Posts
                  hmmmmm. Well, if I want to display it on all pages, where would I call the snippet? Would I need to remove the chunk of HTML and move it into a Chunk and call the snippet in the chunk? Like so:

                  ->ChunkName
                  
                  [!vbStats!]
                  
                  <a href="#">[+members+]</a>
                  
                  ...etc...
                  


                  And my template would be:

                  
                  <div id="foo">
                      {{ChunkName}}
                  </div>
                  
                  ..etc..
                  
                  
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    That would work. You could also put the snippet call in your template, and the placeholder wherever you want that displayed. The point is to make sure the snippet call occurs before the placeholder tags.
                      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
                      • 10395
                      • 96 Posts
                      Ok, I gave it a try and it returned something, an error, but still something to go off of.

                      Anyone help me out with this error? I’m not really sure what it’s trying to say..



                      Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/gaia/public_html/new_er/manager/includes/document.parser.class.inc.php(748) : eval()’d code on line 10

                      Thanks.