<?php
$path = __DIR__ . '/templates/';
$template = <<<ND
{% extends "base.html.twig" %}
{% block body %}
<h5>First macro call</h5>
{{ _self.foo(1) }}
<h5>Second macro call</h5>
{{ _self.foo(2, "Free ElePHPants") }}
{% endblock %}
{% macro foo(number, optional) %}
Content no. {{ number }} -
{{ optional|default("No optional addition") }}
{% endmacro %}
ND;
$name = 'macro.html.twig';
$loaders = [
new Twig\Loader\ArrayLoader([$name => $template]),
new Twig\Loader\FilesystemLoader($path),
];
$loader = new Twig\Loader\ChainLoader($loaders);
$twig = new Twig\Environment($loader);
echo $twig->render($name);