Link to home
Start Free TrialLog in
Avatar of joehodge
joehodge

asked on

passing variables into coldfusion (cfinclude) templates

Hi,

I am migrating a pl/sql web based system onto an Coldfusion server so have to re-write all my stored procedures into .cfm files.

In my current environment I use templates e.g.

PROCEDURE Pr_Main (p_procedure_name varchar2 default 3, p_div_count varchar2 default null) IS

BEGIN

        Pkg_StyleSheets.Pr_HeadTitle('KPI Front End');
         Pkg_StyleSheets.Pr_StyleSheet1;
         Pkg_StyleSheets.Pr_Javascript;
          Pkg_LOGIN.Pr_Menu(p_div_count);
      htp.bodyclose;

END;

and this creates the basis for all my pages,

I want to re-create this in coldfusion but read that:

..The model of an included page is that it is part of your page; it just resides in a separate file. The cfinclude tag cannot pass parameters to the included page, but the included page has access to all the variables on the page that includes it......

from:

http://livedocs.macromedia.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00000995.htm

Being a complete newbie to coldfusion how do I get round this?
Avatar of pinaldave
pinaldave
Flag of India image

CFC are something similar to cfinclude.
You can pass the params to that and receive the result back. Coldfusion component functions are object oriented methods of coldfusion.
http://www.macromedia.com/devnet/mx/coldfusion/cfcs.html
Regards,
---Pinal
ASKER CERTIFIED SOLUTION
Avatar of Dain_Anderson
Dain_Anderson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dain_Anderson
Dain_Anderson

Oops, that should read:

#Attributes.My_Variable2#
#Caller.My_Variable1#
Dain has good suggestion.
If you are confused what is different between Custom Tag/module and CFC .
Here is good article for you : http://www.macromedia.com/devnet/mx/coldfusion/articles/custom_cf_tag.html
Regards,
---Pinal
Avatar of joehodge

ASKER

wow! thanks for the quick responses.

I will plod my way through them so I might not respond straight away.

thanks
you can use the <cfinclude template="yourpage.cfm"> to call pages to yours. I'm not sure where a custom tag or cfmodule would help? unless you have apps you want to include in your page?

if you have say a calendar popup app...

use cfmodule to call and pass parameters or cf_tagName and used much the same...

or if you have properties set on certain pages and want to include them, this also includes queries and styles,

use <cfinclude template="YourTemplateName.cfm">

but you mentioned you have templates, I think cfinclude would work best... I may have missed something though ;0)

~trail
one thing to may sure of, if there are required for the template, they have to be set on the page, of called before the template that requires them is called... example:

<cfset myvarr = NameOfHouse>
<cfinclude template="qry_houses_template.cfm">

<cfoutput query="houses_names_sp">
#currentrow# - #house_name#
</cfoutput>

the qry_houses_template.cfm would contain something like this....

-------this calls a stored procedure--------------
<cfquery name="houses_names_sp" datasource="#DSN#">
      { call dbo.houses_names_sp ('#myvarr#') }
</cfquery>

or

-------standard query--------------------
<cfquery name="houses_names_sp" datasource="#DSN#">
   SELECT * FROM YourTable WHERE HouseName = '#myvarr#'
</cfquery>

so the variable would have to be set prior to calling that page, or it will give an error...

~trail
Hi,

I used the cfmodule tag:

----------Home.cfm-----------------
<!-- header starts here -->
      <cfmodule template = "Header.cfm" title = "Home Page">
      </head>
<!-- header ends here -->

<!-- content starts here -->
      <body>


----------Header.cfm-----------------


<cfset title = attributes.title>

<cfoutput>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
      <title>#title#</title>
      
<link rel="stylesheet" type="text/css" href="./stylesheet/OTIF.css">
      
<SCRIPT language="Javascript">
                  var password;
                  var username;

etc...

thanks for all your help!
just to let you know, most would code what you did in a cftemplate, and set their header attribute in their application.cfm, what you did seems kind of backwards and not a proper use of cfmodule..

it's like driving a big rig to and from work in the morning, it'd work but why not just use a car???? much easier huh?? You set your header title in your application.cfm build a thousand pages and all you have to do is change that one variable. You use cfmodule you have to change it on every other page where you call it. Seems like a lot of extra hastle to me, which is why I recommended the cfinclude. One I thought yuo were doing just this with it, and two you really don't need a cfmodule because it's not a function or app your running on your page, it's just a header with a dynamic title...

Anyway, it's your page and your code, I personally haven't seen it done that way anywhere,a nd wouldn't use that method myself, our site has well over a thousand pages and it would be such a hastle doing that.

best regards,
~trail
I agree with trailblazzyr55 in that unless your title dynamically changes page-by-page, you would want to set it at a higher level (i.e., Application.cfm); however, if your page title is dynamic, you could certainly use CFMODULE, as it would make sense in this situation.

-Dain
I guess I've got away from static page creation...

you can certainly have different page titles for different pages with cfinclude.. here's an example of what I'm saying...

[Folders]

wwwroot...
application.cfm
index.cfm
[Site Loc 1]
[Site Loc 2]
[Site Loc 3]
[Javascript]
[Queries]
[SysFunc]

..[Site Loc 1]
  index.cfm
  otherfiles.cfm..

..[Site Loc 2]
  index.cfm
  otherfiles.cfm

.. and so on...

Now you set a default header title in your root application.cfm ie: <cfparam name="title" default="MyTitle">

this would be your site hompage url: http://www.yourdomain.com  right????

this uses the root index.cfm

NOW:

you have subfolders in you wwwroot directory (site pages)... following???

in those folders you can have a file called header.cfm and title would be ofcourse #title# right??

your index page for that site/folder/part of site would set the title variable which over-rides the application default...

so each page in that folder which calls the correct header.cfm template uses that specific title, you want to change the title at a different time, easy... edit the index.cfm for that folder...

each page in that folder would use cfinclude to call the header... there's no need for cfmodule anywhere...

so it puzzles me why you'd want to use cfmodule to call a header, yes it's possible... but why do it?

Anyway, not to cause a big discussion or anything, that's just my way, others have different ways maybe for easier or harder, just the amount of time I guess you want to spend editing at a later point I guess...

questions been answered so I guess they're happy with what they got ;o)

Dain_Anderson,

just out of curiosity... why would using cfmodule make sense in this situation? or better sense than cfinclude? Not partial to cfinclude or anything I'm just curious ;o)

~trail







Trail,

If you're just using CFMODULE for a title, I would tend to agree that it's overkill, even if the title is dynamic. One situation that I was confronted with was for an intranet application I built a while ago for a bank that essentially required a "header" file to be on every page that was dynamically built. The header file ran a routine that created on-the-fly inner-page title graphics (using Java). If we used CFINCLUDE to do this, the page would wait until the graphic loaded, while doing it with CFMODULE, we were able to process the rest of the page while the external CFMODULE routine processed the dynamic graphic. Granted, each situation needs to be evaluated as to the best method, but I assure you in some cases (such as the one I described) it could easily be necessary.

Now, as for HTML titles, etc, I personally use a "wrapper" for all layouts, etc. so that I don't have to run any includes or modules for most applications. But I think for the scope of this question, that gets to be a little complicated.

-Dain
Yep you're right, I could definitely see where having a function to display dynamic page titles based on criteria would require a cfmodule.
I don't think joehodge was using Java or any functions, he just wanted to display a page title by means of templates in the header... from what I read anyway...

I think you're right a wrapper would do as well. but yes i think is beyond the scope of the question ;o)

~trail
Hi all,

it seems like I've kicked off a bit of a discussion!

The header 'title' attribute was just to get things going, I will need to pass in all sorts of variables when I use my menu template. I was also looking to completley seperate the logic e.g. SQL selects etc from the HTML code in other pages, so that it should(?) be easier to maintain.

I will probably change the code now I know about application.cfm but I just wanted to get a working example of passing parameters through templates.

I have already encountered a few issues with using cfmodule so I'm sure you'll here from me again!
Also as our site is an intranet, every page has its own unique title so if every page contained an identical header e.g.

<cfset title = attributes.title>

<cfoutput>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
     <title>#title#</title>
     
<link rel="stylesheet" type="text/css" href="./stylesheet/OTIF.css">
  <script src="/scripts/main.js" type="text/javascript"></script>

</head>

etc

Is it better practice to have this in a template or should I be putting all this into application.cfm or 'wrappers'?
I'd prefer a wrapper here, as commonly used in the FuseBox methodology, or perhaps an "output" CFC method. For SQL, I _personally_ encapsulate all my queries, sorted by functionality, in CFCs. I store heavily-used, common CFCs in Session variables, then stransfer them to the Request scope using StructAppend() (surrounded in a CFLOCK). If you're not familiar with CFCs, I'd highly recommend you take a look. I don't even use Application.cfm for common variables like the datasource name, paths, etc. -- I use an initialization CFC to store this data, which in turn accesses an XML file for all settings. This allows me to "port" the site easily to many different clients without having to change _any_ code at all; rather, I simply alter the _manifest.xml file for each client.

To give you an idea of what one of my Application.cfm files looks like, I've attached it here:

<CFSILENT>
<!------------------------------------------------------------
      No cache headers --->
<CFHEADER NAME="Expires"          VALUE="Tue, 01 Jan 1985 00:00:01 GMT">
<CFHEADER NAME="Pragma"        VALUE="no-cache">
<CFHEADER NAME="cache-control"       VALUE="no-cache, no-store, must-revalidate">

<!------------------------------------------------------------
      Application/Session Control Variables --->
<CFPARAM NAME="Request.SERVER_PORT"   DEFAULT="80">
<CFPARAM NAME="Request.SessionTimeout"       DEFAULT="#CreateTimeSpan(0,0,59,0)#">
<CFPARAM NAME="Request.AppRefresh"         DEFAULT="">
<CFPARAM NAME="Request.FailoverEmail"         DEFAULT="xxxxx@xxxx.com">
<CFPARAM NAME="Request.Debug"                 DEFAULT="No">

<!------------------------------------------------------------
      Disable/Enable debugging --->
<CFSETTING SHOWDEBUGOUTPUT="#Request.Debug#">

<!--- For installations with different SSL host, use a unified app name --->
<CFPARAM NAME="Request.ApplicationName" DEFAULT="SA_#GetToken(CGI.SERVER_NAME, 1, ".")#">

<!------------------------------------------------------------
      We use JSESSIONIDs, NOT CFID/CFTOKEN --->
<CFIF IsDefined("Cookie.CFID")>
      <CFCOOKIE NAME="CFID" VALUE="#CFID#" EXPIRES="NOW">
</CFIF>
<CFIF IsDefined("Cookie.CFTOKEN")>
      <CFCOOKIE NAME="CFTOKEN" VALUE="#CFTOKEN#" EXPIRES="NOW">
</CFIF>

<!------------------------------------------------------------
      Instantiate Application for the Specific Client --->
<CFAPPLICATION
      NAME="#Request.ApplicationName#"
      CLIENTMANAGEMENT="No"
      SESSIONMANAGEMENT="Yes"
      SETCLIENTCOOKIES="No"
      SESSIONTIMEOUT="#Request.SessionTimeout#"
      APPLICATIONTIMEOUT="#CreateTimeSpan(0,12,0,0)#">

<!------------------------------------------------------------
      First Pass: Make sure this account exists
      Get Account settings and server-specific vars
      (stored in Application) --->
<CFLOCK SCOPE="APPLICATION" TYPE="EXCLUSIVE" THROWONTIMEOUT="Yes" TIMEOUT="20">
       <CFIF IsDefined("Application.Started")>
             <CFSET StructAppend(Request, Application)>
      <CFELSE>
            <CFINVOKE COMPONENT="components.accounts.cfc_AccountSettings" METHOD="getAccountInformation" RETURNVARIABLE="IsValid">
            <!------------------------------------------------------------
                  Ensure the account exists (true) --->
            <CFIF IsValid>
                  <CFSET StructAppend(Request, Application)>
            <!------------------------------------------------------------
                  Account doesn't exist --->
            <CFELSE>
                  <CFMODULE TEMPLATE="security/dsp_accessDenied.cfm" ErrUnencrypted="ACC-1001">
                  <CFABORT>
            </CFIF>            
      </CFIF>
</CFLOCK>

<!------------------------------------------------------------
      Browser Detection --->
<CFLOCK SCOPE="SESSION" TYPE="EXCLUSIVE" TIMEOUT="10" THROWONTIMEOUT="Yes">
      <CFIF NOT IsDefined("Session.Browser")>
            <CFINVOKE COMPONENT="components.cfc_BrowserDetection" METHOD="getBrowser" RETURNVARIABLE="Session.Browser">
      <CFELSE>
            <CFSET StructAppend(Request, Session)>
      </CFIF>                        
</CFLOCK>

<!------------------------------------------------------------
       UTF-8 Encoding --->
<CFSET SetEncoding("FORM", "UTF-8")>
<CFSET SetEncoding("URL", "UTF-8")>

</CFSILENT>

-Dain

thanks for the expert advice!,

I will open another thread with the subject of the above so your time is rewarded.