We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20014
    • 200 Posts
    alright I have a div where I want a snippet to place an image from a particular folder into a div, there are three pages, all exactly the same and I want to avoid having to create three seperate templates.

    I have created three separate snippets and at first I created a TV for each, and assigned all three TVs to a single template, the checkboxes showed in the editor but if I click one tv for one page, another tv for another page, and finally the last tv for the third page, I get all three tvs on each page. I know I am missing something basic here, but I don’t know what.

    essentially I want to be able to add images to a div of a single template but from different sources on different pages without having to create a huge amount of templates, of essentially the same code.

    I have checked the wiki, and did a search here, but I didn’t find the thread with the key I needed.
      • 20014
      • 200 Posts
      no ideas? I have scoured and found this article which holds that one grain I need to get this done,

      http://www.gregorysmart.com/2009/07/22/utilizing-modx-templates/

      I have called chunks which contained calls to the php snippet I use for rotating my images from a folder depending on which page is being viewed, I have called TVs
      and called the snippets themselves in the if and elseif areas, but I can only get a page with an empty div where the images are supposed to be,
      the only thing which works is using two templates with two different snippet calls in the div which is supposed to hold the images.

      I still can’t get the output I want, have been only working with modx for a couple months now and am trying to figure out all the ins and outs of implementing its concepts...so any help from coders/developers for this lowly designer would be much appreciated.
        • 20014
        • 200 Posts
        got it, created two snippets each with php code to randomize the output of an image from a set of images in folder like this:

        <?php
        // images named 1.jpg, 2.jpg etc
        // total number of images in the folder
        $total = "11";
        // type of image file eg. .jpg or .gif
        $file_type = ".jpg";
        // location of folder containing images
        $image_folder = "assets/templates/your_template/images/name_of_folder_containing_your_images";
        $start = "1";
        $random = mt_rand($start, $total);
        $image_name = $random . $file_type;
        echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";
        ?>


        of course for each snippet changed the name of the folder with the images i.e. folder one for one page and folder two for page two for example [[randomimage1]] and [[randomimage2]]

        next I created a new snippet [[ifsnippet]]using this:

        <?php
        $id = $modx->documentObject['id'];
        if ($id=="33")
          $output="{{firstchunk}}";
        elseif ($id=="32")
           $output="{{secondchunk}}";
        else
           $output="{{firstchunk}}";
        return "$output";
        ?>


        noticed I put the pages id....I then created two chunks the first and second chunk referenced above like this:

        <div class="photo" id="photo">[[randomimage1]]</div>

        notice that it calls the actual snippet that changes images of one folder.

        then FINALLY I CREATED A TV [*subheaderimages*]
        and put the [[!ifsnippet!]]
        as its default value, assigned it to one template and then assigned that template to two separate pages which were exactly the same except for the images there were to display randomly.

        pheeeeew! seems like a lot of work, there has to be a better way, but it works now I can rid myself of different templates just for the need of one single change in the element....and I know for a fact that if I was a php guru I would be able to combine both the if snippet and random snippet and half my headaches would be over, but I just don’t have the mind to be that level of a coder, I just paint pretty pictures laugh laugh

        modx forums definitely needs not just a newbie area but an area that holds designer’s hands and puts things in layman’s terms laugh laugh

          • 3749
          • 24,544 Posts
          It does seem kind of convoluted. Maybe I’m misunderstanding what you want, but I wonder if a single template containing a single TV with a drop-down choice of images would do it (or a drop-down list of @CHUNK items with the chunks containing the div and the image).
            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
            • 20014
            • 200 Posts
            this is what I was going for, I actually have several pages all using the same template but a couple of them have divs that I want populated by images from separate folders on the server, these aren’t galleries essentially divs containing graphics that would change as a visitor surfed around the site, so if they visit one page and then go back it would have a different image, or if they hadn’t cleared their cache and returned to the site new images would be present.

            I can achieve this by creating two templates and in one area placing a randomize snippet created for each folder designated for a particular page, I’d rather just be able to place one line of code in one template, yet still achieve the same effect: as a visitor goes to page A he see images from the folder for page A and as he/she goes to page B sees images from the folder for that page and so on and so on.

            I know there is a much simpler solution...

            i tried putting snippets directly into the template, chunks, there were several variations, and this is the only one which returned the content.

            if there are other designers who can’t code php worth a damn
            this link gives you the document object list....feel this thread could really help people place content

            http://svn.modxcms.com/docs/display/MODx096/Document+Object

            so each could tweak the if elseif and else code above to their needs.

            it may not be pretty but the problem is solved.
              • 3749
              • 24,544 Posts
              I would pass the page ID (or the value of a template variable indicating or set to the image directory) to the snippet as a parameter and have the snippet set the correct directory based on that parameter.

              &pid=`[*id*]`


              Then in the snippet:

              switch($pid) {
              
                 // create image path here
              
              }


              OTOH, if it ain’t broke . . . 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
                • 20014
                • 200 Posts
                so, if I didn’t have the situation where I wanted a script to randomize, just an image or perhaps other content I would put a snippet in the body [!contentpath? &pid=`[*id*!]

                and then create a snippet called contentpath with code like this:

                <?php
                switch($pid) {
                $image_folder = "assets/templates/your_template/images/name_of_folder_containing_your_images";
                }
                ?>
                

                I am already envisioning uses, perhaps with ditto, if I have let’s say six categories, I could assign an image to each category so that on the ditto main summary page each new post depending on it’s category would have its own unique image....or assign an image to a user, etc? or am I completely off base with what you are saying?
                BobRay you’ve got me intrigued, I just need a bit more clarification...I am not sure if I am following you correctly.



                  • 3749
                  • 24,544 Posts
                  Quote from: modxlearner123 at Mar 25, 2010, 11:29 AM

                  so, if I didn’t have the situation where I wanted a script to randomize, just an image or perhaps other content I would put a snippet in the body [!contentpath? &pid=`[*id*!]

                  and then create a snippet called contentpath with code like this:

                  <?php
                  switch($pid) {
                  $image_folder = "assets/templates/your_template/images/name_of_folder_containing_your_images";
                  }
                  ?>
                  

                  I am already envisioning uses, perhaps with ditto, if I have let’s say six categories, I could assign an image to each category so that on the ditto main summary page each new post depending on it’s category would have its own unique image....or assign an image to a user, etc? or am I completely off base with what you are saying?
                  BobRay you’ve got me intrigued, I just need a bit more clarification...I am not sure if I am following you correctly.

                  I think you’ve lost me there (and vice versa). I was trying to get to bed and my code was overly abbreviated.

                  If you wanted a particular image for each category, you’d send the category in the snippet tag:
                  [!GetImage? &category=`##`!]

                  Where ## is the category number.

                  Then in the snippet:
                  <?php
                  switch($category) {
                     
                      case '1':
                          $img = 'path_to_single_image';
                          break;
                      case '2':
                          $img = 'path_to_single_image';
                          break;
                      case '3':
                          $img = 'path_to_single_image';
                          break;
                      default:
                          $img = 'path_to_single_image';
                  }
                  return $img;
                  ?>


                  If you wanted a random image from a particular directory for each category, the snippet would look something like this:

                  <?php
                  switch($category) {
                     
                      case '1':
                          $dir = 'path_to_dir1/';
                          break;
                      case '2':
                          $dir = 'path_to_dir2/';
                          break;
                      case '3':
                          $dir = 'path_to_dir3/';
                          break;
                      default:
                          $dir = 'path_to_default_dir/';
                  }
                  
                  /* get the files in $dir into an array */
                  $dh  = opendir($dir);
                  while (false !== ($filename = readdir($dh))) {
                      if (strpos($filename,'.jpg')) {  /* we only want image files */
                          $files[] = $filename;
                      }
                  }
                  /* pick one */
                  $img = array_rand($files);
                  
                  /* splice $dir and $img */
                  $img_path = $dir . $img;
                  return $img_path;
                     
                  ?>

                    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
                    • 20014
                    • 200 Posts
                    BobRay, I have to tell you, your patience and clarity in your responses to the many questions I have posted here have really opened doors in many cases.

                    The examples you give (both here and on your site) gives that one golden kernel of understanding so another can resolve their current problem with your help and then hopefully resolve future issues logically by themselves by now understanding the function of the strings and variables they are looking at.

                    just this code and the examples you give could have a plethora of uses and it actually resolved my original issue in many many less steps!

                    thank you for clarifying, I know it must be tough for you guys to have to confront some rather idiotic questions, especially from us in the php coding learning curve, but I have already found that one of the beauties of modx is that it forces one to understand PHP logic and not just install "modules" haphazardly without some understanding of what’s going on behind the scenes...something which a CMS like joomla is famous for. Case in point, many designers spend considerable amount of time learning how to theme or skin and then that’s it...I for one am quite humbled by the ability of php coders and of php itself, it’s something that needs to be understood precisely because of its power...if I don’t learn the very minimal then I open myself up to a world of hurt...(failed deployments and/or security issues)

                    A thousand thanks to you and the others who take the time to create the needed resources for educating the dreaded noob and for taking a deep breath before jumping into a threads that may seem rather ridiculous to many.
                      • 3749
                      • 24,544 Posts
                      Thanks for the kind words. smiley

                      I like any question I can answer without worrying that I’m doing more harm than good. smiley

                        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