Solved

Creating path variable for images and  Include files

Posted on 2004-04-02
1
194 Views
Last Modified: 2013-12-24


when i needed to create a variable to determine the path, i've did the following:

<!---create rootpath variable on each page-->
<cfset rootpath = "../">

then called it like :

<img src="<cfoutput>#rootPath#</cfoutput>images/spacer.gif" width="780" height="1" alt="" border="0" /><br />

On my sites, the header and foother is often placed in an include file and then called on pages througout the site.  So if i set the rootpath in the page that was calling the include file, it would replace #rootPath# with " ../".

This works, however, i wanted to know is there a more efficient way to do this.   Can anyone give some help.  

Thanks,
Y


0
Comment
Question by:techiebabe
1 Comment
 
LVL 5

Accepted Solution

by:
Seth_Bienek earned 200 total points
ID: 10743938

I use the statement below, because it allows me to use access the variable for templates in subdirectories as well:

<!--- Will set a string with the appropriate number of ellipses for a relative path reference. --->
<cfset request.relpath = repeatstring(".", listlen(cgi.script_name, "/")-2)>

The '-2' at the end indicates that the main folder for this app sits 2 folders deep from the web root.  This obviously can be left out if your application sits in your web root folder.

As far as efficiency goes, generally you trade efficiency for flexibility and robustness.   But either of these approaches (yours and mine) uses a negligible amount of server resources, so either is fine efficiency-wise.

Just as an added note, you may notice that I store the variable in the request scope.  This is so includes, custom tags, modules, etc. will have access to the variable as well.  It makes using custom tags which reference images much simpler to manage.

Take Care,

Seth
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to dynamically set the form action using jQuery.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question