• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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?
  • 4
  • 3
1 Solution
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
beregothAuthor Commented:
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


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

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

<cfset application.root = ""

you will now have your root directory on dev

No need to have this var set on each page

does this make sense???
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

beregothAuthor Commented:
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.
you need to have an application.cfm file

this file is an independt file that contains all applicaiton var

in this you will set your application file

so you do the following in application.cfm

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

now in the href you would need the following

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

this will work

the application.cfm file is located in your root dir
beregothAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now