Link to home
Start Free TrialLog in
Avatar of beregoth
beregoth

asked on

Relative Links in an include file

I am looking for the best way to handle this.  I have an include file in one directory that contains <a>nchor tags to link to other pages.  From that point in the directory structure, everything is working find..but if I reference the include file from a different page in a different directory, of course it does not work.  Is there a best practice regarding this and a way to accomplish this task so that the include file contains relative links to other pages based on where it is included?
Avatar of bigbadb
bigbadb

I think the best way to do is to create an application var that holds the root dir information. such as

application.root = "http://www.yourname.com/live/maindir"

This will allow you to create link files that start at the base directory so you can do

<cflocation url="#application.root#/dir/page.cfm">



Then you can always start from the root regardless of directory
Avatar of beregoth

ASKER

That sounds very reasonable...however, if I use a fully qualified url, like in your example, then I will not be able to test locally and that, for abvious reasons, is a must.  OOh, what about this...set a variable, like you suggest, at the top of every page...when the <cfinclude> file is read in, it will read in that variable to get the relative current position so it knows how many directories levels it needs to go up the chain to get to the correct link....this is basically what you were suggesting, only I won't use the url, I'll use a string like "../../", then when the include file is loaded, it will use a concatenated string to build the url.  One question (assuming you agree I am on the right track) though...will syntax like this work...

application.offset = "../../"


<a href="#application.offset#file.cfm">

?
if you set application.offset on each page this would work

however

it is much easier to have this stored in one file and change there.  Not on each page.

say for instance you were doing this in dev on server 1.1.1.1

you would change the application.root (only while in production) to

<cfset application.root = "http://1.1.1.1/live/maindir"


you will now have your root directory on dev

No need to have this var set on each page


does this make sense???
It makes sense, I just can't seem to get it to work.  In my first page to the site, index.cfm, I put the following code...

<cfapplication name="MikesResume">
<cfset application.root = "//localhost/resume">

(note that I tried the cfset without the cfapplication tag and it said I could not do that...this is new to me so bear with my ignorance).

Later, in the file I want to include I put the following in the <a>nchor tag..

<a href="#application.root#/Employment/otherfile.cfm">

but when I run the app and hover over the link I get some really strange syntax (which does not resolve the #application.root# variable) and when I click on it, nothing happens...literally..no error message no nothing.

So while I hope I am understanding your intention...I am wondering if I am still not implementing it correctly.
ASKER CERTIFIED SOLUTION
Avatar of bigbadb
bigbadb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the application.cfm file is located in your root dir
That did it!  Thanks so much for spending the time walking me through this...I am obviously a novice to many of the cool features in CF so I really appreciate the effort.