We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29164
    • 3 Posts
    I am looking for the API call that will allow me to get a list of all documents and their aliases using a certain template to create a list of links from.

    All input is appreciated.

    Tabor Carlton
      • 29164
      • 3 Posts
      SOLUTION FOUND!!

      After some lengthy API digging (and a lot of modifications) I found a way to accomplish this.

      What I’m doing is generating a list of links for every page that uses a certain template so I can have a dynamically generated list for a news story listing.

      In this case I’ve pulled the long title as the link text,

      <?php
      	
      	// get list all documents in site
      	$list = $modx->documentListing;
      	
      	// convert to numerical array
      	// * this is required, there are  no numeric indexes available
      	$list = array_values($list);
      	
      	
      	// loop through all documents
      	for ($i=0; $i<sizeof($list); $i++) {
      	
      		// get information about each page based on document Id ($list[$i])
      		$array = $modx->getPageInfo($list[$i],1,'template, alias, longtitle'); 
      		
      	
      		// only generate menu items from these templates (in this example template id 3)
      		if ($array['template']==3) {
      			print '<a href="'.$array['alias'].'">'.substr($array['longtitle'],0, 40).'...</a>';
      		}
      		
      	}
      	
      	
      ?>



      Hope this helps someone else!

      Tabor
        • 6705
        • 79 Posts
        Thanks for this, Tabor.

        How would I go about using that code? Would I just save it as a snippet, and then make a page which calls that snippet?

        I want to find out which pages use which template, as we have ended up with a number of interim and development templates on our site as we have progressed, and it’s now time for a tidy-up to get rid of unused templates and replace little-used templates with others, where possible. However, there doesn’t seem to be anything in the MODx Manager which shows this information (or I’ve not found it, if there is), so this code might be of use to me.
          Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
          • 29163
          • 18 Posts
          Yes, throw that code into a snippet (PHP must be run in snippets) and then you can just call that snippet in the [*content*] variable - don’t even worry about going into code view or anything... just dump it straight into the rich text area for your [*content*] variable.

          Try this in your snippet call David:

          <?php
          	
          	// get list all documents in site
          	$list = $modx->documentListing;
          	
          	// convert to numerical array
          	// * this is required, there are  no numeric indexes available
          	$list = array_values($list);
          	
          	
          	// loop through all documents
          	for ($i=0; $i<sizeof($list); $i++) {
          	
          		// get information about each page based on document Id ($list[$i])
          		$array = $modx->getPageInfo($list[$i],1,'template'); 
          		
          	
          		print 'This document uses template: ' . $array['template'];
          			
          		
          	}
          	
          	
          ?>


          Hope that answers your question.

          Tabor
            Tabor Carlton // Interactive Art Director
            http://www.taborcarlton.com
            http://www.alternative-media.net