We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42852
    • 2 Posts
    Hello,
    I have followed 2 tutorials show how to include external files but non seem to work?
    im currently only trying to call the header / footer / content.



    - The first way says to create a snippet called "includeTemplate" with this code:
    <?php
    if ( !isset($tpl) || $tpl== "" ) return "Missing Template file!";
    $tpl = $base_path.’assets/templates/’.$tpl;
    ob_start();
    include($tpl);
    return ob_get_clean();
    


    and then call the header by pasting this code in to the header chunk
    [[includeTemplate? &tpl='assets/templates/main/site_header.html']]






    - The second way says to create a snippet called "includeFile" with this code:
    <?php
    # Snippet to include files from filesystem
    # [[includeFile? &file=`assets/templates/mytemplate/file.html`]]
     
    if ( !isset($file) || $file== "" ) return "No file specified."; //check if there's a file given.
     
    //Start the buffer
    ob_start();
     
    //include
    include $file;
     
    //get contents from the buffer
    $ob_contents = ob_get_contents();
     
    //and kill/delete the buffer
    ob_end_clean();
     
    //return it to MODx
    return $ob_contents;
    


    and then call the header by pasting this code in to the header chunk
    [[includeFile? &file=`assets/templates/main/site_header.html`]]




    both are not working.

    my files are located at:

    assets/templates/main/site_header.html
    assets/templates/main/site_footer.html
    assets/templates/main/site_template.html

    site_template.html looks like this:
    [[$bpSiteHeader]]
    [[*content]]
    [[$bpSiteFooter]]




    any ideas . what am i doing wrong ?
      • 3749
      • 24,544 Posts
      I think you're having a problem with back-ticks and single quotes (assuming that the paths are correct).

      In a snippet tag, all property values need to be enclosed in back-ticks (not single quotes).

      [[includeTemplate? &tpl='assets/templates/main/site_header.html']]


      In the snippet code, however, you want single quotes, not back-ticks as a delimiter:

      $tpl = $base_path . 'assets/templates/'. $tpl;
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 42852
        • 2 Posts
        wow... thats great.
        It worked.
        It was the backticks. I've never seen them used before.

        THANKS

          • 3749
          • 24,544 Posts
          The backticks allow you to have either single or double quotes in the property value.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
          • As well as spaces. Have to delimit by something; 'way back when it was by whitespace, so backtick delimiters were added. Actually, bacticks have been used in sql for a long time; they may have been in the original language specification as far as I know.
              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