Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
How to include an external php file?
VipulK
New
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?
0
Comments
"{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.
It's not just the solution, it's also the better idea to reduce fragility.
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
and saved it in
/themes/my-theme/SmartyPlugins/function.mainmenu.php
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}
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: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 confusingStart 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 intohome/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 fromSmartyPlugins
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.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 tohome/public_html/includes/menu.php
.But maybe it could not be included like that. Try the following
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: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.
Oops, sorry - my fault! Better reading questions would certainly lead to better answers as well...
Glad it worked out for you now.