We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23178
    • 22 Posts
    Ok so I am trying something pretty simple. I want to use external templates.

    I have created a super simple snippet that goes like this:
    $a = include ’assets/templates/test.html’
    return $a;

    I keep getting the random number 1 inserted. If I echo the include instead like this
    echo include ’assets/templates/test.html’
    then there is no number 1 inserted.

    Just to be sure I wiped out my install, re-installed a vanilla install and did nothing other than create this one snippet and call it in the base template created on install. I also recreated my html file to include nothing other than a h1 tag with some test text.

    What is going on here? Banging my head against the wall!

      • 28215
      • 4,149 Posts
      You shouldn’t use include. Use file_get_contents. Create a snippet named ’getTpl’ or such:

      $output = '';
      if (file_exists($file)) {
         $output = file_get_contents($file);
      } else {
         $modx->log(modX::LOG_LEVEL_ERROR,'Template not found at: '.$file);
      }
      return $output;
      


      Then call it:

      [[!getTpl? &file=`[[++assets_path]]templates/test.html`]]
      

        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 23178
        • 22 Posts
        Ah ok thank you! I was following the example in the snippet documentation here http://rtfm.modx.com/display/revolution20/Snippets.

        Thanks again for leading me in the right direction.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Consider what include is actually returning into your variable. Include is not a function, it is a directive. It includes the specified file, and returns 1 for success or 0 for failure.

          So you would do this
          include 'file.php';
          return;
            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
            • 23178
            • 22 Posts
            Thank you for that extra bit of info. Now I understand where that 1 was coming from wink