Skip to main content
Inspiring
December 27, 2022
Question

In ColdFusion do I add to the title tag from a document that is "Included"?

  • December 27, 2022
  • 2 replies
  • 576 views

I have an index page that calls several includes using variables. Part of my site has an inventory section. I would like to insert the name of the inventory item into the title tag. From the index, I can tailor the title tag in the header to show the section and categories, but since the query for the individual item is in the included file, that variable doesn't exist yet when the index comes up. I tried to use the cfhtmlhead from the inventory page, but it neither adds to nor replaces the title that is called in the index.cfm page. Here is my code, but it doesn't work.

<cfhtmlhead text="<title>xxxx</title>"></cfhtmlhead>

I guess, I could pass the inventory item name in the URL, but most items have spaces in them and then I have to deal with that too.

Thanks, Kirk

    This topic has been closed for replies.

    2 replies

    Charlie Arehart
    Community Expert
    Community Expert
    December 28, 2022

    When you say the use of cfhtmlhead does not replace the title, are you doing a "view source" in the browser to see what html was rendered? You may see two titles...one that IS in the head (as put there by that tag) and another that YOUR OTHER CODE put there. Whichever appears first in the browser "wins". 

     

    First, try commenting out your cfml code that puts in that OTHER title: does the "new" one now appear (in the browser title bar)? If so, think of a way to dynamically NOT put in that other title if you can somehow KNOW that you will be putting in the other later. 

     

    I realize you may say you "can't do that". Please at least do the view source above to see what is really going on in the browser about the "two titles". We need to understand the problem before we can solve it. If my solution doesn't work, we'll find another.

     

    (I honestly don't see how bkbk's first answer here relates to this challenge. It seems instead to be talking about "separation of concerns", which is sometimes a useful concept but won't seem to solve this, unless I've totally misread the problem.)

    /Charlie (troubleshooter, carehart. org)
    Inspiring
    December 28, 2022

    I ended up solving the problem by including the Inventory item name in the URL that calls the page. A less elegant solution than just using the cfhtmlhead tag, but it works. Doing so exceeded the max url length that I has specified in my Applications.cfm to prevent code injections, but that was an easy fix.

     

    I will play with the cfhtmlhead some more and look deeper into the rendered code. When I was testing that yesterday, for testing purposes, I used this code...

    <cfhtmlhead text="<title>xxxx</title>"></cfhtmlhead>

    When I viewed the rendered code, I searched for xxx to see if it popped up down in the code and I didn't find it.

     

    I totally admit that I am a complete ColdFusion hack programmer. I started with HTML code and slowly added things that I needed. I did take a quarter of CF in college 20 years ago and probably haven't used any newer code or newer techniques since then.

    Charlie Arehart
    Community Expert
    Community Expert
    December 28, 2022

    Dirk, if that test showed no title of that xxx value, then that's certainly a different problem to solve.  (I wanted to wait to hear first, lest that reply be even longer.)

     

    So first, if you may use cfflush in your code, that can conflict with this. (It should be that if you do a cfflush before that cfhtmlhead tag, it should generate an error. Hopefully there's no error handling hiding that.) 

     

    If you wanted to prove if the tag is putting out that title and nothing else is overriding it, try doing a cfabort right after it. Then do the view source in the browser.

     

    We can dig further from there, but I realize you may feel that with your brute force solution, it's not important to dig further. 🙂 

    /Charlie (troubleshooter, carehart. org)
    BKBK
    Community Expert
    Community Expert
    December 28, 2022

    No problem at all. The solution: just remember that you can place the query in the most suitable position. To do so, you should separate the query tag from the code that uses the query-result.

     

    That is, split the relevant include - the one that contains the query - into 2 separate includes. The first include contains just the query. The second contains code that processes the inventory item.

     

    So, let's assume you have saved the code <cfquery name="getInventoryItem"></cfquery> as the page inventoryItemQuery.cfm and the item-processing page as processInventoryItem.cfm. 

     

    Then you could arrange the includes as follows:

     

    <!--- index.cfm --->
    
    <cfinclude template="inventoryItemQuery.cfm">
    
    <html>
    <head>
        <cfoutput>
        <title>#getInventoryItem.itemName#</title>
        </cfoutput>
    </head>
    
    <body>
    <!--- Process the details from the query getInventoryItem --->
    <cfinclude template="processInventoryItem.cfm">
    
    </body>
    </html>