We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34066 ☆ A M B ☆
    • 23 Posts
    I need help creating two conditional tags or output modifier to use in my templates.
    (1). Basically I have a TV in one template (will call it "thisTV") and (thisTV) consists of 2 checkboxes
    (box A ==[[this]]||box B== [[that]])
    .

    The checkboxes (box A & box B) are options for 2 pre-templated chunks.

    Option B (box B) requires extra js & css inside the
    <head></head>
    element, so i need a function like this:


    -Parse {thisChunk} here if (box B) in on my template variable is checked. (I will use this inside the <code><head></head></code> element of my template kinda like this.

     [[if checkbox (box B):in (thisTV) =is checked: then parse {thischunk} here]]



    (2). The Other conditional TV is for another template the function is like this:

    -Parse {thisChunk} here on every page that is child of this ID; and Parse {thisOTHERChunk} here on every page that is child of this other ID

    [[*parent:is=`4`:then=`[[$this-chunk]]; if-[[*parent:(is-not-4-but-8)]]:then=`[[$another-chunk]]` ]]


    I spent a considerable amount of time on the web looking for this before comming here, so I do apologize in advance if my explanation does not make sence or if this is not possible, I also looked through the documentation but did see any examples that were good enough or relevant enough to enable my understanding.. Your insight is much appreciated. Thanks
      Tony Porto | Valpo Creative | http://www.valpocreative.com
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I wouldn't recommend using Output Modifiers for either case. Butter knives are for buttering bread, you really shouldn't use them as pry tools or screwdrivers wink

      In the first case, I would have a snippet in the B chunk that uses $modx->regClientCSS() and $modx->regClientStartupScript(). Then you can just use the TV tags as usual.

      For the second case, since it's basically a three-way conditional, I'd use a snippet instead. Anything more complicated than a simple yes/no conditional can become resource-expensive.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 34066 ☆ A M B ☆
        • 23 Posts
        Makes perfect sense, just sounds harder(but better) given my PHP skills.
          Tony Porto | Valpo Creative | http://www.valpocreative.com
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Make a stab at it, and we'll be here to help if you get stuck. Even now, it usually takes me two or three tries to get even the simplest snippet to work right. When a snippet works right the first time, it's cause for celebration wink

          Something like this should do the trick:
          $parent = $modx->resource->get('parent');
          switch ($parent) {
              case 4:
                  return '[[$this-chunk]]';
                  break;
              case 8:
                  return "[[$that-chunk]]";
                  break;
              default:
                  return;
                  break;
          }
          return;

          While you cannot use a chunk directly in PHP code, you can return a set of chunk tags. MODx will process the chunk tag as usual on the next pass through the parser. While you could use the API chunk parsing functions, it's easier to let MODx do the heavy lifting wink
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 34066 ☆ A M B ☆
            • 23 Posts
            Quote from: sottwell at Oct 11, 2013, 12:42 AM
            Make a stab at it, and we'll be here to help if you get stuck. Even now, it usually takes me two or three tries to get even the simplest snippet to work right. When a snippet works right the first time, it's cause for celebration wink

            Something like this should do the trick:
            $parent = $modx->resource->get('parent');
            switch ($parent) {
                case 4:
                    return '[[$this-chunk]]';
                    break;
                case 8:
                    return "[[$that-chunk]]";
                    break;
                default:
                    return;
                    break;
            }
            return;

            While you cannot use a chunk directly in PHP code, you can return a set of chunk tags. MODx will process the chunk tag as usual on the next pass through the parser. While you could use the API chunk parsing functions, it's easier to let MODx do the heavy lifting wink


            Awesome, Thank you.. I was actually just reading on how to register the css. I did find this:

            $modx->regClientCSS('assets/css/style.css');


            Only confused on how to tell modx to call this when my chunk is selected.

            Thank you
              Tony Porto | Valpo Creative | http://www.valpocreative.com
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              As far as using the $modx->regClient...() API functions, they're pretty easy to use. Just define the CSS, HTML, or JS block you want inserted, or a path to a file, and MODx takes care of rest of it.
              $css = '<style>
              .blue {color:blue;}
              .red {color:red;}
              </style>';
              $modx->regClientCSS($css);
              $js = 'assets/js/myjsfile.js';
              $modx->regClientStartupScript($js);
              
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 34066 ☆ A M B ☆
                • 23 Posts
                Quote from: sottwell at Oct 11, 2013, 12:52 AM
                As far as using the $modx->regClient...() API functions, they're pretty easy to use. Just define the CSS, HTML, or JS block you want inserted, or a path to a file, and MODx takes care of rest of it.
                $css = '<style>
                .blue {color:blue;}
                .red {color:red;}
                </style>';
                $modx->regClientCSS($css);
                $js = 'assets/js/myjsfile.js';
                $modx->regClientStartupScript($js);
                
                So how does the connection between the snippet and chunk work. In other words when this chunk is selected on the TV how does Modx know to use the css & JS that we registered in the snippets.
                  Tony Porto | Valpo Creative | http://www.valpocreative.com
                  • 3749
                  • 24,544 Posts
                  If your CSS is in the chunks, you can just do something like this:


                  $parent = $modx->resource->get('parent');
                  switch ($parent) {
                      case 4:
                          $chunkName = 'this-chunk';
                          break;
                      case 8:
                          $chunkName = 'that-chunk';
                          break;
                      default:
                          return '';
                          break;
                  }
                  
                  $css = $modx->getChunk($chunkName);
                  $modx->regClientCSS($css);
                  
                  return '';
                    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
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Put the snippet in the chunk's content. When the chunk is loaded, the snippet is run.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 34066 ☆ A M B ☆
                      • 23 Posts
                      Awesome, thank you both Susan & Bob.
                        Tony Porto | Valpo Creative | http://www.valpocreative.com