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

    I have this setup

    Modx 1.0.2

    The template looks like
    [[IncludeFile? &file=`assets/templates/my-template.html`]]


    inside the template I have 2 calls to the same snippet

    [[testSnippet]]
    [[testSnippet]]


    The snippet looks like

    <?php
    include_once($modx->config['base_path'] . 'assets/snippets/test-snippet.php');
    ?>


    The file test-snippet.php is

    <?php
    echo "BBBB";
    return "AAAA";
    ?>


    The output of the template is

    BBBB



    I see 2 problems:

    1. return doesn’t work.
    2. Only the first call is successful.

    Any ideas why this is happening?

    Thank you



      • 21257 MODX Staff
      • 730 Posts
      It’s only running once because of include_once.
      (Note that you don’t need brackets on include and require statements.)
      You need to do the return from the snippet itself, not from the included file.
      So in test-snippet.php, instead of the return statement, just set a variable, like $output = ’foo’;
      Then return it from the main snippet code.

      <?php
      include $modx->config['base_path'] . 'assets/snippets/test-snippet.php';
      return $output;
      ?>
      

        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 26931
        • 2,314 Posts
        http://tipsfor.us/2009/11/02/writing-custom-php-snippets-for-modx-part-vii/
        "You can’t return a value directly from an included file: because MODx treats Snippets as functions, it’s considered good form to always return a value, e.g. “return $output;” or “return TRUE;” but this MUST be returned from the original Snippet in the database; if you return output from the included file, you’ll have to return it again from the original database Snippet code. See the video for this quirk in action."
          • 5340
          • 1,624 Posts
          Using include instead of include once fixed the problem.

          I’ll have a look at the video too.

          Thx guys