Summary: How to refer to files using relative URLs
Version: 1
Prerequisites:
Status:
Maintainer: Stefano
Questions answered by this recipe
- How can I avoid hardcoding links to images saved locally?
- The images that are uploaded are saved in a directory named after the group's name - how can I refer to those files, without having to hardcode on the page where I show them the whole directory path?
- I think that soon or later I will change the name of my site - is there a way to avoid having hardcoded links in my pages?
Answer - Overview
- First of all you have to edit your config.php file and define a variable which you will use later to refer to the path.
- Second, on the page you edit, you mention the variable followed by the filename.
Answer - Detailed procedure
Edit your .../local/config.php file and enter the following line:
$FmtPV['<variable name>'] = "'<fixed part of the path>' . '{' . '\$Group' . '}/'";
After saving, you'll be able to get the path just by calling the variable you defined on your page with
{$<variable name>}
Example
If somebody who belongs to the group dummygroup uploads the file image.jpg to the host www.myhost.org, and pmwiki saves automatically the images to a directory called after the group's name (default behaviour) under the path /pmwiki/uploads, it will be enough to specify the following in config.php:
$FmtPV['$Imagedir'] = "'http://www.myhost.org/pmwiki/uploads/' . '{' . '\$Group' . '}/'";
This way, on the page that that person edits, s/he will have to specify only
{$Imagedir}image.jpg
in order to show the image on the page. Alternatively, if only the link has to be displayed, the whole can be enclosed as usual in double square brakets like this:
[[{$Imagedir}image.jpg]]
which will result in
http://www.myhost.org/pmwiki/uploads/dummygroup/image.jpg(approve links)
Explanation
The string
$FmtPV['$Imagedir'] = "'http://www.myhost.org/pmwiki/uploads/' . '{' . '\$Group' . '}/'";
defines a variable called $Imagedir which will contain the path
http://www.myhost.org/pmwiki/uploads/{$Group}/
which is then translated with the real group name when it is loaded on the page using the variable
as
http://www.myhost.org/pmwiki/uploads/dummygroup/
Notes
Release Notes
If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".
Comments
See Also
Contributors