We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26401
    • 247 Posts
    I’d like to create a cross between ditto and @DIRECTORY binding.

    essentially I want to create something like a gallery where all images’ urls (that are in a set folder) will be pushed into a chunk through a placeholder.

    is there something like this in place already?
      • 26401
      • 247 Posts
      well if there isn’t here’s a snippet I came up with
      I borrowed a chunk from MikeT’s Randomimage
      http://modxcms.com/extras/package/364

      <?php
      /*
      ::::::::::::::::::::::::::::::::::::::::
       Snippet name: ParseImages
       
      Description:
          parses images in &folder in chunk &tpl
      ::::::::::::::::::::::::::::::::::::::::
      Usage:
          [!ParseImages? &tpl=`myChunk_tpl` &folder='assets/images/folder'!]
      */
      	//	$folder;
      	//	$tpl;
      	$output = "";
      	$filesArray = Array();
      	
      	$imgDir = opendir ($folder);
      
      	while ( $file = readdir( $imgDir ) )
      	{	
      		//checks that file is an image
      		$file_type = strrchr( $file, "." );
      		$is_image = eregi( "jpg|gif|png",$file_type );
      		
      		if ( $file != '.' && $file != '..' && $is_image )
      		{
      			array_push($filesArray,$file);
      		}
      	}
      	
      	rsort($filesArray);
      	
      	foreach ($filesArray as &$fn) {
      	
      		// set the placeholder 
      		$params['image']=$modx->config['site_url'].$folder.$fn;
      		$params['imagefilename']=$fn;
      		
      		// Parse the chunk
      		$output .= $modx->parseDocumentSource($modx->parseChunk($tpl, $params, '[+', '+]'));
      	}
      	closedir ($imgDir);
      
      	return $output;
      ?>
      


      edit - added rsort() to sort alphabetically descending - newest at the top (if you number the images)
      can be switched to sort() for reverse(esc)
        • 26401
        • 247 Posts
        okay, so even though it is working, I get this problem

        in the chunk if I fetch [+image+] I get the image - YAY
        but if I try and use [+image:phpthumb etc.. +] I get nothing.

        I’m sure this has something to do with the order of parsing, but I don’t know how to fix it.

        any help here?
        I tried
        $output .= $modx->parseDocumentSource($modx->parseChunk($tpl, $params, '[+', '+]'));


        instead of
        $output .= $modx->parseChunk($tpl, $params, '[+', '+]');


        but that didn’t work...
          • 2912
          • 315 Posts
          FileDownload snippet will do what you want too. You can point it to the folder of your choice. List specific valid extensions and change the Template Chunk to suit your formatting needs.
            BBloke
            • 26401
            • 247 Posts
            sweet, thanks !!