SoFunction
Updated on 2025-04-14

Coldfusion MX Skill Essence Collection 2 Page 1/6

Set your root directory

Author: Kyle 2/16/03

content:

Set your root directory
Website developers have always had to redirect the issue of relative path vs. absolute path. In the ColdFusion environment, you can use the CFINCLUDE volume tag on multiple different pages, but since we will use the CFINCLUDE syntax in which directory, using relative paths in the CFINCLUDE volume tag often causes a lot of trouble.

For example, a included menu may have a link to the homepage of the website, like the following:

<A HREF="">Home Page</A> 
There will be no problem introducing this included file in any file below the root directory of the website. However, if you introduce this menu file into an archive in a subdirectory, the above hyperlink will be invalid, or at least it will be linked to the wrong page. At this time, you will actually hope that the hyperlink is written like this:

<A HREF="../">Home Page</A> 
There are two ways to overcome this problem. One is to use the absolute path directly in the hyperlink. To use this method, you must preset a path mapping in ColdFusion. You can establish these correspondence relationships in the "Path Correspondences" block of the ColdFusion Administrator. Once you have established a path correspondence, you can write the hyperlink to the homepage of the website as follows:

<A HREF="/mymapping/>Home Page</A> 
If you use an absolute path, no matter where you introduce the file, the hyperlinks in the file will always remain valid. I usually use an application variable called “   to store the corresponding settings of this path, and apply this variable directly in the hyperlink. Using this method, if I really need to modify the name corresponding to the directory structure or path, then I only need to modify the value of such a variable.

<CFSET  = "/MyMapping"> 
If you apply this variable, your hyperlink will look like this:

<A HREF="##/"> 
Create such a variable at the beginning of each page, and you can apply the value of this variable in other parts of the page. The included file works just like the content of the file is actually written to the original page. Therefore, if file A introduces file B, then in file B we can directly refer to the variable values ​​in file A:

<!--- Set the RootDir variable value on each page --->
<CFSET RootDir ="../">
<!--- Refer to the value of the RootDir variable in the page introduced (just be sure that this variable is indeed defined, otherwise an error message will appear when introducing the file) --->
<A HREF="#RootDir#news/"></code>
123456Next pageRead the full text