• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How can I dynamically change title and meta description using CF?

New Here ,
Jul 06, 2009 Jul 06, 2009

Copy link to clipboard

Copied

Our site is very dynamically assembled, and we have one cfm which generates the page header portion, and others to fill out the rest of the page.

Currently, our page title and meta description tag are static.  I wish to be able to change them based upon what tempate is being displayed, but by the time the code runs for the guts of the page, the header has already been generated.  Is there any way using CF that I can change the page title and meta tag values?  I can do this using javascript, but by then the page has been sent to the client and the page source still contains the original static values.  This has to render at the server before it is sent to the client so that search engines get the proper title and description values.

Thanks in advance if you have some advice for me.

TOPICS
Advanced techniques

Views

2.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 06, 2009 Jul 06, 2009

Copy link to clipboard

Copied

Sounds like you would like to modify your response after it has been built, but before it is returned to the web server.

There are several places you can do this.

1) With an Application.CFC you can do it in the OnReqeust() function.  If you build on OnReqeust() function, this code can be run after the reqeust has been built, but before it is returned to the web server.  Be aware that using this function can have a profound affect on web service and ajax reqeusts, so plan accordingly.

2) You could build similar functionality into your own code.  Instead of building your response straight into ColdFusion's response out buffer, build it into a variable that you can manipulate before "outputing" it into the response out buffer.

3) You can do similar functionality by tapping into the underlining java request/response object to intercept the resposne before it is sent to the web server.

OR

You could rework your logic flow so that the system knows what header needs to be used at the time the header is being generatated so as to avoid the above intercepts and convolutions.,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 16, 2009 Jul 16, 2009

Copy link to clipboard

Copied

Paul-V,

You could modify my custom tag to add a line for the META description.  Currently, it does exactly what you are asking for but only for the TITLE tag.

http://www.webtrenches.com/post.cfm/changing-the-page-title-after-it-has-rendered

-Mike

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2009 Jul 16, 2009

Copy link to clipboard

Copied

tSpark,

Beautiful in it's simplicity!  Thank you! Works great for me for the title so far.  Now I have a question about the meta description.  What is the correct syntax to use in the ReReplaceNoCase function?  There are several meta tags so I need to be sure to get only the description tag.  But if I am to include enough string for it to find and remove only the description tag, there is a quotation mark that will mess up the function call - see the red quotation mark below.  Can I escape that somehow?

Sample...

<meta name="description" content="Lot's of great stuff">

Function call...

ReReplaceNoCase(pageContent,"<meta name="description.*>", "", "ALL")

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 16, 2009 Jul 16, 2009

Copy link to clipboard

Copied

You can just change the quotes around that argument to single quotes.  Like this...

ReReplaceNoCase(pageContent,'<meta name="description.*>', "", "ALL")

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2009 Jul 16, 2009

Copy link to clipboard

Copied

Using the single quotes makes sense (should have thought of that from javascript).  But in doing so it looks like it doesn't stop the replace at the > character at the end of the meta tag.  I'm terrible with regular expression stuff, so I think something isn't quite right in that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 16, 2009 Jul 16, 2009

Copy link to clipboard

Copied

Try this...

ReReplaceNoCase(pageContent,'<meta name="description.*?>', "", "ALL")

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2009 Jul 16, 2009

Copy link to clipboard

Copied

LATEST

I think I tried that in one of my iterations, but if it doesn't work, this does.  I figured it out just a bit ago and hadn't gotten it posted yet.

ReReplaceNoCase(pageContent,'<meta name="description(.|\n)*?>', "", "One")

So for the custom tag for any meta tag, I created this which works like a charm...

<cfif thistag.executionmode IS "start">
<cfparam name="attributes.MetaName" default="" type="string">
<cfparam name="attributes.newContent" default="" type="string">
<cfscript>
  pageContent = getPageContext().getOut().getString();
  getPageContext().getOut().clearBuffer();
  pageContent = ReReplaceNoCase(pageContent,'<meta name="#attributes.MetaName#(.|\n)*?>', "", "One");
  writeOutput( pageContent );
</cfscript>
<cfhtmlhead text = "<meta name=#Chr(34)##attributes.MetaName##Chr(34)# content=#Chr(34) & htmleditformat(attributes.newContent) & Chr(34)#>">
</cfif>

Call it using this...

<cf_UpdateMetaTagContent MetaName="description" newContent="#variables.NewDescription#" />

Thank you for your assistance, tSpark.  This is perfect.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation