We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26310
    • 130 Posts
    i’m trying to create a simple snippet that can be called multiple times on a page.

    This snippet takes 3 parameters and outputs two placeholders.

    Basically, you enter in the original price of an item (p), a sale price (s), and a boolean determining whether or not the item is on sale (c).

    I had this working great until I tried to add it to a Ditto call and then realized it was screwing up when it was called multiple times. Now I’m completely lost.
    <?php
    if(!function_exists(salecost)) {
    function salecost($p, $s, $c){
    if ($c == '1') {
    	$cost = '<del>$'.$p.'</del><br /><span style="color:red;">$'.$s.'</span>';
    $frmcost = $s;
    }else{
    	$cost = '$'.$p;
    $frmcost = $p;
    }
    $modx->setPlaceholder('cost', $cost);
    $modx->setPlaceholder('frmcost', $frmcost);
    
    }
    return;
    }
    ?>
    


    Sample of how I’m calling it:
    [[salecost? &p=`20` &s=`12` &c=`1` ]]


    It just doesn’t produce anything now. Do I need to add a function call within the snippet? How do I execute a snippet wrapped in a function without calling the function?

      I twitch because I care....and drink too much coffee.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The problem is the setting of the placeholder; each snippet overwrites the placeholders with their own values until finally you’re left with just the ones from the last snippet to be run. Can’t you use a TV to set these values instead? And format the output in your Ditto chunk tpl. I would imagine phx would be very useful here.
        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
        • 26310
        • 130 Posts
        Do I need to clear the placeholders after I use the return? I can hack my way through PHP but snippet creation is new to me and the Wiki is a little daunting.

        Edit: Perhaps what I should be using is PHx? If TV is empty do nothing but if there’s a value display value? Is that where PHx can come in?
          I twitch because I care....and drink too much coffee.
          • 10449
          • 956 Posts
          You don’t have to use placeholders. Just return() or echo() the output instead. And make sure you reset/initialize the $output var.
            • 26310
            • 130 Posts
            Still no luck.

            Do I need to place a function call within the snippet?


            Ok so I changed the output to an echo call and just put my snippet call where I wanted the output to be. I guess I was just trying to get all fancy with using placeholders. Thanks for your help
              I twitch because I care....and drink too much coffee.