Smarty Relative include path - php

File structure is as follows:
index.php
settings/
|-manage_account.php
templates/viriditio-v2/
|-index.tpl
templates/virditio-v2/css
|-style.css
localhost/~braden/virditio/index.php shows the template like expected showing index.tpl with the style sheet paths correctly showing:
<link rel="stylesheet" href="templates/virditio-v2/css/style.css" type="text/css"/>
However localhost/~braden/virditio/settings/manage_account.php shows the same path, which is the relative path (should be ../ to be complete).
What's an easy way to make it relative to the template? Or absolute to the root? Is there a Smarty function that includes css files and makes them not relative?
Currently I have it set like this:
config:
template_url = "templates/virditio-v2/"
and .tpl:
<link rel="stylesheet" href="{#template_url#}css/reset.css" type="text/css"/>
EDIT
It's not pretty but I was able to accomplish it with
{assign var='config_url' value=#template_url#}
{assign var='template_url' value=http://`$smarty.server.SERVER_NAME`$config_url}
Any better solutions out there?

Why not access the CSS like
/templates/virditio-v2/css/style.css
with an absolute path?
If its about different hosting environments in subdirectorys, consider a config option to set the base directory and append it as a vairable to the path in your smarty template.

If I understood correctly the issue is to have a CSS file linked to a webpage no matter where in the folder tree the page comes from. This might be written as:
<link rel="stylesheet" href="/~braden/virditio/templates/virditio-v2/css/style.css" type="text/css"/>
This would make it a fixed path starting from the root of the server.

Related

File don't get attached when I use absolute path in HTML CSS

I was using PHP include function to include the header and footer but now I want to change location of my files so I created a folder named admin panel and created a PHP file in it. Now I want to connect my header.php file using include but all the path in header file are give in reference so I want to change it to absolute path but when I change it then it doesn't work.
The reference file path is given like this:
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all">
Now when I change it to absolute then it dont work
<link href="/home/username/public_html/css/bootstrap.css" rel="stylesheet" type="text/css" media="all">
I am having GoDaddy hosting so this is the way to add absolute path in GoDaddy they say. This is GoDaddy's answer link:
https://in.godaddy.com/help/what-is-my-absolute-path-16023
If I am doing something wrong then please tell me thanks in advance.
If you use a forward slash at the beginning of your path, the path will start in your root folder.
Like this:
<link href="/css/bootstrap.css" rel="stylesheet" type="text/css" media="all">
This path should work in any file in any directory.
Best way for you is to get the absolute path through php, so you have no doubt. And to get that just use this
<?php
$path = getcwd();
echo "The absolute Path is ".$path;
?>
Suppose you site is www.example.com and you are giving the absolute path like /home/username/public_html/css/bootstrap.css it will search at http://www.example.com/home/username/public_html/css/bootstrap.css but if you give like /css/bootstrap.css it will search at http://www.example.com/css/bootstrap.css. if there is a forward slash at the beginning.

Including external stylesheets in a PHP file

<link href="/app/app.css" rel="stylesheet" type="text/css">
The above is the code I'm using to access a stylesheet, but it's not working at the .
Some extra details:
This is in a .php file, but it's located within the head of an html section
I'm working on a temporary url (i.e. 'my.ipa.dd.res/mydomain.com/dir/'). This might be the reason it's not working.
Edit:
It's a stylesheet I'd like to use on several pages, which is why I'm trying to point to a root directory (so that I don't need the file in every single folder I create).
Well I think you need to store your root directory path as a string to include your css file with an absolute URL.
Something like :
<link href="{$absoluteRootPath}/css/styles.css" rel="stylesheet" type="text/css">
If you remove the leading slash it will look for the css file in the folder relative to the current.
<link href="app/app.css" rel="stylesheet" type="text/css">
Edit: to reuse in multiple scripts in different dirs you would need to specify an absolute path, so to avoid having to change it in multiple places when you go live (ie stop using the temporary url) you should define a variable.
<?php
// set absolute path to the stylesheet
$cssPath = 'http://my.ipa.dd.res/mydomain.com/dir/';
?>
And
<link href="<?php echo $cssPath; ?>app/app.css" rel="stylesheet" type="text/css">
Depending on your php architecture you may need to define $cssPath as a global or you may be able to add it to an existing config variable. This is completely dependent on your framework.
I've been having this same problem recently. Here's what worked for me
Now on the index page, replace link rel="/app/app.css" href="stylesheet" type="text/css"
with
?php include 'style.php' ?
And on the style.php page insert the following tags:
style and /style
You can put the regular CSS between those two tags. See if that works. BTW for some reason I can't insert the greater or less than symbols without making the code disappear... Forgive me, I'm new here..

PHP Website - Loading Stylesheets in Sub Directories

I'm building a PHP based site with this directory structure
index.php
css
style.css
bootstrap.css
includes
header.php
footer.php
bikes
road.php
mountain.php
The Problem
So I'm working on road.php and I obviously need to be able to link to both style.css and bootstrap.css, but when I declare at the start of road.php to include the header.php and footer.php it is like as if it cannot find the stylesheets and the site reverts back to the default 1990s look.
I have also found that any form of link on the page loads a 404. I'm only just starting out with PHP because I need some more power in my sites, but I just can't seem to get my head around the super basic things.
I just don't know what to do and I'm finding myself turning my back on the whole PHP language.
Thanking you in advance,
Stu :)
I can't be certain without seeing the actual content of header.php (in perticular the part where you import the stylesheets), but it sounds like you are using a relative path to your stylesheets. Something like <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />. This works fine for index.php, but since the other pages are inside the subfolder bikes, they will be looking for the CSS files in yoursite.com/bikes/css.
The solution is to provide an absolute path. Something like this:
<link rel="stylesheet" type="text/css" href="http://yoursite.com/css/style.css" media="screen" />
This way, it doesn't matter if the page is inside a subfolder (or a subfolder of a subfolder) - it will allways look for the CSS file in the right location.
If you are using multiple domain names, or for some other reason you cannot hardcode the domain name, you can prepend a slash (/) to the path as well:
<link rel="stylesheet" type="text/css" href="/css/style.css" media="screen" />
This path is relative to the root of the website, not to the current directory.

Calling a file with PHP keeping local URLs of called file

i have two files:
1.- root/folder/folder/themes/themeindex.php
and
2.- root/index.php
I want to include themeindex.php in index.php so when you enter to root directory, it will load the theme without taking you to (or showing you) the root/folder/folder/themes/ path.
I'm struggling to find or figure out a way to include the themeindex.php file but keeping the URLs local to its themes folder.
Meaning my
<link rel="stylesheet" type="text/css" href="style.css" />
will remain as that and I won't have to turn it into:
<link rel="stylesheet" type="text/css" href="/folder/folder/themes/style.css" />
I hope this all makes sense.
EDIT:
Hopefuly this explanation of my reasons helps a bit more...
1.- I want the final developer to be able to create themes as intuitively
as possible. So, the URLs remain as simple and intuitive as possible.
2.- I need to include the active theme into the root
directory, so it autoloads when the root is opened.
So if you combne, my reason number one, with my reason number two, then you
might understand how important it is for URLs to remain local and easy to
understand.
Instead of style.css you could request style.php, a file you define that uses imports root/folder/folder/themes/style.css and echoes it outright.
you can give a base tag in your code and point it to: the folder where you have the css files.
http://www.w3schools.com/tags/tag_base.asp
In root .htaccess file, add next lines:
RewriteEngine On
RewriteRule ^style.css$ /folder/folder/themes/style.css [L]
To include the theme file is simple:
include('root/folder/folder/themes/themeindex.php');
The other issue is specific to HTML. HTML needs to have the web path (not the absolute path) to the file so it can load it and use it.
The best you can probably do is something like:
<link
rel="stylesheet"
type="text/css"
href="<?php echo $theme_dir; ?>style.css"
/>
but that will still expose your theme directory. Why are you trying to hide that directory?
Response to comment:
If you take the code I have above and let your developer know how to structure their own content directory. All you would need to do it keep track of something like $active_theme_root and echo that out to load their own customized theme. As far as I know, you cannot set an HTML include directory, so HTML will need to know the web path to all needed assets
Best solution may be to have them use a global stylesheets folder for all themes ("root/stylesheets"). They would then link to this global folder in a relative manor:
<link rel="stylesheet" type="text/css" href="stylesheets/mytheme/style.css" />

Include and path problem

<?php
// This is index.php
ob_start();
include 'tem/u.html';
ob_end_flush();
?>
<html>
<!-- This is u.html -->
<link rel="stylesheet" href="style.css" media="screen" />
<body>
<p> abc </p>
</body>
</html>
Now my problem is when i run h.html -> Ok with style.
But when i run index.php -> Ok without style (because now the index.php include style.css, not tem/style.css)
Need a fix
If possible, refer to a domain relative path to the style.css, for example
<link rel="stylesheet" href="/style.css" media="screen" />
If that is not possible, you need to keep track on the page base in some way, which I cannot tell because I do not know enough about your application. But anyway, like
<link rel="stylesheet" href="<?php echo $pageBase; ?>/style.css" media="screen" />
where $pageBase is a variable containing the url to the root of your application.
I'm assuming that the tem directory is supposed to be for some sort of template, and so you don't want it to be directly exposed to the user; rather, you want to be able to include the files so that they're accessible via index.php, possibly with the option of later changing what files are included.
You could create another PHP file called style.php (in the root directory) which would include tem/style.css. You could do this for any other files that your templates used as well — the idea being that each PHP file in the root directory would correspond to a "role" in the template, not a particular template file, so that the template could later be changed without everything needing to be rewritten.
This might get a bit cumbersome if you had a lot of files required by your template, so it might be better to have a single script that could be instructed which file to load (through a $_GET variable). But in that case, you need to be very careful not to allow the user to specify arbitrary files. I'd suggest avoiding this approach until you're more proficient in PHP.
EDIT: On second thought, I'd suggest using a <base> tag in your template HTML file, as suggested in my comment on #gnud's answer.
This has nothing to do with PHP or include. This has to do with your browser, and how URLs are interpreted.
When your browser is pointed at http://xyz.abc/tem/h.html and asked to load "style.css", it tries to load http://xyz.abc/tem/style.css - this is known as a relative url, relative to the current document location.
When your browser is at http://xyz.abc/index.php and is asked to load the stylesheet in the same way, it tries http://xyz.abc/style.css. Maybe you see the problem?
As for a solution, you might use a domain-relative path for the stylesheet ("/tem/style.css").
just always use absolute path to your css file
<link rel="stylesheet" href="/tem/style.css" media="screen" />
that's all

Categories