Skip to content

Instantly share code, notes, and snippets.

@crimson-knight
Last active March 2, 2020 12:14
Show Gist options
  • Save crimson-knight/5c0da54edcf792fb3a000d369a788dde to your computer and use it in GitHub Desktop.
Save crimson-knight/5c0da54edcf792fb3a000d369a788dde to your computer and use it in GitHub Desktop.
Want a nice scalable way to produce breadcrumb schema mark up in a Rails app? Here you go! Just customize the base and pass it a hash, and you'll get compliant Schema for Google to use nice Bread Crumbs in your urls.
<%#
# Want to add fancy Bread Crumbs to your search results page?
# https://developers.google.com/search/docs/data-types/breadcrumb
# Here's a simple layout I made to make this easy to apply across all search engine findable content.
# Just use this in the bottom of your show page or index pages/views.
# To use this, just pass in an array of hashes, each hash with {name: , slug: }.
# Each tier of slug should contain the full uri with no leading slash, instead of just the object's slug.
# Examples:
# yourwebsite.com/ use: path = nil
# yourwebsite.com/blog use: path = {obj: path = [{name: 'Blog', slug: 'blog'} ]}
# yourwebsite.com/blog/post-about-something use: path = {obj: path = [{name: 'Blog', slug: 'blog'},{name: @post.title, slug: 'blog/' + @post.slug }]}
# # This will produce a single breadcrumb for your home page.
# render ('schema-path'), obj: paths = nil
# # This will produce a 2 tier breadcrumb for your blog page.
# render ('schema-path'), obj: paths = [ { name: 'Our Blog Posts', slug: 'blog' } ]
# # Use the view instances variables
# render ('schema-path'), obj: paths = [ { name: 'Our Blog Posts', slug: 'blog' }, { name: @post.title, slug: 'blog/' + @post.slug} ] }
# There are no limits to the depth of the bread crumbs, but I suggest keeping you're public url structure under 4 tiers when possible.
-%>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [{
"@type": "ListItem",
"position": 1,
"name": "Crimson Knight Studios",
"item": "https://www.crimsonknightstudios.com/"
}<%- unless obj.nil? -%><%- obj.each_with_index do |path, index| -%>
,{
"@type": "ListItem",
"position": <%= index+2 -%>,
"name": "<%= path[:name] -%>",
"item": "https://www.crimsonknightstudios.com/<%= path[:slug] -%>"
<%- if obj.length-1 == index || obj.nil? -%>
}]
<%- else -%>
}
<%- end -%>
<%- end -%>
<%- else -%>
]
<%- end -%>
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment