Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFINCLUDE vs CFMODULE

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
US
This may be a very fundamental question, but what is the difference b/w CFINCLUDE and CFMODULE??

I'm asking because I'm using CFINCLUDE, and the results are not happening the way they should. See, I wrotet his custom script called "BadWordsScript" and I want to call that on all my export files, however, when I use CFINCLUDE it doesn't work. But if I copy and paste the entire code onto the files themselves, it works great. I know that session vars can't be accessed when using CFINLCUDE, so I was wondering if there is something else that can't be accessed when using CFINCLUDE.

Thanks.

[sub]
____________________________________
Just Imagine.
[sub]
 
One thing... You can have a cfinclude/cfmodule/custom tag in a cfoutput tag but it won't make a difference, you have to have the cfoutputs in the included file.

CFINCLUDE is cold fusion's first crack at functions I'd say but you can include any text so it works good for headers to (application.cfm works better for headers, but I didn't need someone pointing that out and distracting from the topic)..

<CFMODULE...> works the same as <CF_...> They're moth access to custom tags.. <CF_...> is the slick way to access it, it looks great, but its for files in the same directory or in the custom tags directory of cold fusion.

<CF_file> will call file.cfm but <CF_directory/file> will not, it will get an error.

People on shared hosting often won't get the host to put custom tags in cf's custom tags folder... Or in many cases don't want to... I don't want certain parts of my work to be open access.

That's where <CFMODULE> comes in.. With this, you can call files in different directories using relative paths..

<CFMODULE template="directory/file.cfm">

<CFMODULE template="../../otherfolder/myfilehere.cfm">

As with <CF_...> you can pass parameters...

<CFMODULE template="../getfirstname.cfm" pername="John Doe">

(I'll detail how to write that tag in a minute.)

Also with CFMODULE you can say:

<CFMODULE template="directory.subdir.file">

But that's hard to describe here, consult with livedocs:


The biggest difference with CFINCLUDE and CFMODULE is that to call local variables from an include you just call the variables (#variable#). To call with cfmodule or (<cf_...>), you call via the caller scope (#caller.variable#).

When a custom tag passes a value through a parameter as with the example:

<CFMODULE template="../getfirstname.cfm" pername="John Doe">

You call pername via the attributes scope (#attributes.pername#)

I promised code for getfirstname.cfm... This looks simple, I know, its an example.

Code:
[b]getfirstname.cfm[/b]
----------------
<cfparam name="attributes.pername" default="">

<cfif len(trim(attributes.pername))>
  <cfoutput>#trim(attributes.pername)#</cfoutput>
<cfelse>
  No name was passed.
</cfif>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
I know that session vars can't be accessed when using CFINLCUDE
of course they can

CFINCLUDE just copies a block of code into your page

it's exactly the same as if you had copied the code into the page yourself, except that when you use CFINCLUDE, there's only one line in every page, rather than the same block of code in every page (maintenance nightmare)

rudy
SQL Consulting
 
And though it defeats the purpose of custom tags, cfincludes can mock custom tags.. In my custom tag example above, I could invoke it with..

Code:
<cfset pername="John Doe">
<cfinclude template="getfirstname.cfm">

And the code for getfirstname.cfm in this method would be:

Code:
[b]getfirstname.cfm
----------------[/b]

<cfparam name="pername" default="">

<cfif len(trim(pername))>
  <cfoutput>#trim(pername)#</cfoutput>
<cfelse>
  No name was passed.
</cfif>

Custom tags also allow for opening and closing tags.. like in one case, I fake a windows alert box, so I say..

Code:
<cf_exclamation title="This thing happened">
  Hey, Look, Something happend!
</cf_exclamation>

In the closing tag, cold fusion calls the tag again.. but I control what it displays via variables available in the ThisTag Scope.


ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Thanks for all the replies. I have tried both <CFINCLUDE> and <CFMODULE> and the results are the same. For some reason when calling the badwords file using any one of those two functions it does not work properlly. YET, when I actually paste the code into the file itself, it works.

I was thinking maybe it might have something to do with the file using [colorm red]<cfoutput query="QUERYNAME" group="User_ID">[/color]. Can that intefere with the <CFINCLUDE> or <CFMODULE>?

[sub]
____________________________________
Just Imagine.
[sub]
 
It can interfere in cfmodule, not with cfinclude...

Cfmodule would refer to it as <cfoutput query="caller.QUERYNAME" group="...">

I'd say its a code issue, I can't imagine what except that something needs to be cfoutput'd and is not.. That might cause it.

"[red]It doesn't work[/red]" Does that mean that it errors, or it just doesn't affect anything?


ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Hey, "it doesn't work" means that out of ten test answers, only one gets flagged. But there are six answers that should get flagged for badwords. However, when I copy/paste the code into the individual files themselves, it goes good, all six answers get flagged.

Thanks.

[sub]
____________________________________
Just Imagine.
[sub]
 
The one that gets flagged, is it by any chance the first or last one in the list?

It might be time to share some code. I think sharing the file you're trying to include and an excerpt (at least) from the document you're trying to use cfinclude in.

It really sounds like its going through once when it should go through all ten times.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Hi, the word that gets flagged is in middle of the list.

This is the code(s):
Code:
<cfparam name="FormId" default="104">
<cfinclude template="includes/_functions.cfm">
	
<cfset delimiter = chr(9)>
<cfset FileName="test_#DateFormat(now(),'MMDDYYYY')#.txt">
<cfset FullPathName = "#getdirectoryfrompath(gettemplatepath())#files\#FileName#">
  
<cfquery name="getUser" datasource="#default_ds#">
SELECT	DISTINCT U.User_ID, U.FirstName, U.LastName, FA.FormAnswer, FA.FormField_ID, FA.CreateDate AS timestamp
FROM	FAns FA INNER JOIN
        FrmFlds FF ON FA.FormField_ID = FF.FormField_ID INNER JOIN
        Users U ON FA.User_ID = U.User_ID
WHERE   FA.Form_ID = #FormId#) AND FA.Exported = 0
ORDER BY U.User_ID, FA.FormField_ID
</cfquery>

<!--- create text file --->
<cffile action="write" file="#FullPathName#" output="FirstName#delimiter#LastName#delimiter#FieldOne#delimiter#FieldTwo" addnewline="Yes">

<cfoutput query="getUser" group="User_ID">
  <cfmodule template="definevars.cfm" vars="vFieldOne,vtheFieldTwo">

	<cfoutput>
		<cfif FormField_ID is 1340>
		  <cfset vFieldOne = trim(FormAnswer)>
		<cfelseif FormField_ID is 1341>
		  <cfset vtheFieldTwo = Replace(FormAnswer,chr(13)&chr(10),"|","ALL")>
		</cfif>	
	</cfoutput>
	
	[COLOR=blue][b]<cfinclude template="badWordsScript.cfm">[/b][/color]
	
	<cfif #badWordsstatus# EQ 0 and len(vtheFieldTwo) gt 0>
      <cffile action="append" file="#FullPathName#" output="#FirstName##delimiter##LastName##delimiter##vFieldOne##delimiter##vtheFieldTwo#" addnewline="Yes">
	  <cfset StatusDesc = "Done">	  
    <cfelse>
      <cfset StatusDesc = "Failed">
    </cfif>	
</cfoutput>

<cfmail from="email@isp.com" to="email@isp.com" subject="test">
<cfsilent>
  <cfmailparam file="#FullPathName#">
</cfsilent>
</cfmail>

Code:
[b]CFINCLUDE FILE:[/b]
<!--- if "FormId" or "from_id" are defined then continue, else abort --->
<cfif isdefined("FormId") or isdefined("Form_Id")>
  <!--- query gets all the formfield's from the formfields table --->
  <cfquery name="getFields" datasource="#default_ds#">
    SELECT DISTINCT FA.User_ID, FF.FormField_ID
	FROM   FAns FA INNER JOIN FrmFlds FF ON FA.FormField_ID = FF.FormField_ID
	WHERE  FA.Form_ID = #FormId#
	ORDER BY FF.FormField_ID
  </cfquery>
  <!--- gets all the badwords from the badwords table --->
  <cfquery name="badwordscheck" datasource="#default_ds#">
    SELECT RTRIM(BadWords) as BD
    FROM BadWordsTable
  </cfquery>
  
   <!--- defines all needed vars --->  
   <cfmodule template="definevars.cfm" vars="FAList,badwordslist,listMatches,fields,badWordsstatus">
    <!--- creates a list of the 'formfield_id' --->
    <cfset fields = valuelist(getFields.FormField_ID)>
    <!--- creates a list of the 'badwords' --->
	<cfset badwordslist = valuelist(badwordscheck.BD)>
    <!--- sets badwordsstatus to 0 intially --->
	<cfset badWordsstatus = 0>
    <!--- creates var to store badwords --->
	<cfset listMatches = "">

	<cfoutput>
	  <cfloop index="FAListIndex" from="#listfirst(fields)#" to="#listlast(fields)#">
		<cfset FAList = listappend(FAList,trim(FormAnswer))>
		<cfset FAList = Replace(FAList," ",",","ALL")>
		  <cfloop list="#FAList#" index="i" delimiters="|,">
    		<cfif listFindNoCase(badwordslist, i)>
      			<cfset listMatches = listAppend(listMatches,i)>
				<cfset badWordsstatus = badWordsstatus + 1>
    		</cfif>			
		  </cfloop>
		<cfset FAList = "">
	  </cfloop>
	</cfoutput>
   
<cfelse>
  <cfabort>
  No form identified
</cfoutput>
</cfif>

[sub]
____________________________________
Just Imagine.
[sub]
 
Observe the colored texts:

Code:
<!--- if "FormId" or "from_id" are defined then continue, else abort --->
<cfif isdefined("FormId") or isdefined("Form_Id")>
  <!--- query gets all the formfield's from the formfields table --->
  <cfquery name="getFields" datasource="#default_ds#">
    SELECT DISTINCT FA.User_ID, FF.FormField_ID
    FROM   FAns FA INNER JOIN FrmFlds FF ON FA.FormField_ID = FF.FormField_ID
    WHERE  FA.Form_ID = #FormId#
    ORDER BY FF.FormField_ID
  </cfquery>
  <!--- gets all the badwords from the badwords table --->
  <cfquery name="badwordscheck" datasource="#default_ds#">
    SELECT RTRIM(BadWords) as BD
    FROM BadWordsTable
  </cfquery>
  
   <!--- defines all needed vars --->  
   <cfmodule template="definevars.cfm" vars="FAList,badwordslist,listMatches,fields,badWordsstatus">
    <!--- creates a list of the 'formfield_id' --->
    <cfset fields = valuelist(getFields.FormField_ID)>
    <!--- creates a list of the 'badwords' --->
    <cfset badwordslist = valuelist(badwordscheck.BD)>
    <!--- sets badwordsstatus to 0 intially --->
    <cfset badWordsstatus = 0>
    <!--- creates var to store badwords --->
    <cfset listMatches = "">

    [red]<cfoutput>
      <cfloop index="FAListIndex" from="#listfirst(fields)#" to="#listlast(fields)#">
        <cfset FAList = listappend(FAList,trim(FormAnswer))>
        <cfset FAList = Replace(FAList," ",",","ALL")>
          <cfloop list="#FAList#" index="i" delimiters="|,">
            <cfif listFindNoCase(badwordslist, i)>
                  <cfset listMatches = listAppend(listMatches,i)>
                <cfset badWordsstatus = badWordsstatus + 1>
            </cfif>            
          </cfloop>
        <cfset FAList = "">
      </cfloop>
    </cfoutput>[/red]

    <!--- [red]The code above is likely the errant code,
          I'd say... change the [blue]<cfoutput>[/blue] to
          [blue]<cfoutput group="#user_ID#">[/blue].[/red] --->
   
<cfelse>
  <cfabort>
  No form identified
</cfoutput>
</cfif>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Hey, i'll adding that to the script and see what happens. But will adding that cause any performance issues?? I don;t want the dB using twice as many resources if I place "group" in <cfoutput> on both files.

The first file calls: <cfoutput query="getUser" group="User_ID">

And the <CFINCLUDE> file will call: <cfoutput group="User_ID">, and if that doesn't work i'll try: <cfoutput query="getFields" group="User_ID">

[sub]
____________________________________
Just Imagine.
[sub]
 
It shouldn't change performance issues, but it may not work..

CF has issues with includes having cfoutputs, sometimes you've got to work around it a bit... My suggestion was just that... a suggestion.. it may require some experimentation but I think that line is where your problem is.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
I don't really know... A custom tag probably wouldn't have any different output.. but its worth a shot I suppose.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Thanks, i'm gonna a lue of options/sugestions now. Another idea I have is to wrap the <CFINCLUDE> yag in <cfoutput> tags. Maybe calling the <cfoutput> outside the <CFINCLUDE> might work.

I'll let you know what happens.

Thanks.

[sub]
____________________________________
Just Imagine.
[sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top