Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.

Commit

Permalink
[WIP] Macros (shortcodes) feature. (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Nov 8, 2018
1 parent e660fc2 commit d21ac52
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions res/layouts/macros.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% macro youtube(id) %}
<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
<iframe src="//www.youtube.com/embed/{{ id }}?autoplay=1" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" allowfullscreen title="YouTube Video"></iframe>
</div>
{% endmacro %}

{% macro gist(user, id) %}
<script type="application/javascript" src="//gist.github.com/{{ user }}/{{ id }}.js"></script>
{% endmacro %}
2 changes: 2 additions & 0 deletions res/layouts/page.content.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% set macros = "{% import 'macros.twig' as macro %}" %}
{{ include(template_from_string(macros ~ page.content)) }}
1 change: 1 addition & 0 deletions src/Collection/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function __construct(SplFileInfo $file = null)
}
$this->setVariable('virtual', $this->virtual);
$this->setVariable('published', true);
$this->setVariable('content_template', 'page.content.twig');
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Renderer/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct($templatesPath, $config)
// add extensions
$this->twig->addExtension(new \Twig_Extension_Debug());
$this->twig->addExtension(new TwigExtension($config->getOutputPath()));
$this->twig->addExtension(new \Twig_Extension_StringLoader());
$this->twig->getExtension('Twig_Extension_Core')->setDateFormat($config->get('site.date.format'));
$this->twig->getExtension('Twig_Extension_Core')->setTimezone($config->get('site.date.timezone'));
}
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/website/content/Macros-and-variables/macros.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Macros
layout: macro.html
---
# Macros in Markdown

## Youtube video
{{ macro.youtube('NaB8JBfE7DY') }}

## GitHub Gist
{{ macro.gist('Narno', 'fbe791e05b93951ffc1f6abda8ee88f0') }}
13 changes: 13 additions & 0 deletions tests/fixtures/website/content/Macros-and-variables/variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Variables
layout: macro.html
var1: 'var 1'
---
# Variables in Markdown

## Set and show
{% set foo = 'bar' %}
foo: `{{ foo }}`

## FM variable
page.var1: `{{ page.var1 }}`
6 changes: 6 additions & 0 deletions tests/fixtures/website/layouts/macro.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends '_default/page.html.twig' %}

{% block content %}
{# page.content #}
{% include page.content_template %}
{% endblock %}

0 comments on commit d21ac52

Please sign in to comment.