For anyone that's interested, this is what I came up with.  This might be
handy if you want to use CFINCLUDEs using absolute paths instead of
relative paths.  It gives the application more directory path
independence, allowing you to relocate the directory that the application
resides in without changing any code.

Example:

<cfinclude template="#PathTo('/include/header.cfm')#">

<cfscript>
/*
*  Return a relative path from the current page to an
*  absolute URL on the same site.
*/
function PathTo(t) {
  var fa = ListToArray(cgi.script_name, '/\');
  var ta = ListToArray(t, '/\');
  var flen = ArrayLen(fa);
  var tlen = ArrayLen(ta);
  var rpath = '';
  var i = 0;
  var j = 0;

  // Find common branch point in paths
  for (i=1; i lte Min(flen, tlen); i=i+1)
    if (fa[i] is not ta[i]) break;

  // Path back to branch
  rpath = RepeatString('../', flen - i);

  // Append remainder of path to target
  for (j=i; j lte tlen; j=j+1)
    rpath = rpath & IIf(j neq i, DE('/'), DE('')) & ta[j];

  return rpath;
}
</cfscript>

----- Original Message -----
From: "Jim McAtee" <[EMAIL PROTECTED]>
To: "CF-Talk" <[EMAIL PROTECTED]>
Sent: Monday, September 13, 2004 1:17 PM
Subject: Turn relative links to absolute links

> I'm in the process of restructuring a fairly complex Intranet site -
> moving applications, directories, content, and menus around to give the
> site a bit more of a logical organization.
>
> The CF files and tags which use relative addressing are causing the most
> work, especially if I decide re-reorganize and move something for a
> second
> time.
>
> Simple stuff like:
>
> <cfinclude template="../../include/header.cfm">
>
> I believe I've seen techniques (or UDFs?) posted on how to reform the
> tag
> using an absolute address.  Assuming that the 'include' directory is
> hanging off of the site root and that I have a template in an arbitrary
> directory, as show below, how would this be done?
>
> /
> ../include/header.cfm
> ../applications/myapp/index.cfm
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to