Replacing the Title Text in DotNetNuke Modules
8/31/2005 11:14:54 AM
In this snippet of code I want to provide you with a method of changing the title tags of DotNetNuke. This code could also be applied to screen scraping or another other post processed HTML text you wish to modify
In this
snippet of code I want to provide you with a method of changing the title tags
of DotNetNuke. This code could also be applied to screen scraping or another
other post processed HTML text you wish to modify
The reason for developing this code was mainly for SEO purposes. A forum module
for the site did not change the title tag when accessing different forum
categories or posts. If you work with SEO issues you'll know that that the Title
tag is one of the most important tags on a Web page for targeting and
displaying pages within the search results for the various engines. One of the
major drawbacks as a module developer for DNN is the inability to easily modify
the title tag within the HTML document. Granted if you do not have multipage
modules it really isn't an issue, but for modules like a forum, directory, or
any module that displays different content within its instance you may want to
modify the title tag.
Here in our code we started in the BasePage.vb class within DNN since we are
modifying the behavior of a module that we don't have the source code for.
Since the code is being processed after the module renders its content, we
manipulate the string output of the page to provide the desired results within
the basepage code with no need for the source of the third party module.
You may also want to use the code within your own modules to modify certain
attributes of the HTML page. Something similar can be done in your module's
code, or within an HTTP module without having to modify the DNN core code. In
this example however, we need to recompile the DNN core code in order to apply
the changes.
The first method we will create is a function called ReplaceTitleText, this
function will accept a string which is the rendered HTML page, and search for
specific tags that contain the content we want to place in the title tag within
the HTML document. In this example we will hard code the values we're searching
for, but in your own code you may want to change it to be more dynamic. In
addition, we'll then use a Regular Expression to find our title tag and insert
the newly found text into it and replacing the existing title text.
Function
ReplaceTitleText(ByVal inString
As String)
As String
'find forum
'<div class="NTForums_Subject">
'<div class="NTForums_Crumb"> - have to strip out HTML
If inString.IndexOf("<div class=""NTForums_Subject"">")
> 0 Then
Dim pattern
As New Regex("<div
class=""NTForums_Subject"">((.|\n)*?)</div>")
Dim oM
As Match = pattern.Match(inString)
Dim title
As String =
"<TITLE>((.|\n)*?)</TITLE>"
Dim tmpString
As String =
Regex.Replace(inString, title, "<TITLE>" & StripHTML(oM.Value) & "</TITLE>")
Return tmpString
Else
Return inString
End If
End Function
This
next method is for stripping out the HTML within the tags we're looking for.
Function StripHTML(ByVal
inText Ass String)
As String
Dim pattern As
String = "<(.|\n)*?>"
tmpString As
String = Regex.Replace(inText, pattern,
String.Empty)
Return tmpString
End Function
Finally we will override the Render method of the Web form.
Render occurs post process, meaning all the module code has ran, and the
page is now a string. We then call our ReplaceTitleText function and send it the
html and then write out the processed output in the HTTP stream to the Web
browser.
Protected Overrides
Sub Render(ByVal
writer As System.Web.UI.HtmlTextWriter)
Try
Dim stringWriter
As System.IO.StringWriter =
New System.IO.StringWriter
Dim htmlWriter
As HtmlTextWriter = New
HtmlTextWriter(stringWriter)
MyBase.Render(htmlWriter)
Dim html
As String =
stringWriter.ToString()
writer.Write(ReplaceTitleText(html))
Catch ex As
Exception
End Try
End Sub
|