We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14214
    • 299 Posts
    I have a snippet
    <?php
    include("geo/geoipcity.inc");
    include("geo/geoipregionvars.php");
    $gi = geoip_open("/home/asens/public_html/geo/GeoLiteCity.dat", GEOIP_STANDARD);
    
    $record = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    if($record->country_code == "US"){
    echo '[[$domesticResponse]]';
    }
    else{
    echo '[[$intlResponse]]';
    }
    geoip_close($gi);


    I would like to have the default be [[$domesticResponse]] and [[$intlResponse]] but wish to have the option of setting these in the snippet call for something other than the default. What changes do I need to make to the above code in order to set those as the default or to select a chunk at the call?

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

    • discuss.answer
      • 4172
      • 5,888 Posts
      try

      <?php
      
      $tplDomesticResponse = $modx->getOption('tplDomesticResponse', $scriptProperties, 'domesticResponse');
      $tplIntlResponse = $modx->getOption('tplIntlResponse', $scriptProperties, 'intlResponse');
      
      include ("geo/geoipcity.inc");
      include ("geo/geoipregionvars.php");
      $gi = geoip_open("/home/asens/public_html/geo/GeoLiteCity.dat", GEOIP_STANDARD);
      
      $record = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
      if ($record->country_code == "US") {
          $output = $modx->getChunk($tplDomesticResponse);
      } else {
          $output = $modx->getChunk($tplIntlResponse);
      }
      geoip_close($gi);
      
      return $output;
      
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 14214
        • 299 Posts
        Awesome! Thank you Bruno!
          • 14214
          • 299 Posts
          Bruno, follow up question if you don't mind. If I want to use that script somewhere else on the page I will get an error. How can I use the result elsewhere without re-querying?
            • 3749
            • 24,544 Posts
            Just a note -- if those properties exist in the snippet's default properties and are empty, the default value in the snippet won't be used unless you use true as a fourth argument to getOption():

            $tplDomesticResponse = $modx->getOption('tplDomesticResponse', $scriptProperties, 'domesticResponse', true);
            $tplIntlResponse = $modx->getOption('tplIntlResponse', $scriptProperties, 'intlResponse', true);


            If you need values elsewhere on the page (as long as it's below your snippet tag), here's one way:

            $modx->setPlaceholder('somePlaceholder', $value);


            The value will be available later in the page with either a placeholder tag or in a snippet with:

            $value = $modx->getPlaceholder('somePlaceholder');
              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