...

cfincludes

wyclef
11-07-2009, 04:38 PM
hey, i'm stuck having to use coldfusion on a project. never touched it before. how to i set the cfinclude path so i don't have to add the relative path to the included file from page to page .. ../ ../../... in php u can just set a variable like mysiteroot and just add it to the beginning of the path... however in this case i don't know what the site root will end up being. also, is it fine just just add the .cfm extension to my page and use the cfinclude thing or do i need to add other cfml tags to make it work.

Gjslick
11-08-2009, 06:55 AM
Hey,

#1, ColdFusion is awesome :thumbsup: Much easier / straightforward than php (in my humble opinion :p).

#2, by using the path '/' in a cfinclude, this should take you directly to the web root of the current web site. That is, there is no need to set up any variables or anything. So whatever your site root turns out to be, that is what '/' will turn out to be.

Examples:

<cfinclude template="/someDocInWebRoot.cfm">

<cfinclude template="/folderInWebRoot/docInFolder.cfm">
#3, It's ok to just add the .cfm extension to any file. There is no cfml markup required. Just like the php engine searches for <?php and ?> tags, the ColdFusion engine will search for any cf tags for processing, and if there are none, will just return the text of the document as-is.

Hope that helps. Btw, if you have any other questions, post again in this thread. Coding Forums doesn't send out a notification email that there is a new thread until 1am each day, but now that I'm subscribed to the thread, I'll get an email right away and help you out.

-Greg

wyclef
11-08-2009, 03:38 PM
right, the problem here is that the site will be housed in some subdirectory... so if i put the includes folder in .../folder/subfolder/includes and i have no clue what folder and subfolder will be named putting a / in front of the path won't help. that's why i thought if i could set up a global variable for the site root it would be easy to change once the site goes live. also, how does one handle changing content within an include file such as a title tag. i'd like to include the entire header but the title tag changes from page to page.

Gjslick
11-08-2009, 07:27 PM
Ok, I see what you're trying to do. You have a few options.

1) You could create a mapping to your includes directory in the ColdFusion administrator. The administrator is where you change all server settings, just like what the php.ini file does, except it's a nice web based interface. The only thing is, you'll need to know the administrator password. To access the administrator, it's http://www.yourhosthere.com/CFIDE/administrator (Just obviously replace www.yourhosthere.com (http://www.yourhosthere.com) with your host).

Under "Server Settings" -> "Mappings", you'll be able to add a logical path, such as '/includes', and map it directly to a system directory, such as 'C:\inetpub\wwwroot\yoursite\someSubDir\includes' (or wherever your directory is).

Then, you'll be able to access this directory just like if it were in the web root, with <cfinclude template="/includes/includedFile.cfm"> ColdFusion will find the mapping, and find your file.


2) You can use the variable method, like you wanted to do originally. This is obviously as simple as just creating a variable for the path to your includes file.


<!--- Set a variable to point to the includes directory --->
<cfset includesDirectory = "/subDir/myCFsite/includes">

...

<!--- Include a file from the includes directory --->
<cfinclude template="#includesDirectory#/myInclude.cfm">
If you want to only set this variable once, and in one place (so it's easy to maintain), you can put it in an Application.cfm file. Application.cfm is a special file that gets automatically included at the beginning of every request to a cfm page. If you're working on a pre-existing app, there is probably one either in the directory that you are working in, or in a parent directory. ColdFusion looks up the directory tree until it finds one to include for a request.

So you could set the variable in that Application.cfm file, and it will be available to all pages where the Application.cfm file lives, and its sub directories. Then, when you want to change the includes path, you'd only have to change it in one place.

One thing though, your app might use the newer Application.cfc file instead of an Application.cfm file. That is the more object oriented approach (CFC's are ColdFusion's classes). If so, I'll walk you through setting a request variable in that, but most apps have Application.cfm files.

---------------------------

Ok, with your changing of the title tag in your header file, you're basically going to have to use a variable in the header code, and set that variable before you include the file.

yourPage.cfm

<cfset pageTitle = "My Title">
<cfinclude template="/includes/header.cfm">
header.cfm:

<html>
<head>
<cfif NOT isDefined( 'pageTitle' )>
<cfset pageTitle = "Default Title">
</cfif>

<title><cfoutput>#pageTitle#</cfoutput></title>
</head>

<body>
...
In the example, I provided a way to give a default title if the pageTitle variable is undefined when the header template is included. The cfoutput tags are just for outputting ColdFusion variables onto a page.

wyclef
11-08-2009, 07:41 PM
this is helpful thanks. so with the application.cfm or cfc file is that something I would have control of? the problem i'm having is that i don't have any real access to the servers. i just know they run CF and the IT guys aren't being very accomodating so i'll have to pass off my files to them when I'm done. I've downloaded and installed the developers edition on my mac so I can test stuff out but anything that will involve accessing administrative panels I'm not going to have access to.

Gjslick
11-08-2009, 08:35 PM
Ok, I see. Then I guess you'll have to use the global variable method of setting your includes path.

But yes, all you have to do is create a file named Application.cfm in your directory, and ColdFusion will automatically find it and include it at the beginning of every request to the cfm files in that directory. There is also another 'special' file that will automatically get included at the end of every request if needed, which you just name: OnRequestEnd.cfm

Not having server access is a bummer though. You might run into a problem in that they probably already have an Application.cfm file in the web root, with the code needed to define application and session management. Ideally, you would put the cfset for your global variable in that file.

The problem is, if the server finds an Application.cfm file in the directory of a requested template, it will stop looking up the directory tree for one, and therefore not include their Application.cfm file. However, I would just go with what you're doing, and then when you give them your files, just tell them that they'll have to copy whatever is in your Application.cfm file into theirs, and then remove your file.

Real quick though, you might want to ask them if they use an Application.cfm file or an Application.cfc file. If they use the cfc version, you might have to put your variable into the request scope, instead of the global (i.e. page variables) scope. It's kind of the same thing, but would just be declared as request.includesDirectory instead.

Ex:
<cfset request.includesDirectory = "/dir/dir2/includes">

<cfinclude template="#request.includesDirectory#/myInclude.cfm">
Greg

wyclef
11-09-2009, 07:41 PM
whats with this site? http://www.communitymx.com/content/article.cfm?cid=8316B
it says not to use the body or head tag in an included file?

Gjslick
11-09-2009, 08:25 PM
Hmm, I think he's talking about using an include for each of the "pieces" of his page. He's saying not to put header code in those includes, because he already has it all in his main page.cfm (in his attached files). I don't know how he was planning on implementing navigation, but it seems that he would duplicate his <html>, <head>, and <body> tags for each unique page, and just include the common pieces in (like his display header, site nav, and display footer).

Just a matter of design I guess. His design does seem to be more flexible though than the simple "header code include," in the fact that each page can have a unique title, and include whichever scripts and style sheets that they need. You just lose the maintenance capability of being able to quickly change the doctype for all of your pages at once, which really isn't that much of a big deal.

I personally use a popular framework called Fusebox to handle this kind of code sharing (which is actually both a ColdFusion and a PHP framework. www.fusebox.org), but I'm not sure you'd want to get involved with that at this point, as there is a bit of a learning curve (and you already have one for ColdFusion).

If that is the kind of site that you are developing though, where you'd have common pieces shared between all pages, then I'd personally go with what he is suggesting.

-Greg

wyclef
11-09-2009, 08:53 PM
i''ll check that out. i was also looking at mura. not really sure about frameworks at this point though. ok thats good about being able to include main tags in an include file... i'd rather lay mine out a bit differently where i have a couple big includes with variables in them.

Gjslick
11-09-2009, 10:16 PM
Yeah, whichever design works best for you. As long as all of the html formatting for the page comes together correctly in the end, the browser won't care.

Also, ColdFusion basically just ignores and doesn't process anything that is not a cf tag (just like php ignores anything that is not between <? and ?> tags), so it really doesn't matter which html tags or text that you put in each file. You can always do View -> Page Source in your browser when your page loads, to check that your final html output is coming together correctly.

-Greg

wyclef
11-09-2009, 10:58 PM
hey, if i set a variable is it fine to use it twice?

Gjslick
11-09-2009, 11:04 PM
Yeah, of course.

wyclef
11-24-2009, 04:37 PM
Hey Gjslick,

Do you know where someone can go to find freelance CF help? I'm working on a project with a developer who might fall through and am trying to figure out a backup.

Gjslick
11-24-2009, 05:13 PM
Hmm, nah, nothing that I know of personally. But a quick search of google seems to return quite a few results.

How big of a project is this btw?

wyclef
12-03-2009, 07:39 PM
hey, so is it fine to use about 8 cfincludes to make up a template?? or is that too many?

Gjslick
12-03-2009, 07:49 PM
Nah, you can use as many as you want. Just as long as you yourself can keep track of it all!

wyclef
12-04-2009, 11:02 PM
do you know anything about the bs dreamweaver cfm file bug where it inserts line breaks when u upload a file and NO i don't use dreamweaver and dont like the program at all so nobody give me a hard time about dreamweaver because i'm already having a hard enough time dealing with the stupid dreamweaver bs bug.

Gjslick
12-05-2009, 01:23 AM
#1) What exactly are you talking about? What exactly happens?
#2) What version of Dreamweaver are you using? I'm using Dreamweaver 8 (have been for years) and have never had a problem like that.
#3) Don't go posting on the forum all P.O'd in a rambling rant that doesn't make any sense. Calm down and explain.

wyclef
12-05-2009, 02:14 AM
http://dwmommy.com/?day=1/15/2006

Gjslick
12-05-2009, 05:11 PM
do you know anything about the bs dreamweaver cfm file bug where it inserts line breaks when u upload a file and NO i don't use dreamweaver and dont like the program at all so nobody give me a hard time about dreamweaver because i'm already having a hard enough time dealing with the stupid dreamweaver bs bug.

Ok that link in your previous post had nothing to do with "dreamweaver cfm file bug where it inserts line breaks when u upload a file"

But also, I never use the design view. I not only prefer to write the HTML myself, but it's also just a lot easier to integrate with the server side processing that ColdFusion (or any other language) does when you write it yourself.

wyclef
12-06-2009, 09:23 PM
yes... I KNOW all about dreamweaver design view crap. i have to pass the files along to someone who uses dreamweaver. basically there is an additional line break for every cfinclude.

<cfinclude template="#root#/includes/heading.cfm">

it seems like there is some issue with Dreamweaver... do i need to add a /> at the end of each include?

http://www.cfinternals.org/blog/2008/03/dreamweaver-cs3.html

sorry but i can't deal with dreamweaver.

wyclef
12-06-2009, 10:29 PM
i cant quite put my finger on what the problem is but it seems like i'm not the only one struggling with include files and dreamweaver... http://kb2.adobe.com/cps/165/tn_16511.html

Gjslick
12-06-2009, 10:56 PM
Yeah that's weird stuff. Perhaps it has to do with invalidly nested code though. Make sure all of your html is properly formatted, with matching end tags for tags that need them, and in the correct spots.

Come to think of it, I have in fact seen that issue once before, a really long time ago. A developer that I was working with was doing kind of a hack per se, where inside a loop he had a cfif to insert </tr><tr> tags in after so many iterations of the loop (to break things into new table rows after so many items had been outputted). This resulted in valid HTML code in the web browser when ColdFusion was done processing the page, but Dreamweaver would get confused with it and try to re-write some of the code to put in </td>'s and </tr>'s where it thought that they were supposed to go. Mad annoying, but I haven't seen that issue since.

So maybe this has to do with your issue. Are you using HTML tables? If you are, make sure that the table's tags are correctly nested. The 3 required tags (table, tr, td) should always be like this:

<table>
<tr>
<td>Data in row 1</td>
<td>More Data in row 1</td>
</tr>

<tr>
<td>Data in row 2</td>
<td>More Data in row 2</td>
</tr>
</table>
And don't ever have any other HTML code (forms, text, w/e) between the <table> and <tr> tags, or the <tr> and <td> tags, or even between </td> and <td> tags. Make sure all opening <tr>'s and <td>'s are closed at the proper location as well.

If you're not using tables, check for other unclosed start tags, like <li> tags, <option> tags, or even <div> tags and such. Your page may render correctly in the browser (because browsers are built with developer idiocy in mind), but that doesn't mean that you have correct/valid code.

----------------------------------------

On this same note, does this only happen with specific pages, or all of your pages? If you want, post your source code on a page it happens to, and I'll take a look at it for any issues that I can spot.

-Greg


P.S. No, you do not need to make the <cfinclude> tag self closing (with the />). ColdFusion doesn't care either way, but apparently Dreamweaver does, according to that link you posted earlier.

wyclef
12-06-2009, 11:00 PM
no tables. all the code validates perfectly. the only code that doesn't validate is some embed src to display a pdf from the issuu.com site but that code isn't in the include files. it seems like there are exactly the same number of line breaks as cfinclude tags and in the same location in the source. not sure if it matters but i also have some cfsets in there as well.

wyclef
12-07-2009, 06:00 PM
check this page out. that guy uses /> to close his cfinclude tag and runs into a similar issue. check out the first demo

http://www.bennadel.com/blog/250-CFInclude-Templates-Are-Parsed-Regardless-Of-File-Type.htm

Gjslick
12-07-2009, 06:17 PM
Nah that link has nothing to do with your issue. Ben is saying that by using a cfinclude tag, whatever document that is included is parsed for ColdFusion tags, regardless of the file extension. You could do: <cfinclude template="myPage.html"> and myPage.html would be treated as a .cfm page.

Php's include statement works the same way. Even if you do: include( 'myFile.html' ), myFile.html is treated as a php file, and is parsed for <?php and ?> tags.

Gjslick
12-07-2009, 11:33 PM
Ok, here's what's happening. When ColdFusion processes a page, it takes all of your CF tags, processes them, and then returns the resulting HTML page to the browser. So the resulting HTML that the browser gets, appears as though CF tags have been "removed."

The thing is, anything outside of a CF tag does not get removed. That includes the line breaks around your CF tags. Here's an example, and the reason why you have those extra line breaks in the source:


<cfset root = "/root_path/">(LINE BREAK)
<cfset title = "Title">(LINE BREAK)
<cfset bodyid = "home">(LINE BREAK)
(LINE BREAK)
<cfset block = "Some Text">(LINE BREAK)
(LINE BREAK)
<cfinclude template="#root#/includes/header.cfm">(LINE BREAK)
<cfinclude template="#root#/includes/nav.cfm">(LINE BREAK)
<cfinclude template="#root#/includes/subnav.cfm">(LINE BREAK)
All of those line breaks stay in the resulting document that gets generated.

There's two things you can do about it, if you so chose. One is the <cfsilent> tag. It suppresses all output between <cfsilent> and </cfsilent>. Ex:

<cfsilent>
<cfset root = "/root_path/">
<cfset title = "Title">
<cfset bodyid = "home">

<cfset block = "Some Text">
</cfsilent>(Line Break, can't do anything about it)

<!--- Don't want to wrap your includes in a <cfsilent>, as it would suppress their output --->
<cfinclude template="#root#/includes/header.cfm">(LINE BREAK)
<cfinclude template="#root#/includes/nav.cfm">(LINE BREAK)
<cfinclude template="#root#/includes/subnav.cfm">(LINE BREAK)
OR, what you could do, is use the <cfsetting> tag to only enable output for things that are inside a <cfoutput> tag. This is sometimes not as easy to work with though, and you will be pulling your hair out if you forget its there and are trying to output text to the page which is outside of a <cfoutput> tag. Ex:

<cfsetting enableCFOutputOnly="yes">

<!--- Yee haw, no line breaks in the generated output --->
<cfset root = "/root_path/">
<cfset title = "Title">
<cfset bodyid = "home">

<cfset block = "Some Text">

This text, although sitting directly on the page, will NOT be displayed in the page's output, because it is not within <cfoutput> tags.

<cfoutput>This text will be displayed in the page's output.</cfoutput> <!--- Don't even get a line break here --->


Note that if you do it that way, that each of your included templates will need the <cfsetting> tag as well, as it only applies to the template where it is used. And all of your html code would need to be within <cfoutput> tags. I actually do this anyway, putting <cfoutput> at the start of my html output, and </cfoutput> at the end. If I need to loop over a query, I use <cfloop query=""> instead of <cfoutput query="">.

Wish you just described your problem in the first place...

wyclef
12-07-2009, 11:58 PM
what about this guy who does both? http://www.bennadel.com/blog/369-CFSilent-vs-CFContent-Reset-True.htm

so if i do the second option i have to include that in every included file as well? i can't just put it in the doctype include file?

Gjslick
12-08-2009, 12:21 AM
Dude, honestly, you're putting way too much time and effort into this for a few extra lines in the source code that nobody will ever see, and which also your CF9 will automatically suppress. Tell them to upgrade if the have the $$.

But otherwise, just stick with the <cfsilent> tag instead of confusing yourself with the <cfcontent> tag, which btw I've never seen used like that, although it would in fact work to suppress those extra 3 line breaks that you might have... Honestly, I wouldn't have even wasted my time with 3 line breaks even if everyone in America were on 56k modems, let alone the high speed internet that most people have nowadays.

wyclef
12-08-2009, 12:39 AM
i've come this far... might as well go the full distance at this point. so what if you are using cfcontent in the html to output variables... can you nest cfcontent in cfcontent? i see what you are saying, using this <cfsetting enableCFOutputOnly="yes"> is a bit more involved but i think i almost get it... just can't get it to work. do i wrap the cfincludes with the cfcontent as well? wait, if i wrap cfincludes in cfoutput am i going to have to escape every single # sign that makes up a variable?

Gjslick
12-08-2009, 01:00 AM
Whoa whoa whoa, #1, don't use <cfcontent> at all. If you want to know what it actually does, then read about it in the CF livedocs, but otherwise Ben Nadel (the guru that he is) is just using it really as a hack to suppress every little wee-bit of whitespace. If you were to use it in all of your cfincludes, it would actually remove the outputted content of all includes before the one that was currently processing, and thus you would only be left with the outputted content of the last cfinclude.

I really recommend just using <cfsilent>, and just wrapping it around all of your "processing" code at the top of the page.

<cfsilent>

<!--- suppressed output code goes here (cfsets, queries, whatever). No line breaks will be kept in the output --->
<cfset myVar1 = "Test">
<cfset myVar2 = "Test 2">

</cfsilent>(Line break 1)
(Line break 2)
<!DOCTYPE ... >
<html>
blah blah blah rest of the page
That will only leave you with two linebreaks at the top of the page, unless you really wanted to be crazy and put the <!doctype> tag right next to your </cfsilent>, then that would suppress those two extra line breaks, but reduce your source code readability (which of course, is fail).

Otherwise, use the <cfsetting> tag with enableCFOutputOnly="yes" (or "true" for the attribute value, whatever you prefer).
Ex:

<cfsetting enableCFOutputOnly="yes">

Absolutely everything in the template is not added to the output buffer,
UNLESS that text is between a <cfoutput> and a </cfoutput> tag.
This text itself is not added to the output buffer, and will not be sent to the client's browser.

<cfoutput>This text WILL be added to the output buffer, and WILL be sent to the client's browser.</cfoutput>
I actually do just that on simple pages meant for returning JSON data to an AJAX call. Here's an example directly from production code on my site. Don't mind the code that you might not understand, it's just an example of using enableCFOutputOnly.

<cfsetting enableCFOutputOnly="yes">

<cfset user = application.UserManager.getUser( form.userID )>

<!--- Return JSON object to client. --->
<cfoutput>
{
userID: #user.getUserID()#,
firstName: "#jsStringFormat( user.getFirstName() )#",
lastName: "#jsStringFormat( user.getLastName() )#"
}
</cfoutput>
Only the text wrapped in the <cfoutput> tag will be returned to the client.

Hope that helps.

Greg

wyclef
12-08-2009, 01:25 AM
yea that helps. maybe i'll just live with a few lines. i got the cfoutput thing to work it's just that it wants me to escape every damn # sign and I use those for html entities throughout the content so it's a total pain in the arse and seems completely impractical so I guess I'll just stick with cfsilent. that will at least cut back on the half the breaks and just live with 2 breaks b4 doctype and hope they upgrade their servers to 9.1. thx for yer help man, this has been a crazy rollercoaster of whitespace madnesssssssss

Gjslick
12-08-2009, 01:43 AM
Lol, tell me about it... But yeah, that's much more practical with the <cfsilent> tags then, rather than having to escape all of your # signs.

Good luck buddy, and lemme know if you need anything else.

Greg



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum