We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12410
    • 353 Posts
    Hi Guys,
    I'm experimenting with switching body html based on Resource ID:

    The below code should work but I get a stange var_dump with $resource saying string(7) "12" if I am on resource id 12. Shouldn't string be 2? I think this is whats causing the conditionals not to work right. If I echo $resource I get the corresponding id correctly but the string(7) is possibly causing the issue as it relates to the number of characters in
    [[*id]]
    . Only the conditionals with the || operator aint working. The other are fine. Anybody suggest alternate code to get this working please? THANKS

    <?php
    $resource = '[[*id]]';
    $resource = trim($resource);
    
    if ($resource == '3' || $resource == '24' || $resource == '35' || $resource == '38' || $resource == '42' || $resource == '44' || $resource == '47' || $resource == '32'){
     echo "[[!include? &file==`assets/templates/inc/newslisting.php]]";  
    } 
    else if ($resource == '54' || $resource == '64' || $resource == '75'){
     echo  "[[!include?&file==`assets/templates/inc/bloglisting.php]]";
    } 
    else if ($resource == '56'){
     echo  "[[!include?&file==`assets/templates/inc/contact.php]]";
    } 
    else{
     echo  "[[!include?&file==`assets/templates/inc/textContent.php]]";  
    }
    var_dump($resource);
    [ed. note: howster last edited this post 12 years, 5 months ago.]
    • I hope to get into the code without comments.
      //id get the current resource
      $id = $modx->resource->get('id');
      switch ($id) {
      case 3:
      case 24:
      case 35:
      case 38:
      case 42:
      case 44:
      case 47:
      case 32:
          $res = 'newslisting';
          break;
      case 54:
      case 64:
      case 75:
           $res = 'bloglisting';
          break;
      case 56:
          $res = 'contact';
          break;
      default:
          $res = 'textContent';
      }
      if ($res) include $modx->getOption('assets_path').'templates/inc/'.$res.'.php';
      return;

      I advise you to pay attention http://modx.com/press/modx-the-official-guide/
        Valentin Rasulov - Industrial design (Hobbies - Web Development)
        Development not standard projects on CMF MODx Revolution.
        Small sample of extensions
        • 4172
        • 5,888 Posts
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 12410
          • 353 Posts
          Perfect - THANKS A MILLION

          Of the 3 solutions which all work PERFECT which would you suggest in terms of performance overhead and page load times?

          <?php
          //[[!templateSwitcher? &newslisting=`3,24,35,38,42,44,47,32` &bloglisting=`54,64,75` &contact=`56` &default=`textContent`]]
           
          $id = $modx->resource->get('id');
          unset($scriptProperties['default']);
           
          foreach ($scriptProperties as $file => $ids) {
              $ids = explode(',', $ids);
              if (in_array($id, $ids)) {
                  $params['file'] = 'assets/templates/inc/' . $file . '.php';
                  return $modx->runSnippet('include', $params);
              }
          }
           
          $params['file'] = 'assets/templates/inc/' . $default . '.php';
          return $modx->runSnippet('include', $params);


          with

          [[!templateSwitcher? &newslisting=`3,24,35,38,42,44,47,32` &bloglisting=`54,64,75` &contact=`56` &default=`textContent`]]


          or the second snippet

          <?php
          //id get the current resource
          $id = $modx->resource->get('id');
          switch ($id) {
          case 3:
          case 24:
          case 35:
          case 38:
          case 42:
          case 44:
          case 47:
          case 32:
              $res = 'newslisting';
              break;
          case 54:
          case 64:
          case 75:
               $res = 'bloglisting';
              break;
          case 56:
              $res = 'contact';
              break;
          default:
              $res = 'textContent';
          }
          if ($res) include $modx->getOption('assets_path').'templates/inc/'.$res.'.php';
          return;


          with:

          [[templateSwitcher]]


          I really appreciate the effort you put into these guys!

          Thanks

          PS this worked too by the way for people following the thread.

          <?php
          $resource = $modx->resource->get('id');  //modified from above
           
          if ($resource == '3' || $resource == '24' || $resource == '35' || $resource == '38' || $resource == '42' || $resource == '44' || $resource == '47' || $resource == '32'){
           echo "[[!include? &file==`assets/templates/inc/newslisting.php]]"; 
          }
          else if ($resource == '54' || $resource == '64' || $resource == '75'){
           echo  "[[!include?&file==`assets/templates/inc/bloglisting.php]]";
          }
          else if ($resource == '56'){
           echo  "[[!include?&file==`assets/templates/inc/contact.php]]";
          }
          else{
           echo  "[[!include?&file==`assets/templates/inc/textContent.php]]"; 
          }

          [ed. note: howster last edited this post 12 years, 5 months ago.]