-
-
Save alphabet/3af6b0dcb556dc3822f00cf95c6813ef to your computer and use it in GitHub Desktop.
Using page tags to control embed codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To create a reliable string of page tags to use for comparison in Liquid conditional statements, place this in layout.html between the <head> and <body>: | |
{% assign tags_list = "" %} | |
{% for tag in page.tags %} | |
{% assign tags_list = tag.name | append: ", " | append: tags_list %} | |
{% endfor %} | |
// We'll need a consistent tag naming convention that contains a 'trigger' phrase and an 'id' to use in the link to the embedded widget. | |
// In this case, we need an ID for a Phone2Action widget, so we'll be using the tag format 'p2a-12345' | |
// | |
// In the page template, we'll loop through the tags, scope the loop to only tags with the 'trigger' phrase, and then split that tag on the delimiter: | |
{% if tags_list contains "p2a-" %} <!-- checks tags_list contains trigger phrase --> | |
{% for tag in page.tags %} <!-- loop through all page tags --> | |
{% if tag.name contains "p2a-" %} <!-- scope loop to tags w/trigger phrase --> | |
{% assign p2a_widget_code = tag.name | split:'-' %} <!-- split tag into array --> | |
p2a[0]:{{ p2a_widget_code[0] }}<br> <!-- prints array index 0, our trigger phrase --> | |
p2a[1]:{{ p2a_widget_code[1] }}<br> <!-- prints array index 1, the ID after our trigger phrase--> | |
{% endif %} | |
{% endfor %} | |
{% endif %} | |
// Once you have the ID in mind, insert it into your widgets embed markup, a la: | |
p2a embed code: | |
<div id="advocacy-actionwidget" data-domain="your website" data-shorturl="{{ p2a_widget_code }}" style=" width: 500px; height: 815px;"></div> | |
// Because page tags are printed at the bottom of some pages via `{% include "page_tags" with page %}`, we'll want to prevent widget tags from being printed in this list. | |
// In _page_tags.html, wrap the tag forloop with a conditional to exclude tags that include the 'p2a' prefix: | |
{% for tag in page_tags.tags %} | |
{% unless tag.name contains "p2a-" %} <!-- unless tag name contains trigger phrase, print the tag name --> | |
<span class="page-tag"><a href="{{ tag.url }}">{{ tag.name }}</a></span> | |
{% endunless %} | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment