Users running a non-download version of Vanilla (pulled from github), on branch release/2019.016 or master from the last 2 weeks should upgrade to release/2019.017 or latest master for security reasons. Downloaded official open sources releases are not affected.
Please upgrade here. These earlier versions are no longer being updated and have security issues.

How to include an external php file?

I'm trying to include the top main navigation of my website above the forum to keep the user experience consistent. But for some reason the file doesnot appear at all. Its something related to smarty I think. This is what I've tried so far:

{include file='../../../../includes/menu.php'}
and
{include_php file='../../../../includes/menu.php'}

in themes/THEME-NAME/views/default.master.tpl

To give you a sense of my directory structure:

/
/includes/
  | menu.php
/forums/                          *vanilla installation in forums*
  | cache/
    | Smarty/
      | compile/
        | vanilla^%%D3^D31^D31D02CD%%default.master.tpl
  | themes/
    | THEME_NAME/
      | views/
        | default.master.tpl
/index.php                       *my site home page*

The compiled file ( vanilla^%%D3^D31^D31D02CD%%default.master.tpl) turns this code into

<?php $_smarty_tpl_vars = $this->_tpl_vars;
$this->_smarty_include(array('smarty_include_tpl_file' => '../../../../includes/menu.php', 'smarty_include_vars' => array()));
$this->_tpl_vars = $_smarty_tpl_vars;
unset($_smarty_tpl_vars);
 ?>

The path to the file is still correct but the file still doesn't get included. How do I do it?

Tagged:

Comments

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    "{include_php} is deprecated from Smarty"

    You would have to create a custom smarty function in your theme. Look at this discussion: https://vanillaforums.org/discussion/27767/how-to-create-custom-smarty-functions

    Afterwwards you can use that custom function in your default.master.tpl

    But if you already have a custom theme you could use a themehooks file and use the Vanilla way of inserting things. I would recommend to download the eventi plugin and find out which event you need to hook into to insert your file.


    Linc
  • LincLinc Director of Development Detroit Vanilla Staff

    @R_J said:
    You would have to create a custom smarty function in your theme.

    It's not just the solution, it's also the better idea to reduce fragility. :chuffed:

  • Okay. Here's what I've done
    1. In my theme's default.master.tpl (/themes/my-theme/views/default.master.tpl) I added
    {mainmenu}
    2. I created a new file

    <?php
    /*
     * Smarty plugin
     * -------------------------------------------------------------
     * File:     function.mainmenu.php
     * Type:     function
     * Name:     mainmenu
     * Purpose:  loads default menu of the website
     * -------------------------------------------------------------
     */
    
    function smarty_function_mainmenu($params, &$smarty)
    {
        include_once('../../../../includes/menu.php');
    }
    
    ?>
    

    and saved it in /themes/my-theme/SmartyPlugins/function.mainmenu.php

    1. Reloaded the page and got an error Something has gone wrong.

    3.a. Moved the file to /library/vendors/SmartyPlugins/. The page loaded but main menu didn't come up. In the html there's a blank line in place of {mainmenu}

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    You would have to tell the smarty class that you have another folder to look for your custom function. Create a file /themes/my-theme/class.my-themethemehooks.php with the following content:

    <?php
    
    class My-ThemeThemeHooks implements Gdn_IPlugin {
        public function gdn_smarty_init_handler($sender) {
            // add directory "/themes/my-theme/SmartyPlugins/"
            $sender->plugins_dir[] = dirname(__FILE__).DS.'SmartyPlugins';
        }
    }
    

    But beware: you would have to exchange every "my-theme" in the above code with the name of your theme. It should better not have a dash since as far as I know that is not allowed in a class name.


    Now you can save your function into the /themes/my-theme/SmartyPlugins folder.

    In order to test if it works I wouldn't use the code you started with. The number of ../ could be a bit confusing ;)
    Start with a simple echo '<h1>HEUREKA!</h1>;. If you see that, you can include the file. But still instead of using that much dots, I would suggest you use a constant Vanilla defines: include PATH_ROOT.'../includes/menu.php';. That would make it a bit more obvious what's happening.


  • Thank you R_J, you've been most helpful. I'm getting the echo on the page but not the include file.

    I might be wrong but PATH_ROOT.'../... would not work because it'll turn it into home/public_html/forums/../includes/menu.php which is not valid. But 4 times ../ is not working either even though it is the correct relative path from SmartyPlugins directory.

    I tried include_once('menu.php') and copied the menu.php file in SmartyPlugins and it works so it's probably due to invalid path. Is it possible to get an error message? I've enabled debug mode from config but there was no change.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator
    edited November 2016

    It was just a guess since I don't know where your includes folder is. If you tell me the absolute path, I could give better help - the better the question, the better the answer ;)

    home/public_html/forums/../includes/menu.php is a valid path and it would resolve to home/public_html/includes/menu.php.

    But maybe it could not be included like that. Try the following

    $filename = realpath(PATH_ROOT.'/../includes/menu.php');
    if (!file_exists($filename)) {
        echo '<h1>"', htmlspecialchars($filename), '" does not exist!</h1>' ;
        return;
    }
    include $filename;
    

    This way either everything works fine or you see which file have been tried to be included.

    If you still get a blank screen, I would say you have an error in your menu.php
    Try to exclude this as a possible reason for the error by importing something really easy. Create a menutest.php in the folder where the menu is with something like this:

    <?php
    
    echo "<h1>I'm menutest.php!</h1>";
    


    VipulK
  • It worked!!

    There were two changed that were needed to make it work. Instead of using
    include PATH_ROOT.'../includes/menu.php';
    I needed
    include realpath(PATH_ROOT.'/../includes/menu.php');

    Thanks a lot.

    BTW..I did share a crude folder structure in the first post. And I also copied menu.php to the same location as function file and it was working then as well.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Oops, sorry - my fault! Better reading questions would certainly lead to better answers as well... :blush:

    Glad it worked out for you now.


    VipulK
Sign In or Register to comment.