We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30837
    • 20 Posts
    I have a weird problem that needs some expert help...

    I’m trying to build a very simple site using a very simple template... wanting to use external files for my tpl files...

    So... a snippet called ’include’:
    <?php
    if (file_exists($file)) {
       $tpl = include $file;
    } else { $tpl = 'File not found at: '.$file; }
    return $tpl;
    ?>

    And a call to the snippet inside my template:
    [[!include? &file=`assets/templates/header.php`]][[!include? &file=`assets/templates/menu.php`]]
    [[!include? &file=`assets/templates/footer.php`]]

    Returns a very strange thing: a number 1 appears before each template section when I preview the page, see:
    http://www.blutree.tv/modx

    Anybody have any clue why? lol
      • 36702
      • 76 Posts
      I’m guessing that $tpl = include $file; returns true on success (ie 1)

      Shouldn’t you be using $tpl = file_get_contents($file);
        • 30837
        • 20 Posts
        Wow. that makes complete sense.
        That did it.
        Thanks for your LIGHTNING QUICK reply!

          • 28042 ☆ A M B ☆
          • 24,524 Posts
          The return value of the include directive is 1 for success and 0 for failure. This drove me nuts for a long time when I first began writing serious snippets that included external php files, as I thought I was being clever in using a one-liner to do the include and the return all on one line. The lesson I learned was to be careful of what I return, because it’ll do exactly what I ask. Unlike when raising teenaged boys.
            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