We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 54277
    • 8 Posts
    Is it possible to add something like "else if" in Chunks?
    [[*id:eq=`resource id`:or:eq=`resource 2 id`:then=``:else=``]]
      • 17301
      • 932 Posts
      Yeah but in chunks the placeholder would be a [[+id]] instead of [[*id]]
        ■ email: [email protected] | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
        • 54277
        • 8 Posts
        Quote from: lkfranklin at Jun 15, 2018, 06:44 AM
        Yeah but in chunks the placeholder would be a [[+id]] instead of [[*id]]

        how should I write the else if code?

        [[*id:eq=`resource id`:or:eq=`resource 2 id`:then=`

        `:elseif:*id:eq=`b`:then=`

        `]]

        is it?
          • 13226
          • 953 Posts
          @zzfkelvin

          Are you using Revo ?
            • 3749
            • 24,544 Posts
            Stuff like this is infinitely faster and more reliable in PHP, and not very complicated:
            (I'm guessing at what you want -- feel free to clarify)

            [[!SomeSnippet]]



            /* SomeSnippet code */
            
            $output = ''
            $docId = $modx->resource->get('id');
            
            if ($docId == 12 || $docId == 22) {
               $output = 'something';
            } else {
               $output = 'something else';
            }
            
            return $output;

              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
              • 54277
              • 8 Posts
              Quote from: BobRay at Jun 17, 2018, 05:44 AM
              Stuff like this is infinitely faster and more reliable in PHP, and not very complicated:
              (I'm guessing at what you want -- feel free to clarify)

              [[!SomeSnippet]]



              /* SomeSnippet code */
              
              $output = ''
              $docId = $modx->resource->get('id');
              
              if ($docId == 12 || $docId == 22) {
                 $output = 'something';
              } else {
                 $output = 'something else';
              }
              
              return $output;


              I'm trying to add some schema codes into our website, I created a chunk named XJTLU_Schema, and call this chunk inside site.head chunk.
              In this XJTLU_schema chunk, I set some condition to add schema codes according to different page template, for example,

              [[*template:eq=`28`:then=`
              <script type="application/ld+json">
              {
              "@context": "http://schema.org",
              "@type": "Person",
              "name": "[[+sedona.name]]",
              "honorificSuffix": "[[+sedona.highdegree]]",
              "jobTitle": "[[+sedona.rank]]",
              "description": "[[+sedona.bio]]",
              "image": "[[+sedona.image]]",
              "address": "[[+sedona.office]]",
              "email": "[[+sedona.email]]",
              "telephone": "[[+sedona.phone]]"
              }
              </script><!--Schema for staff-->
              `:else=``]]

              [[*template:eq=`23`:or:is=`24`:or:is=`37`:then=`
              <script type="application/ld+json">
              {
              "@context": "http://schema.org",
              "@type": "EducationalOrganization",
              "name": "[[*longtitle]]",
              "description": "[[*content]]",
              "address": "[[*contactAddress:nl2br]]",
              "email": "[[*contactEmail]]",
              "telephone": "[[*contactPhone]]"
              }
              </script><!--Schema for department,centre & research insitution-->
              `:else=``]]

              I only need to identify some key templates with different types, such as person, organization, event.....But for the other pages, I don't want to type the template ID one by one, I think if it could has some code like "else" to apply one all the other pages.
                • 54277
                • 8 Posts
                Quote from: iusemodx at Jun 15, 2018, 09:51 PM
                @zzfkelvin

                Are you using Revo ?

                yes
                  • 3749
                  • 24,544 Posts
                  Don't put all that code on every page -- that's crazy. Think about what happens when you need to change a script.

                  Put the scripts in three chunks named Chunk28, Chunk23-24-39, and DefaultChunk. Then do this.

                  Put this where you want the script:

                  [[!AddScript]]


                  Create a snippet called AddScript with this code:

                  /* AddScript snippet */
                  $chunkName = '';
                  
                  $template = $modx->resource->get('template');
                  
                  switch($template) {
                     case 28: 
                       $chunkName = 'Chunk28';
                       break;
                  
                     case 23:
                     case 24:
                     case 37:
                       $chunkName = 'Chunk23-24-37';
                       break;
                     
                     default:
                       $chunkName = 'DefaultChunk';
                       break;
                  }
                  
                  return $modx->getChunk($chunkName);


                  It will be much faster and infinitely easier to maintain.
                    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