Copy link to clipboard
Copied
I found this documentation, Adobe ColdFusion 9 * cfhtmlhead , but how do I use it in my page? Where do the <cfhtmlhead text="#sHeader#"> tag go on the page?
Okay, so this is what I have.
<cfsavecontent variable="sHeader">
<cfoutput>
<title>#title# | My Company Site Claire</title>
<meta name="keywords" content="#tags#" />
<meta property="og:locale" content="en_US"/>
<meta property="og:type" content="website"/>
<meta property="og:title" content="#cfData.data.name#"/>
<meta property="og:url" content="#canonical#" />
<meta property="og:site_name" content="My Company website"/>
<meta name="twitter:site" content="@MySite">
<meta name="twitter:title" content="#cfData.data.name#">
<meta name="twitter:creator" content="@MySite">
<link rel="canonical" href="#canonical#" />
</cfoutput>
</cfsavecontent>
<cfhtmlhead text="#sHeader#">
Okay, so now that I have the "text" in the cfhtmlhead tag, do I have to do anything else for it to show inside of the header section of the page?
I figured it out. CFSaveContent cannot be inside of the cfoutput tag. If I move from line 8 to 20 out of the cfoutput, it works.
Copy link to clipboard
Copied
2Charlie wrote:
do I have to do anything else for it to show inside of the header section of the page?
No, but why bother? Why not replace <cfcontent> with <head> which is so much clearer.
Cheers
Eddie
Copy link to clipboard
Copied
Thanks for the response. Do I need to put what's in the <cfcontent> or <head> at the very top of the document or can I put it anywhere on the page? The thing is, some of the information like title and tags can only be extracted once an API has been called. Thus, I need to save the content in the <cfsavecontent> tag but then I don't know where else on the page to place the <cfhtmlhead> tag.
Copy link to clipboard
Copied
You can use <cfContent> and <cfHtmlHead> anywhere in the script (as long as you're not flushing), but using <head> has to be in its correct place within the script.
In other words, the following two examples will produce equivalent HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<!--- API calls here. --->
<cfsavecontent variable="sHeader">
<title>My Title</title>
</cfsavecontent>
<cfhtmlhead text="#sHeader#">
</body>
</html>
or
<!doctype html>
<html lang="en">
<head>
<!--- API calls here. --->
<meta charset="utf-8">
<title>My Title</title>
</head>
<body>
</body>
</html>
Cheers
Eddie
Copy link to clipboard
Copied
Okay, here's what I have.
Inside the index.cfm page, I have a custome script to call an apiCall.cfm file. Inside of this apiCall.cfm file, I have something like this.
<cfif cgi.query_string neq "">
<cfif IsDefined("artDetails") && !IsJSON(artDetails)>
<h3> The URL you requested does not provide valid JSON </h3>
<cfelse>
<cfoutput>
<cfloop>
<!---go through the loop of the API data returned--->
<cfsavecontent variable="sHeader">
<title>#title# | My Company Site Claire</title>
<meta name="keywords" content="#tags#" />
<meta property="og:locale" content="en_US"/>
<meta property="og:type" content="website"/>
<meta property="og:title" content="#cfData.data.name#"/>
<meta property="og:url" content="#canonical#" />
<meta property="og:site_name" content="My Company website"/>
<meta name="twitter:site" content="@MySite">
<meta name="twitter:title" content="#cfData.data.name#">
<meta name="twitter:creator" content="@MySite">
<link rel="canonical" href="#canonical#" />
</cfsavecontent>
</cfloop>
</cfoutput>
<cfhtmlhead text="#sHeader#">
</cfif>
<cfelse>
<p> If you would like to search, please use the top bar.</p>
</cfif>
So, this does not work. However, if I put line 8 to 20 plus line 23 right under line 1 then it the meta data shows up in the page. However, at this location, I can't grab anything from the API data returned because it hasn't gone through the loop to grab the information.
Copy link to clipboard
Copied
Yikes! You do not want to have <title> in a loop. There can only be one <title> tag in the <head> tag of a valid HTML file. Your loop also has no condition, is this just pseudocode as an example?
I would strongly recommend that you don't have apiCall.cfm emit any HTML. Set variables as necessary and have index.cfm emit the HTML depending on the variables set.
Cheers
Eddie
Copy link to clipboard
Copied
No, the cfloop is just a pseudocode example and there will always be only one title or article. Our current setup is that the index.cfm page does not do anything. It's basically a placeholder or container. Could this be the problem why I'm getting anything in my meta data?
Copy link to clipboard
Copied
Yes, that could be the problem. If index.cfm is the only script that calls apiCall.cfm then any page that you browse to, other than index.cfm, will not contain the meta data you are expecting.
Are you perhaps confusing index.cfm with application.cfm?
Cheers
Eddie
Copy link to clipboard
Copied
I figured it out. CFSaveContent cannot be inside of the cfoutput tag. If I move from line 8 to 20 out of the cfoutput, it works.