Element 'title' occurs too few times, XHTML validation warning in ASP.NET.MVC master page - asp.net-mvc

I am getting the following XHTML validation warning in my ASP.NET MVC master page:
Validation (XHTML 1.0 Transitional): Element 'title' occurs too few times.
The title tag for the master page is included in the ContentPlaceHolder in the head tag as shown in the code below. The title tag in the ContentPlaceHolder is not taken into account when performing the validation, and I do not want to just add another one in the head tag because then I will be left with two title tags.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server">
<title></title>
</asp:ContentPlaceHolder>
</head>
One work around that I have found is to use the following technique in the head tag:
<% if (false) { %>
<title></title>
<% } %>
Is this the best practice to resolve this warning? I am not a huge fan of adding the excess code just to pass validation warnings but I will live with it if there is not a better alternative.

Do this instead:
<head>
<title><asp:ContentPlaceHolder ID="title" runat="server">Default Page Title Here</asp:ContentPlaceHolder></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
Or as an alternate, set the title programattically from each page.
What's happening in your case is that when a new view is created, it creates empty content items which override the default content in the placeholders. If you remove the empty content blocks from the view, the default placeholder content will be used, but then you can't set the contents from the view. Using the code above you can override a default title from each view and include scripts, etc. in the head independently of each other.

Here possible solutions are
First solution is
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server">
//<title></title> - this line should be removed.
</asp:ContentPlaceHolder>
second solution is,
Check whether the head tag having attribute runat="server",if have not set runat prperty means not a problem else need to remove the runat tag.

Related

Open Graph meta tags not accessible, preview of page in Facebook shows "422"?

I can't make my open graph tags accessible to Facebook. When I try to share a page, the site links properly, but the og attributes don't show up, and the title just displays as "422". I'm not sure what this means.
When I run the facebook debugger https://developers.facebook.com/tools/debug/og/object/ I get the following error, which isn't very helpful:
Error parsing input URL, no data was cached, or no data was scraped.
Using the echo feature in the debug tools, all I get is:
Document returned no data
The meta tags appear to be set up properly in my document:
<!DOCTYPE html>
<html>
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="robots" content="noarchive,noodp,noydir" />
<meta name="referrer" content="always" />
<meta property="fb:app_id" content="375576830972731" />
<meta property="og:site_name" content="Site Name" />
<meta property="og:title" content="Title of content" />
<meta property="og:url" content="https://website.domain.com/123456" />
<meta property="og:image" content="http://s3-us-west-1.amazonaws.com/domain/media/images/001/original/001" />
<meta property="og:description" content="Description of content" />
<meta property="og:type" content="article" />
</head>
I made sure to allow for the Facebook crawlers in robots.txt:
User-agent: Facebot
Allow: /
User-agent: facebookexternalhit/1.1
Allow: /
What I am I missing?
The problem here was my protect_from_forgerymethod in application.rb. I added an exception and the Facebot can now see the page.
class StoriesController < ApplicationController
protect_from_forgery except: :show

GSP variables set in layout not visible in pages

I have some GSP variables set in the "main" layout file of my Grails application (included below). The values of these variables don't appear to be accessible in the GSP pages that are rendered by the sitemesh. Also, they are not visible in any templates that are rendered by the tag. I have tried setting the scope="request" (see code) below, but that doesn't appear to make any difference. I'm clearly not understanding the scoping rules for GSP variables.
Can anyone clarify how GSP variables are scoped and make a recommendation on how I can communicate them from the layout all the way down to templates (if I can at all).
<!DOCTYPE html>
<%-- <html lang="${org.springframework.web.servlet.support.RequestContextUtils.getLocale(request).toString().replace('_', '-')}"> --%>
<html lang="${session.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'}">
<head>
<title><g:layoutTitle default="${meta(name:'app.name')}" /></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<asset:javascript src="bootstrap.js" />
<theme:load />
<asset:javascript src="application.js" />
<asset:stylesheet src="application.css" />
<asset:link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<g:layoutHead />
<%-- Defineform body layout column dimensions. These values are used by Bootstrap based forms to
layout using configured column widths and offsets. --%>
<g:set var="labelWidth" value="${grailsApplication.config.ark.layout.labelWidth ?: 'col-sm-3'}" scope="request" />
<g:set var="controlWidth" value="${grailsApplication.config.ark.layout.controlWidth ?: 'col-sm-4'}" scope="request" />
<g:set var="controlOffset" value="${grailsApplication.config.ark.layout.controlWidth ?: 'col-sm-offset-3'}" scope="request" />
<%-- For Javascript see end of body --%>
</head>
<body>
<g:render plugin="arkUi" template="/layouts/menu/navbar"/>
<g:render plugin="arkUi" template="/layouts/content"/>
<g:render plugin="arkUi" template="/layouts/footer"/>
<!-- Include deferred Javascript files and other resources -->
<asset:deferredScripts/>
</body>
</html>
The issue here is that Grails first parses the target GSP page to (among other things) determine which layout to use, then parses the layout GSP and combines them. So the layout can see variables you set in the page but not vice-versa.

How to downcase an entire HTML document parsed with Nokogiri

I need to downcase all text in an HTML document that has been parsed with Nokogiri. Here my code:
agent = Mechanize.new
page = agent.get('http://www.example.com').parser.search('//*[translate(text(),"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") = *]').to_html
There is not error as such in the code; it executes without an error. If I go in and check a random tag in the document, however, the case is still the same as before. Is there another/better way to downcase all text in a document?
You could use traverse to downcase all text nodes:
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open("http://www.example.com/"))
doc.traverse do |node|
node.content = node.content.downcase if node.text?
end
puts doc.to_html
Output:
<!DOCTYPE html>
<html>
<head>
<title>example domain</title>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
body { ... }
</style>
</head>
<body>
<div>
<h1>example domain</h1>
<p>this domain is established to be used for illustrative examples in documents. you may use this
domain in examples without prior coordination or asking for permission.</p>
<p>more information...</p>
</div>
</body>
</html>

grails header title not working with layout - sitemesh and layout

I have a base header layout (base-header-footer.gsp)
<!DOCTYPE html>
<html>
<head>
<title><g:layoutTitle default="${g.message(code: 'title.index.page')}"/></title>
</head>
... some common resources loading....
<body id="launch">
<g:layoutBody/>
...........................
<r:layoutResources />
</body>
</html>
And then 2 more header, one for logged-in user, and another for guest users, and both of these header layout are extending the base layout.
Guest users (anonymouys-header-footer.gsp) -
<g:applyLayout name="base-header-footer">
<!DOCTYPE html>
<html>
<head>
<g:layoutHead/>
</head>
<body>
... render guest user header
<g:layoutBody/>
</body>
</html>
Logged-in users (loggedin-header-footer.gsp) -
<g:applyLayout name="base-header-footer">
<!DOCTYPE html>
<html>
<head>
... some css
<g:layoutHead/>
</head>
<body>
... Render header for logged-in user
</body>
... load some JS file...
</html>
Now in specific pages I apply guest OR logged-in layout based on user's login state, hence I want to show the page specific title user is on, but it doesn't work.
This is how I am using those layout
OrderStatus.gsp -
<!DOCTYPE html>
<html>
<head>
<title>Order status | Some title</title>
<meta name="layout" content="logged-in-header-footer" />
<script type="text/javascript" src="${resource(dir:'js',file:'some.js')}"></script>
</head>
<body>
</body>
</html>
But I still see the title which is defined base-header-footer.gsp, not the one in OrderStatus.gsp
I have also tried using g:layoutTitle in OrderStatus.gsp but doesn't help.
Any help is highly appreciated.
Use
<meta name="layout" content="base-header-footer">
in your pages to load the layout, then add your title there,
<title>${whatever.something()}</title>
in your layout add this:
<title><g:layoutTitle/></title>
enjoy.
Try to use
<title><g:layoutTitle/></title>
in your layouts (base-header-footer and loggedin-header-footer.gsp). More info in the official documentation.

og meta Facebook & rails index: sharing info not updated in facebook share-box but correct in html

I'm using Facebook open graph meta tag inside one of my rails index view, which is an article post including a share on facebook link.
Everything worked well for the first post, and the facebook share box takes into account the meta provided (title, url, etc).
But when I want to share another post, then the facebook share-box keep the previous post info. In the html, the meta are good, and when I use facebook debug, everything is fine.
What I am missing ? There is a cache flush to do ?
Thank you a lot for the help
<meta property="og:title" content="<%= #post.title %> wanted on XXX!" />
<meta property="og:site_name" content= "Site Name" />
<meta property="og:url" content="http://siteroot<%= display_path(#post) %>" />
<meta property="og:description" content="Site is bla bla bla" />
<meta property="og:image" content="<%= #post.vignette.url(:medium) %>" />
<meta property="og:type" content="article" />
<meta property="article:publisher" content="https://www.facebook.com/appname" />
<meta property="article:section" content="Responsive" />
I can't tell if you're doing this or not but make sure you're sharing the post page and not the index page.
href="www.facebook.com/sharer/sharer.php?u=<%= #post.url %>"
The og tags in the head won't update without reloading the page, so you'll have to point Facebook to each post's unique url.

Resources