We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47898
    • 14 Posts
    digitalbricks Reply #1, 7 years ago
    Hello MODX community,

    i have the following code inside a chunk for a MIGX output, where the [[+url]] is set via a resource select TV :

    [[+url:isnot=``:then=`<a href="[[~[[+url]]]]">Learn more</a>`]]
    


    This works as expected: Only if a resource is set in the TV and [[+url]] is not empty, the "Learn more" link is displayed.

    BUT If [[+url]] is empty, i got an entry in the MODX error log:
    [2017-05-05 09:19:27] (ERROR in resource 5 @ /www/htdocs/core/model/modx/modparser.class.php : 1353) Bad link tag `[[~]]` encountered
    


    So it seems that the link tag is parsed even if the condition is not met.
    Any ideas how to prevent this behavior?

    Thanks in advance!

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

      • 22840
      • 1,572 Posts
      Try the following
      [[+url:notempty=`<a href="[[~[[+url]]]]">Learn more</a>`]]


      https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers)
        • 38783
        • 571 Posts
        Would providing an 'else' condition work? ie:

        [[+url:isnot=``:then=`<a href="[[~[[+url]]]]">Learn more</a>`:else=``]]
          If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

          email: [email protected] | website: https://andytough.com
          • 47898
          • 14 Posts
          digitalbricks Reply #4, 7 years ago
          Thanks @Paulp and @AndyTough for your suggestions.
          I tried both but none of them made a difference – the "Bad link tag `[[~]]` encountered" is still logged, causing the error log to grow day by day.
          So it seems that MODX first creates the URLs before regarding if-conditions.
            • 38783
            • 571 Posts
            I wonder if specifying it the other way around would help? So that it is empty unless the condition is met?

            [[+url:default=``:isnot=``:then=`<a href="[[~[[+url]]]]">Learn more</a>`]]
              If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

              email: [email protected] | website: https://andytough.com
              • 47898
              • 14 Posts
              digitalbricks Reply #6, 7 years ago
              @AndyTough I also tried your last suggestion but with same result.
              The site in question is running MODX 2.5.5 but referring to https://forums.modx.com/thread/102009/bad-link-tag-since-2-5-5#dis-post-550028 at least 2.5.6 doesn't change this behavior.

              Regardless of the error_level, i think MODX wastes time (and performance) in generating URLs first, despite the condition is not met. Maybe i should file an issue on github?
              • discuss.answer
                • 42562
                • 1,145 Posts
                donshakespeare Reply #7, 7 years ago
                Try this, it should work
                [[[[+url:notempty=`~[[+url]]`]]]]



                Good read:
                https://modx.com/blog/2012/09/14/tags-as-the-result-or-how-conditionals-are-like-mosquitoes/

                [[*field:is=`0`:then=`[[!SomeScript]]`:else=`[[$SomeChunk]]`]]
                Guess what happens when MODX parses this tag?
                1. Each of the tags contained in the then and the else conditional paths are parsed,
                2. The *field tag itself is parsed and
                3. Finally, the conditional filter is applied.
                [ed. note: donshakespeare last edited this post 7 years ago.]
                  TinymceWrapper: Complete back/frontend content solution.
                  Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
                  5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
                  • 42562
                  • 1,145 Posts
                  donshakespeare Reply #8, 7 years ago
                  Question for you is how you gonna put in ya other HTML link parts
                    TinymceWrapper: Complete back/frontend content solution.
                    Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
                    5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
                    • 47898
                    • 14 Posts
                    digitalbricks Reply #9, 7 years ago
                    @donshakespeare Thank you very much for your suggestions and the link to the MODX blog provided.
                    Now i solved the problem this (quite ugly) way:

                    [[+url:isnot=``:then=`<a href="[[[[+url:notempty=`~[[+url]]`]]]]">Learn more</a>`]]
                    


                    Yes, you see it right – another condition inside the first condition. But i can't imagine another straightforward way for creating the MODX link_tag and the surrounding HTML. If i experience performance degradation i will probably move the logic for the markup generation to a snippet but for now it works and the annoying entries in the error log are finally gone. Again, thank you very much.

                    But, to be honest, this [[[[+url:notempty=`~[[+url]]`]]]] syntax feels like a workaround not like the intended way. And even knowing that nested MODX tags are parsed from inside out I do not understand why this is also the case for conditions. It's a waste of energy parsing each branch of the condition path while only one branch is needed.
                      • 3749
                      • 24,544 Posts
                      I almost never use nested conditional output modifiers. In addition to evaluating all the tags, there's all the parsing time.

                      Something like this might be much faster (untested):

                      [[!DoUrl? &url=[[+url]] ]]


                      /* DoUrlSnippet */
                      $url = $modx-getOption('url', $scriptProperties, '', true);
                      return empty($url) ? $url : '<a href="' . $url . '">Learn more</a>';
                        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