We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22193
    • 5 Posts
    In short:
    How to process snippet inside piece of chunk returned from another snippet?

    In long:
    I need a banner set up in my web sites header. But the problem is that I have banners in different sizes and formats. Some of them require JavaScript to run. Because of that I can’t use regular banner rotation. But I want to use tracking on banners which are simple GIF/JPG.

    So I was going to use Ad Manager for tracking simple banners. And it worked nice for that except that my design requires a changing <div> around it in case if different sizes and other Flash banners.

    So to work around it I tried to use Random Paragraph which pulls random paragraph from hidden document which contains list of all banners.

    Example of hidden document:
    <p>
    ...FLASH Banner code...
    </p>
    
    <p>
    ...JavaScript Banner code..
    </p>
    
    <p>
    [!adManagerDisplay?position=`1`&doc=`2`!]
    </p>
    
    <p>
    [!adManagerDisplay?position=`2`&doc=`2`!]
    </p>
    
    ...
    



    But the problem is that Ad Manager’s snippet is coming out as text and doesn’t get parsed. Is there any way to parse chunk before it’s text gets processed by another chunk?
      • 22193
      • 5 Posts
      Not possible in ModX?
        • 22840
        • 1,572 Posts
        Quote from: omare8 at Jun 23, 2010, 09:18 PM

        Not possible in ModX?

        Well that’s something I don’t believe and have never heard before wink

        I don’t really know how to do it without copying your setup and its getting late here now, but I am sure its possible, either using PHX or a custom snippet.

        Hopefully me posting here will bump it up for Susan, Bob or one of the MODx team to notice grin
          • 4041
          • 788 Posts
          It sounds a like nested cached/uncached snippet order issue. Since you didn’t show the Random Paragraph snippet call, I am assuming you used uncached [! !] snippet tags on that call. Try using [[ ]] on the Random snippet and see if that helps.
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 22193
            • 5 Posts
            I used cached snippet on banner manager and uncashed on random paragraph and got it working that way. But the only problem is that I had to create multiple positions in banner manager - one per each banner to make it work - otherwise it is caching one and don’t show other banners if I put them all in one position. Not the cleanest solution but I got it to work.
            But I am still looking for cleaner solution.
              • 4041
              • 788 Posts
              You may also have top uncheck caching for the page itself so that your dynamic content works correctly.
                xforum
                http://frsbuilders.net (under construction) forum for evolution
                • 22193
                • 5 Posts
                Problem is that it’s in header of every page and I don’t want to disable caching for the whole site.
                  • 18913
                  • 654 Posts
                  I’ve never played around with RandomParagraph, but I took a look at the code. First, I created a testsnippet that just returned some text and the values of passed parameters. Then I replaced the last line of the RandomParagraph code with this
                    //return str_replace('[+rp.paragraph+]', $results[$method][$rand], str_replace('[+rp.sourcePageUrl+]', $modx->makeUrl((int)$pageId), $template));
                    $output1 = str_replace('[+rp.paragraph+]', $results[$method][$rand], str_replace('[+rp.sourcePageUrl+]', $modx->makeUrl((int)$pageId), $template));  
                    $output2 = trim($output1);
                    $snippet_flag = ((strpos($output2, '[!') && strpos($output2, '!]')) || (strpos($output2, '[[') && strpos($output2, ']]'))) ? true : false;
                    if ($snippet_flag)
                    {
                      $output2 = str_replace('[!','',$output2);
                      $output2 = str_replace('!]','',$output2);
                      $output2 = str_replace('[[','',$output2);
                      $output2 = str_replace(']]','',$output2);
                  
                      $with_params_flag = strpos($output2,'?');
                      if ($with_params_flag)
                      {
                        $snippet_ra = explode('?',$output2);
                        $snippet_name = $snippet_ra[0];
                        $snippet_params = explode('&', $snippet_ra[1]);
                        $passed_params = array();
                        foreach ($snippet_params as $key => $value)
                        {
                            $temp_ra = explode("=",$value);
                            $passed_params[$temp_ra[0]] = $temp_ra[1];
                        }
                        $output2 = $modx->runSnippet('testsnippet',$passed_params);
                      }
                      else
                      {
                        $output2 = $modx->runSnippet('testsnippet',array());
                      }
                    }
                  
                    return $snippet_flag ? $output2 : $output1;
                  


                  The document that this referred to had three "p" tags : the first two were random text, the third was a call to ’testsnippet’.

                  The above amended version of RandomParagraph correctly returned either the random text or the output of the snippet.

                  That said, I didn’t figure out how to pass the snippet name by a variable (e.g. how to use $snippet_name instead of the hardcoded ’testsnippet’) in my quick look. And the values being passed to the snippet included the backslashes, so that would probably need to be changed. And, of course, the entire block of code could probably be written in a much tighter fashion. However, maybe it’s a start.

                  Regards,
                  Matt
                  • FWIW, this can easily be accomplished in Revolution, which fully supports nested tags. One of the main reasons for MODx Revolution to even be developed.
                      • 22193
                      • 5 Posts
                      Great piece of code. I think it can be easily modified to enable nested tags. Just get tags name from sub-string and use it instead of constant ’testsnippet’.

                      I was going to install Revolution but couldn’t even install it - install doesn’t work on my WAMP test server.