Skip to content

Instantly share code, notes, and snippets.

@sverrirs sverrirs/AdsInlineTag.rb
Last active Nov 29, 2016

Embed
What would you like to do?
How to create a Jekyll custom tag that injects Google Ads into posts
# http://www.createdbypete.com/articles/create-a-custom-liquid-tag-as-a-jekyll-plugin/
class AdsInlineTag < Liquid::Tag
def initialize(tag_name, input, tokens)
super
@input = input
end
def lookup(context, name)
lookup = context
name.split(".").each { |value| lookup = lookup[value] }
lookup
end
def render(context)
<<-MARKUP.strip
<div style="width: 728px; margin: 0 auto; padding: .8em 0;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-2754843202551236" data-ad-slot="2662611504" data-ad-format="auto"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>
MARKUP
end
def split_params(params)
params.split("|")
end
end
Liquid::Template.register_tag('ads', AdsInlineTag)
@sverrirs

This comment has been minimized.

Copy link
Owner Author

commented Mar 14, 2016

Replace everything between <<-MARKUP.strip and MARKUP with your own Ad-code.

Then just use {% ads %} to inject the ads code into the right locations in your post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.