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

    I would like to template a list of countries ordered by continents :
    $continents           = ["EUROPE","ASIA"];
    $continents['EUROPE'] = ["France","Germany","Switzerland"];
    $continents['ASIA']   = ["Russia","China","Thailand"];
    
    foreach ( $continents as $continent ) {
    
            $arraycontinent = ...
    
            $output .= $modx->getChunk('tplContinent', $arraycontinent);
    
            foreach ($continent as $country) {
    
                    $arraycountry = ...
    		
                    $output .= $modx->getChunk('tplCountry', $arraycountry);
    
            }
    
    }


    The problem is I don't get the expected result.

    I want this :
    EUROPE : 
     - France
     - Germany
     - Switzerland
    ASIA :
     - Russia
     - China
     - Thailand


    But I get this :
    EUROPE : 
    ASIA :
     - France
     - Germany
     - Switzerland
     - Russia
     - China
     - Thailand


    Could someone help please ?

    This question has been answered by markh. See the first response.

    • discuss.answer
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      I think this should work for you, though haven't tested:

      $continents           = ["EUROPE","ASIA"];
      $continents['EUROPE'] = ["France","Germany","Switzerland"];
      $continents['ASIA']   = ["Russia","China","Thailand"];
      
      $output = array();
      foreach ($continents as $continentName => $continent) {
          $continentCountries = array();
          foreach ($continent as $country) {
              $continentCountries[] = $modx->getChunk('tplCountry', array(
                 'country' => $country,
              ));
          }
          $continentCountries = implode('', $continentCountries);
          $output[] = $modx->getChunk('tplContinent', array(
              'continent' => $continentName,
              'countries' => $continentCountries,
          ));
      }
      $output = implode('', $output);
      return $output;
      
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 47622
        • 9 Posts
        thank You ! Yes it works !