Skip to content

Instantly share code, notes, and snippets.

@blackandred
Last active December 25, 2016 13:02
Show Gist options
  • Save blackandred/3f7146d0d6ec662ae9fa9a043bffbfc2 to your computer and use it in GitHub Desktop.
Save blackandred/3f7146d0d6ec662ae9fa9a043bffbfc2 to your computer and use it in GitHub Desktop.
Twig - HTML Meta / SEO tags # https://github.com/Wolnosciowiec
{% import "SEOTags.html.twig" as SEOTags %}
{% block header_meta %}
{{
SEOTags.draw({
'title': 'Hello World',
'description': 'Hi, this is a test',
'keywords': ['first', 'second', 'third'],
'image': 'http://.../image.jpg'
})
}}
{% endblock %}
{#
# Renders meta tags for every page
# --------------------------------
#
# A part of an anarchist portal - wolnosciowiec.net
*
* Wolnościowiec is a project to integrate the movement
* of people who strive to build a society based on
* solidarity, freedom, equality with a respect for
* individual and cooperation of each other.
*
* We support human rights, animal rights, feminism,
* anti-capitalism (taking over the production by workers),
* anti-racism, and internationalism. We negate
* the political fight and politicians at all.
*
* http://wolnosciowiec.net/free-software
*
* License: LGPLv3
* Author: Wolnościowiec team
#}
{% macro draw(tags) %}
{# == Generic tags == #}
<meta property="twitter:site" content="@wolnosciowiec" />
<meta property="og:site_name" content="Wolnościowiec" />
{# == Title == #}
{% if tags.title is defined %}
<title>{{ tags.title|striptags }}</title>
<meta property="og:title" content="{{ tags.title|striptags }}" />
{% endif %}
{# == URL == #}
{% if tags.url is defined %}
<meta property="og:url" content="{{ tags.url }}" />
<meta property="twitter:url" content="{{ tags.url }}" />
<link rel="canonical" href="{{ tags.url }}" />
{% endif %}
{# == Description == #}
{% if tags.description is defined %}
{% set description = tags.description|striptags|truncate(350, true)|striptags|replace({"\n": " "}) %}
<meta name="description" content="{{ description }}" />
<meta property="og:description" content="{{ description }}" />
<meta property="twitter:description" content="{{ description }}" />
{% endif %}
{# == Type == #}
{% if tags.type is defined %}
<meta property="og:type" content="{{ tags.type }}" />
{% endif %}
{# == Section == #}
{% if tags.section is defined %}
<meta property="article:section" content="{{ tags.section }}" />
{% endif %}
{# == Published time == #}
{% if tags.published_time is defined %}
<meta property="article:published_time" content="{{ tags.published_time.format('Y-m-d\TH:i:sO') }}" />
{% endif %}
{# == Image == #}
{% if tags.image is defined %}
<meta property="og:image" content="{{ tags.image }}" />
<meta property="twitter:image" content="{{ tags.image }}" />
{% endif %}
{# == Keywords == #}
{% set keywords = "default_keywords"|trans({}, 'main')|split(',') %}
{% if tags.keywords is defined %}
{% set keywords = keywords|merge(tags.keywords) %}
{% endif %}
{# Search engines are ignoring keywords, but it may be helpful for some web catalogues? #}
<meta name="keywords" content="{{ keywords|join(', ')|striptags }}" />
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment