We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37619
    • 79 Posts
    Is there a way to add a snippet within a snippet?

    For instance, let's say I have 2 snippets (getCategory & translateVar):

    getCategory snippet:
    <?php
    
    $var = "toto";
    
    [[varTranslate? &var=`$var`]]
    
    $output = "Hello ".$name;
    
    return $output;
    


    varTranslate snippet:
    <?php
    switch($var){
      case "toto": $name="Thomas"; break;
    }
    return $name;
    
    


    In the end, I'd like [[getCategory]] to return the $output "Hello Thomas".

    Is this possible? Am I doing it the right way?

    Thank you for your help!

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

      Jean-Marc Buytaert (@jmbuytaert)

      MODX truly is the greatest thing that's ever happened to the Internet.
    • discuss.answer
      • 22303 MODX Staff
      • 10,725 Posts
      You can use the API:

      <?php
      $name = $modx->runSnippet('varTranslate', array('var' => $var));
      

        • 37619
        • 79 Posts
        Thank you opengeek! Works like a charm!
          Jean-Marc Buytaert (@jmbuytaert)

          MODX truly is the greatest thing that's ever happened to the Internet.