Smarty: including a template file from the same directory - php

I have a Smarty template located in a directory under templates_dir: templates/some/dir/template.tpl . In the same directory, I have a sub-template: templates/some/dir/_component.tpl .
I can't include the sub-component using an unqualified include, since apparently it looks it up under the templates_dir:
{include file='_component.tpl'}
How can I tell Smarty to read the file from the same directory, as opposed to the templates root ? I do not want to specify absolute paths, since it will cause problems when changing directory structures.

Could your app pass the template dir to the template so it can use it to create an absolute path?
$smarty->assign('template_d­ir', $smarty->template_d­ir);

Related

How can I build a dynamic absolute path in PHP?

I've a web site made of some folders, one for each section (info, news, blog etc...).
In each of these folders there is an index.php file that should load a layout (common to all). These are stored in a different folder in the root where there is also the main index.php file (the homepage). So i have something like this:
root
-index.php (home)
-/layout
--layout files
-/info
--index.php`
The index file in the /info folder should include the page layout from /layout.
The problem is that the layout files should include other files from other folders.
In layout files I put this:
include 'contents/page-element.php';
But if I try to reach the same page-element.php file from the index.php file in the /info folder, I should do:
include '../contents/page-element.php';
to go to the root and then reach the /layout folder.
I don't want to create a copy of the layout for the folders so I've tried $_SERVER['DOCUMENT_ROOT'], but it does not work in some cases on the localhost and even on the web server.
Can someone help me or let me know how I can build a dynamic absolute path?
By using the built-in constant __DIR__ you can get your absolute path
by print it or save
echo __DIR__;
it will print something like this for you
C:\Users\user\Desktop\test
and there is also another constant that will get the absolute path for your file that executes the command __FILE__
echo __FILE__;
will give you a result like this
C:\Users\user\Desktop\test\index.php

How to define path for subfolder

I'm learning PHP and I'm having problems with the definition of the path.
If in the settings.php file located in the main folder I define a constant with the base path of the site to be able to use this constant in the subfolders I must include the file settngs.php precept by "../" as in the following way: ". ./settings.php "
is there any way to define the settings.php path that is also usable in the subfolder files without having to manually add "../" before the file name?
or a way to define constants usable in every file without including the file that contains the constants?
settings.php:
// FILE SETTINGS.PHP IN THE MAIN DIRECTORY
define('SITE_URL','http://localhost/');
admin/index.php:
// FILE INDEX.PHP IN THE ADMIN SUBDIRECTORY
include('../settings.php');
HOME
There is a way to have SITE_URL working without include('../settings.php') or there is a way to include "settings.php" in subdirectory without the "../" ??
Yes, you can use set_include_path function.
For example:
set_include_path(__DIR__.'/../');
Then include file from parent directory
include 'settings.php';

require_once files from another directory requiring within

This is a big confusing but I'll try to be as clear as possible.
I am using the Starter Site PHP Template in Web Matrix for an assessmnent, now when I move several files such as the files containing the database details, header, footer ect.. into the admin file I am having an issue with my index.php and page.php.
These files use require_once() to include several files within the admin folder. However in the admin folder the files being required have require_once tags again within that directory.
When I use require_once('admin/database.php'); the database file has require_once(somefile.php) within it. There are numerous files like this being included.
How can I use include these files using require_once in the root directory without getting errors like these.
warrning: require_once(/Includes/simplecms-config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\starter-ste\admin\Includes\connectDB.php on line 2
The includes folder is located within the /admin/ folder.
php require statements will look for files in the current directory and directories in the php_include_path. You can solve your problem by adding your include directories to include path.
You can do this dynamically by calling set_include_path
$directories = array('/library', 'var/', get_include_path());
set_include_path(implode(PATH_SEPARATOR, $directories));
[EDIT]
Your server might not know where the root directory of the website is. So, first, get the document's root directory and append it to the file name like this:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
require_once "$root/your_file_path_here";
Looks like you haven't enclosed the file name within quotes. So that
line should probably be like this:
require_once("/Includes/simplecms-config.php");

PHP Require In different folders

Okay so on my website I have a scripts folder which includes a php file which connects to mysql server so If I move a database then It will change it on all the files which are connect to the database.
I also have another folder called templates. In that folder there is a top for the header and the footer. In the header template I wrote:
require("../scripts/connect.php");
And I have another folder called, category. And that folder includes the header and the header includes connect. But then it displays and error that there is no such files.
Please help. Thank you
A good practice is to include a main config in all running php files, usually called config.php :)
in this config file create a constant called SITE_ROOT or something similar that point to the exact folder like this
define("SITE_ROOT", "/var/www/mysite");
Then on any include, include_once, require, require_once use it like this:
require(SITE_ROOT."/scripts/connect.php");
This should solve any relative path drama
You shouldn't use relative paths with the include/require, but use a constant defining the ROOT_PATH of your website.
Example:
In all the files calling needing includes:
define(ROOT_PATH, '../');
include ROOT_PATH . '/scripts/connect.php';
And in /scripts/connect.php (and all the other files that will be included somewhere), all the includes should use ROOT_PATH (without defining it).

PHP broken relative links in nested directories

I'm sure I'm missing some simple explanation, but I want to confirm - so assume I know very little.
I have a directory structure like so (for the time being) of:
My main site (localhost/project/ on my testing server, and C:/xampp/htdocs/project on my HDD) with these files and folders:
Root
graphics
variousgraphics.png
support
stylesheet.css
templates
header.php
footer.php
initialize.php
you
default.php
index.php
anotherfile.php
Up until I created the folder 'you' everything was fine, i.e. I included the initialize file for index.php as <?php include(templates/initialize.php) ?>
But when I decide to include initialize.php using the above method for the default.php file (inside 'you'), it errored out with Warning: include(templates/initialize.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\photoquilt\you\default.php
So naturally I appended ../ to create <?php include(../templates/initialize.php) ?> but then of course that didn't work because the files referenced inside initialize.php weren't appended in the same way, and so I get to here.
It's worth noting for me, an echo of $_SERVER['document_root'] leads to C:/xampp/htdocs
So in summary:
Is there any way to make sure all the link/paths work correctly irrespective of where the originating path was from?
In default.php you can define a constant like
define('ROOT_PATH', dirname(__DIR__));
or for php versions prior to 5.3.0
define('ROOT_PATH', dirname(dirname(__FILE__)));
and then use ROOT_PATH in all scripts to build the the file paths.
see
- http://docs.php.net/language.constants.predefined
- http://docs.php.net/dirname
There are a couple problems here as far as I can tell: the server-sided and the client-sided.
As for the PHP goes, you are doing it fine. Referencing the file by its relative path (../templates/initialize.php) is the way to go. There's another way of achieving the same, though I wouldn't recommend it: editing the include_path to add the root directory of your project. You can do it in an .htaccess located in the root directory, ie:
php_value include_path ".:/path/to/your/project:/usr/local/lib/php"
For the HTML part (images not loading, stylesheets not found), you can set a base href:
<base href="http://path.to.your/in-server/" />
The base href should point the root of your directory. All the images, stylesheets, etc in HTML must then be fixed to use relative URIs from the root of the project (graphics/variousgraphics.png).

Categories