We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18839
    • 39 Posts
    Hi everyone,

    I was wondering on how to do this.

    If I have a snippet with a function in it:

    <?php
    function sample()
    {
        echo "Some text in here";
    }
    ?>
    


    How would I call just that function in the snippet from a chunk?

    Chunk Code:
    [[snippetName? &runSampleFunction]]
    <p>This is some html in my chunk</p>
    
    


    Thanks
      • 4971
      • 964 Posts
      Can this be done using an associative array?
        Website: www.mercologia.com
        MODX Revo Tutorials:  www.modxperience.com

        MODX Professional Partner
        • 18839
        • 39 Posts
        I got the snippet to run depending on the value of the param I passed to it, am calling the snippet multiple times on a page so I wrapped the functions inside if (!function_exists(functionName)), (don’t know what exctly that does, but it’s working smiley )

        For some reason if I put the functions below the switch statement I get an error message saying "function not found", huh, but if I put the functions before the switch statement the code in the snippet runs fine.
        Do functions always have to go before the switch statement or is it something to do with
        if (!function_exists(functionName)) ?

        Code in template:
        [!mySnippetWithMultipleFunctions? &runFunction=`sample`!]
        


        <?php
        if (!function_exists(myFunctionOne))
        {
            function myFunctionOne()
            {
                echo "My Function <strong>One</strong> has run." . "<br />\n";
            }
        }
        
        if (!function_exists(myFunctionTwo))
        {
            function myFunctionTwo()
            {
                echo "My Function <strong>Two</strong> has run." . "<br />\n";
            } 
        }
        
        switch($runFunction)
        {
            case "sample":
                myFunctionOne();
            break;
            
            case "anotherSample":
                myFunctionTwo();
            break;
            
            default:
                echo "Default";
            break;
        }
        ?>
        


        Is this the normal way to call a specific function inside a snippet that contains multiple functions, or is there a way to directly call the functions from the template/chunk?

        Thanks for the help tongue
          • 18839
          • 39 Posts
          Hi ganeshXL,

          Thanks for the help,

          I am using the if(function_exists(php5onlyFunc)) { ..... } in the snippet, because I am calling the snippet multiple times in the template code.
          If I dont wrap each function in the snippet inside if(function_exists(){ ..... }, I get an error saying:

          Fatal error: Cannot redeclare myfunctionone() (previously declared in C:\wamp\www\MODx\manager\includes\document.parser.class.inc.php(766) : eval()’d code:5) in C:\wamp\www\MODx\manager\includes\document.parser.class.inc.php(766) : eval()’d code on line 6

          Also, it’s better to use return($var) instead of echo() statements.

          When I use return($var), when PHP gets to the return, the code in the snippet stops executing and returns the value back to the page.

          I am trying to write a generic set of functions for sites that I develop in MODx and I would like to call a function in this code multiple times from the template file or chunk.

            • 22303 MODX Staff
            • 10,725 Posts
            This is what include_once and require_once are for, including functions or classes once (or any piece of PHP code), regardless of how many times or places you call it in the same request (i.e. from 10 different snippets).

            Return content at the end after buffering it into either an output buffer (no change to echo’s or print’s) or a string.