We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I’m trying to come up with some uses for this. I can see that it would be useful for picking an image to display from a directory of images. I also see that it can provide a link to a file. If it’s an HTML file, it will display (with no template). A .PDF file will launch Adobe Reader. Is there any way to use it to provide a download option for the selected file?

    Any other uses for this?
      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
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Bob--

      when I first wrote up the pages on the Wiki for the bindings, e.g. http://wiki.modxcms.com/index.php/%40DIRECTORY_Binding I concluded that the @DIRECTORY binding wasn’t terribly useful because it lacked a way to filter the files, e.g. *.jpg.

      Now that I’m reading that page again, I realize that it’s pretty poorly written... my "example" doesn’t really show you how to do anything with that. Sigh. The only thing I’ve used them for is to populate a drop-down menu:
      http://wiki.modxcms.com/index.php/Template_Variable_Drop_Down_Menu

      If you can think of other uses for it, please share them. I’m not a big fan of the bindings... too poorly documented, and everything that can be done with them can be done using Snippets.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Does this help? http://sottwell.com/tv-bindings.html

        I much prefer to use a snippet, and any page-specific variables are handled by using a parameter in the snippet call, and using a TV for the parameter value for that document. The only @binding I ever use is the @INHERIT, which I use a lot.

        Understanding the @bindings are actually pretty easy; they’re all in one switch statement in the manager/includes/tmplvars.commands.inc.php file.
                    case 'DIRECTORY' :
                        $files = array ();
                        $path = $modx->config['base_path'] . $param;
                        if (substr($path, -1, 1) != '/') {
                            $path .= '/';
                        }
                        if (!is_dir($path)) {
                            die($path);
                            break;
                        }
                        $dir = dir($path);
                        while (($file = $dir->read()) !== false) {
                            if (substr($file, 0, 1) != '.') {
                                $files[] = "{$file}=={$param}{$file}";
                            }
                        }
                        asort($files);
                        $output = implode('||', $files);
                        break;
        
          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
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Thanks Susan, that is a helpful page.

          Looking at the code, it’s fairly easy to see what the bindings do... but casual users aren’t going to be looking at that. "Casual users"... why did that phrase pop into my head?

            • 3749
            • 24,544 Posts
            Thanks Susan, I had found that code (and your @ bindings page). I just couldn’t think of much to do with this @ binding (or @SELECT, for that matter). I would use a snippet, but I’m writing a book and one of the headings is @ bindings so I can’t just say "use a snippet." wink

              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
              Why not? The TV is there, but it’s like the legacy global meta tags system; if it’s not a whole lot of use, why use it and clutter up the document editing form if it has no advantage?
                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
                • 17851
                • 213 Posts
                I am using @DIRECTORY to populate a dropdown list TV as well. Works great, except I needed to be able to have a blank selection as well. So I changed the following line in manager/includes/tmplvars.commands.inc.php (line 101):

                $output = implode('||', $files);


                to

                $output = '||'.implode('||', $files);


                  Mark Macatee
                  President
                  Power 10 Solutions
                  http://www.power10solutions.com