We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30504
    • 14 Posts
    Finally, I’ve done it... "A Modified Snippet Parser")))
    As some of you know, current MODx snippet parser has some problems: it fails to parse snippet correctly, if any of its parameter values contains symbols ’=’ or ’&’. I’ve been tired to think out different tricks to avoid that problem, so I tried to fix the problem from within. As a result, I modified function evalSnippets in document parser object. Now I will describe modified function and it’s features...

    • at first, the code of function was cleaned and slightly optimized
    • of course, snippet syntax is not changed. This function provides all functionality that the original one do. The rules are old: you should enclose snippet parameter values with ’`’ if parameter value has more than one word (word is meant to be a sequence of digits, letters and underscore character (_))
    • it removes problem with ’=’ and ’&’ characters. You can use even ’`’ character, but it should be escaped with backslash, i.e. it should be written as ’\`’ inside parameter value enclosed by ’`’
    • you can use multiline snippets, i.e. you can place every snippet parameter on a separate line. I think, sometimes it’s very comfortable. Remember that you should leave opening square brackets, snippet name and question mark tightly, without any space and on a single line ([[snippetName? .......]]   
    • you can use some kind of comments between parameters, because all text outside parameters is ignored.

    So here is an example:
    [[snippet?    &param = 23                 //this snippet will work 
                  &param2 = 12ds_a345    - this will be treated as one word, so you can leave it as is
                  &param3 = `See me? Lisa&Mike=Fun.`  - and this value contains several words, 
                                                         so it should be enclosed by '`'. And as you can see, you are 
                                                         able to use '=' and '&'
                  &param4=`Testing char\`. Good` - that's correct - escaped character '`'
    
          As you can see any text outside parameters is ignored, 
          so you can use this feature... or bug... to make some comments
          to your snippet parameters
    ]]
    


    And finally... Where’s the function? wink
    It is attached below))) Hope, it will be useful.
    If you have some suggestions or if you will find some bugs - please, let me know (i haven’t found yet wink ).

      • 27376
      • 576 Posts
      It doesn’t look like this mod/hack accepts snippets in the form of:
      [[snippet?param1=`test` &param2=`test`]]
      Also, [tt]array_combine[/tt] is a php5 only function. However, there are substitute functions for versions < php5 so that shouldn’t be an issue.

      MODx 0.9.7 will fix most of the issues you brought up here. It should be ready for testing come summer.
        • 22289
        • 41 Posts
        This worked beautifully in fixing an issue I was having with passing URLs as a param- but the site went down in the process- any ideas? Does this mean that every instance of every snippet everywhere needs to have two parameters passed to it?, even if there are no parameters being passed? That’s as best as I can make of this. There is a ditto instance being called on the site’s main page, that could be what generates said error...

        « MODx Parse Error »
        MODx encountered the following error while attempting to parse the requested resource:
        « PHP Parse Error »
         
        PHP error debug
          Error:    array_combine() [function.array-combine]: Both parameters should have at least 1 element   
          Error type/ Nr.:    Warning - 2   
          File:    /home/mysite.com/manager/includes/document.parser.class.inc.php   
          Line:    854   
          Line 854 source:    $parameters = array_combine( $matches[1], $matches[2] );    
         
        Parser timing
          MySQL:    0.0889 s   (4 Requests)
          PHP:    0.1698 s   
          Total:    0.2586 s