Skip to main content
July 12, 2008
Answered

How to replace information on an include file?

  • July 12, 2008
  • 3 replies
  • 520 views
Hi all - I have an application that allows the users to edit the HTML for their custom headers and footers.

I am looking to give the users a list of predefined terms to include on their pages that gets replaced when the header is viewed on the application.

E.g. the user edits header.html and puts somewhere in there xxxADVANCED_SEARCHxxx in the position they would like their search box to be.

How can I include header.html into the web application in such a way that I can change the xxxADVANCED_SEARCHxxx into <cfinclude template="/advanced_search.cfm"> so that the actual search information is displayedin place of the xxxADVANCED_SEARCHxxx text?
    This topic has been closed for replies.
    Correct answer dongzky
    I had nothing to do so I made a little demo for your app. Do something like this. You can try to run this first and see the output and then you can base your app in this approach.

    We will have 4 files for this little demo:
    1. layout.html - where your users can edit.
    2. xxxADVANCE_SEARCHxxx.cfm - it contains your advance search coded in cf. note the file name given, this will have meaning later in the code
    3. xxxLOGIN_BOXxxx.cfm - it contains your login box coded in cf. note the file name given, this will have meaning later in the code
    4. render.cfm - this will give the final output

    assume this is your html file (layout.html) where your users can edit:

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

    <html>
    <head>
    <title>Untitled</title>
    </head>

    <body>
    xxxADVANCE_SEARCHxxx

    abcd efghi<br />

    xxxLOGIN_BOXxxx

    </body>
    </html>
    ------------------------------------------------------------------------------------------------------------------------------

    assume this is the content of the xxxADVANCE_SEARCHxxx.cfm:

    <cfset strButton = "advance search">

    <cfoutput>
    <form name="frmTemp" action="test.cfm">
    <input type="Text" name="searchtext"><br />
    <input type="Submit" name="butSubmit" value="#strButton#">
    </form>

    </cfoutput>

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

    assume this is the content of the xxxLOGIN_BOXxxx.cfm:

    <cfset strButton = "Login">

    <cfoutput>
    <form name="frmTemp2" action="login.cfm">
    username<input type="Text" name="username"><br />
    password<input type="Password" name="password"><br />
    <input type="Submit" name="butSubmit" value="#strButton#">
    </form>

    </cfoutput>

    ------------------------------------------------------------------------------------------------------------------------------------------------
    below will be the code for render.cfm which will give us the final output you want:

    <!--- this will block any other html outputs after this tag and those which are outside the <cfoutput> tags --->
    <cfsetting enablecfoutputonly="Yes">

    <!--- read the html file where user edits and save the content in htmlContent variable --->
    <cffile
    action="READ"
    file="C:\files\apps\cfapps\myTest\replace\layout.html"
    variable="htmlContent">

    <cfset lstOptions = "xxxLOGIN_BOXxxx,xxxADVANCE_SEARCHxxx"> <!--- list of possible objects user can change the position --->

    <!--- we loop through the list of the objects and place them in the user's choice of position --->
    <cfloop list="#lstOptions#" index="htmlObjects" delimiters=",">
    <!--- if the object in the list is found, then start --->
    <cfif FindNoCase(htmlObjects,htmlContent,1) neq 0>
    <!--- get the position of the object in the htmlContent variable --->
    <cfset "#htmlObjects#_Pos" = FindNoCase(htmlObjects,htmlContent,"1")>

    <!--- get the first half content of the htmlContent variable until to the position of the last letter of the object (e.g. last letter of xxxLOGIN_BOXxxx is "x" --->
    <cfset firstHTML = Left(htmlContent,Evaluate("#htmlObjects#_Pos")+Len(htmlObjects)-1)>

    <!--- get the remaining content of the htmlContent variable which starts one more position where the last letter of the object ends --->
    <cfset remainingHTML = Mid(htmlContent,Evaluate("#htmlObjects#_Pos")+Len(htmlObjects),Len(htmlContent)-(Evaluate("#htmlObjects#_Pos")+Len(htmlObjects)-1))>

    <!--- replace the object with blank/null --->
    <cfset firstHTML = replacenocase(firstHTML,htmlObjects,"")>

    <!--- overwrite htmlContent variable --->
    <cfsavecontent variable="htmlContent">
    <!--- the cfinclude has the meaning of what i said earlier to take note of the file names given --->
    <cfoutput>#firstHTML#<cfinclude template="#htmlObjects#.cfm">#remainingHTML#</cfoutput>
    </cfsavecontent>
    </cfif>
    </cfloop>

    <cfoutput>#htmlContent#</cfoutput> <!--- and here is the output your user wants --->

    3 replies

    July 13, 2008
    Looks good - dont have much time today to try it but will give it a go tomorrow.

    Thanks for the in depth answer!
    July 12, 2008
    I would like for them to be able to put it anywhere they chose.

    I have been playing and can replace the xxxADVANCED_SEARCHxxx with advanced_search.cfm using two <cffile> tags and a replace function, however, it displays the coldfusion as text and doesnt process the cf tags within advanced_search.cfm - still baffled!
    dongzkyCorrect answer
    Inspiring
    July 12, 2008
    I had nothing to do so I made a little demo for your app. Do something like this. You can try to run this first and see the output and then you can base your app in this approach.

    We will have 4 files for this little demo:
    1. layout.html - where your users can edit.
    2. xxxADVANCE_SEARCHxxx.cfm - it contains your advance search coded in cf. note the file name given, this will have meaning later in the code
    3. xxxLOGIN_BOXxxx.cfm - it contains your login box coded in cf. note the file name given, this will have meaning later in the code
    4. render.cfm - this will give the final output

    assume this is your html file (layout.html) where your users can edit:

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

    <html>
    <head>
    <title>Untitled</title>
    </head>

    <body>
    xxxADVANCE_SEARCHxxx

    abcd efghi<br />

    xxxLOGIN_BOXxxx

    </body>
    </html>
    ------------------------------------------------------------------------------------------------------------------------------

    assume this is the content of the xxxADVANCE_SEARCHxxx.cfm:

    <cfset strButton = "advance search">

    <cfoutput>
    <form name="frmTemp" action="test.cfm">
    <input type="Text" name="searchtext"><br />
    <input type="Submit" name="butSubmit" value="#strButton#">
    </form>

    </cfoutput>

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

    assume this is the content of the xxxLOGIN_BOXxxx.cfm:

    <cfset strButton = "Login">

    <cfoutput>
    <form name="frmTemp2" action="login.cfm">
    username<input type="Text" name="username"><br />
    password<input type="Password" name="password"><br />
    <input type="Submit" name="butSubmit" value="#strButton#">
    </form>

    </cfoutput>

    ------------------------------------------------------------------------------------------------------------------------------------------------
    below will be the code for render.cfm which will give us the final output you want:

    <!--- this will block any other html outputs after this tag and those which are outside the <cfoutput> tags --->
    <cfsetting enablecfoutputonly="Yes">

    <!--- read the html file where user edits and save the content in htmlContent variable --->
    <cffile
    action="READ"
    file="C:\files\apps\cfapps\myTest\replace\layout.html"
    variable="htmlContent">

    <cfset lstOptions = "xxxLOGIN_BOXxxx,xxxADVANCE_SEARCHxxx"> <!--- list of possible objects user can change the position --->

    <!--- we loop through the list of the objects and place them in the user's choice of position --->
    <cfloop list="#lstOptions#" index="htmlObjects" delimiters=",">
    <!--- if the object in the list is found, then start --->
    <cfif FindNoCase(htmlObjects,htmlContent,1) neq 0>
    <!--- get the position of the object in the htmlContent variable --->
    <cfset "#htmlObjects#_Pos" = FindNoCase(htmlObjects,htmlContent,"1")>

    <!--- get the first half content of the htmlContent variable until to the position of the last letter of the object (e.g. last letter of xxxLOGIN_BOXxxx is "x" --->
    <cfset firstHTML = Left(htmlContent,Evaluate("#htmlObjects#_Pos")+Len(htmlObjects)-1)>

    <!--- get the remaining content of the htmlContent variable which starts one more position where the last letter of the object ends --->
    <cfset remainingHTML = Mid(htmlContent,Evaluate("#htmlObjects#_Pos")+Len(htmlObjects),Len(htmlContent)-(Evaluate("#htmlObjects#_Pos")+Len(htmlObjects)-1))>

    <!--- replace the object with blank/null --->
    <cfset firstHTML = replacenocase(firstHTML,htmlObjects,"")>

    <!--- overwrite htmlContent variable --->
    <cfsavecontent variable="htmlContent">
    <!--- the cfinclude has the meaning of what i said earlier to take note of the file names given --->
    <cfoutput>#firstHTML#<cfinclude template="#htmlObjects#.cfm">#remainingHTML#</cfoutput>
    </cfsavecontent>
    </cfif>
    </cfloop>

    <cfoutput>#htmlContent#</cfoutput> <!--- and here is the output your user wants --->
    Inspiring
    July 12, 2008
    does the position have to be where the user wants it(anywhere)? or do you give the users choices on some specific locations on where they can put the object(such as position 1, position 2, position 3, and so on....)?