diff --git a/res/layouts/macros.twig b/res/layouts/macros.twig new file mode 100644 index 0000000..4c901a9 --- /dev/null +++ b/res/layouts/macros.twig @@ -0,0 +1,9 @@ +{% macro youtube(id) %} +
+ +
+{% endmacro %} + +{% macro gist(user, id) %} + +{% endmacro %} diff --git a/res/layouts/page.content.twig b/res/layouts/page.content.twig new file mode 100644 index 0000000..e113a95 --- /dev/null +++ b/res/layouts/page.content.twig @@ -0,0 +1,2 @@ +{% set macros = "{% import 'macros.twig' as macro %}" %} +{{ include(template_from_string(macros ~ page.content)) }} diff --git a/src/Collection/Page/Page.php b/src/Collection/Page/Page.php index 8358b25..0a3c369 100644 --- a/src/Collection/Page/Page.php +++ b/src/Collection/Page/Page.php @@ -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'); } /** diff --git a/src/Renderer/Twig.php b/src/Renderer/Twig.php index eece3a8..46bea6b 100644 --- a/src/Renderer/Twig.php +++ b/src/Renderer/Twig.php @@ -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')); } diff --git a/tests/fixtures/website/content/Macros-and-variables/macros.md b/tests/fixtures/website/content/Macros-and-variables/macros.md new file mode 100644 index 0000000..f2eb53d --- /dev/null +++ b/tests/fixtures/website/content/Macros-and-variables/macros.md @@ -0,0 +1,11 @@ +--- +title: Macros +layout: macro.html +--- +# Macros in Markdown + +## Youtube video +{{ macro.youtube('NaB8JBfE7DY') }} + +## GitHub Gist +{{ macro.gist('Narno', 'fbe791e05b93951ffc1f6abda8ee88f0') }} diff --git a/tests/fixtures/website/content/Macros-and-variables/variables.md b/tests/fixtures/website/content/Macros-and-variables/variables.md new file mode 100644 index 0000000..543477b --- /dev/null +++ b/tests/fixtures/website/content/Macros-and-variables/variables.md @@ -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 }}` diff --git a/tests/fixtures/website/layouts/macro.html.twig b/tests/fixtures/website/layouts/macro.html.twig new file mode 100644 index 0000000..dcab67b --- /dev/null +++ b/tests/fixtures/website/layouts/macro.html.twig @@ -0,0 +1,6 @@ +{% extends '_default/page.html.twig' %} + +{% block content %} +{# page.content #} +{% include page.content_template %} +{% endblock %}