Skip to content

Instantly share code, notes, and snippets.

@brad-jones
Last active February 27, 2016 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brad-jones/62985a9511836d0521b9 to your computer and use it in GitHub Desktop.
Save brad-jones/62985a9511836d0521b9 to your computer and use it in GitHub Desktop.
Foil Nested Blocks Test Case
class AClass
{
public function Bar()
{
echo "Bar";
}
}
class <?= $this->v('name') ?>
{
<?php $this->block('indent', 4) ?>
<?php foreach($this->v('methods') as $method): ?>
<?= $this->insert('method-tpl', $method) ?>
<?php endforeach; ?>
<?php $this->endblock() ?>
}
{
"require": {
"foil/foil": "0.6.4"
}
}
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "bb90ceeab35ca34de68674dffab593de",
"content-hash": "6bddeea81be44d7fb0fbc6a7fd90754c",
"packages": [
{
"name": "aura/html",
"version": "2.4.1",
"source": {
"type": "git",
"url": "https://github.com/auraphp/Aura.Html.git",
"reference": "7f575f35765af41091b85b23a92b168ae0c5765f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/auraphp/Aura.Html/zipball/7f575f35765af41091b85b23a92b168ae0c5765f",
"reference": "7f575f35765af41091b85b23a92b168ae0c5765f",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"aura/di": "~2.0"
},
"type": "library",
"extra": {
"aura": {
"type": "library",
"config": {
"common": "Aura\\Html\\_Config\\Common"
}
},
"branch-alias": {
"dev-develop-2": "2.1.x-dev"
}
},
"autoload": {
"psr-4": {
"Aura\\Html\\": "src/",
"Aura\\Html\\_Config\\": "config/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-2-Clause"
],
"authors": [
{
"name": "Aura.Html Contributors",
"homepage": "https://github.com/auraphp/Aura.Html/contributors"
}
],
"description": "Provides HTML escapers and helpers, including form input helpers.",
"homepage": "https://github.com/auraphp/Aura.Html",
"keywords": [
"Escape",
"escaper",
"escapers",
"escaping",
"form",
"helper",
"helpers",
"html",
"input"
],
"time": "2015-03-27 17:31:31"
},
{
"name": "evenement/evenement",
"version": "v2.0.0",
"source": {
"type": "git",
"url": "https://github.com/igorw/evenement.git",
"reference": "f6e843799fd4f4184d54d8fc7b5b3551c9fa803e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/igorw/evenement/zipball/f6e843799fd4f4184d54d8fc7b5b3551c9fa803e",
"reference": "f6e843799fd4f4184d54d8fc7b5b3551c9fa803e",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
}
},
"autoload": {
"psr-0": {
"Evenement": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Igor Wiedler",
"email": "igor@wiedler.ch",
"homepage": "http://wiedler.ch/igor/"
}
],
"description": "Événement is a very simple event dispatching library for PHP",
"keywords": [
"event-dispatcher",
"event-emitter"
],
"time": "2012-11-02 14:49:47"
},
{
"name": "foil/foil",
"version": "0.6.4",
"source": {
"type": "git",
"url": "https://github.com/FoilPHP/Foil.git",
"reference": "d2b8280a5b9c821fb5e698f576079869965a2e64"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/FoilPHP/Foil/zipball/d2b8280a5b9c821fb5e698f576079869965a2e64",
"reference": "d2b8280a5b9c821fb5e698f576079869965a2e64",
"shasum": ""
},
"require": {
"aura/html": "~2.4",
"evenement/evenement": "~2.0",
"igorw/get-in": "~1.0",
"php": ">=5.4",
"pimple/pimple": "~3.0"
},
"require-dev": {
"brain/monkey": "~1.1",
"mockery/mockery": "0.9.3",
"phpunit/phpunit": "~4.8"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "0.6.x-dev"
}
},
"autoload": {
"psr-4": {
"Foil\\": "src/"
},
"files": [
"inc/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Giuseppe Mazzapica",
"email": "giuseppe.mazzapica@gmail.com",
"homepage": "http://gm.zoomlab.it",
"role": "Developer"
}
],
"description": "PHP template engine for native PHP templates",
"homepage": "https://github.com/Giuseppe-Mazzapica/Foil",
"keywords": [
"template engine",
"templates"
],
"time": "2016-02-22 01:33:41"
},
{
"name": "igorw/get-in",
"version": "v1.0.3",
"source": {
"type": "git",
"url": "https://github.com/igorw/get-in.git",
"reference": "170ded831f49abc6a6061f655aba9bdbcf7b8111"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/igorw/get-in/zipball/170ded831f49abc6a6061f655aba9bdbcf7b8111",
"reference": "170ded831f49abc6a6061f655aba9bdbcf7b8111",
"shasum": ""
},
"require": {
"php": ">=5.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"autoload": {
"files": [
"src/get_in.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Igor Wiedler",
"email": "igor@wiedler.ch"
}
],
"description": "Functions for for hash map (assoc array) traversal.",
"keywords": [
"assoc-array",
"hash-map"
],
"time": "2014-12-15 23:03:51"
},
{
"name": "pimple/pimple",
"version": "v3.0.2",
"source": {
"type": "git",
"url": "https://github.com/silexphp/Pimple.git",
"reference": "a30f7d6e57565a2e1a316e1baf2a483f788b258a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/silexphp/Pimple/zipball/a30f7d6e57565a2e1a316e1baf2a483f788b258a",
"reference": "a30f7d6e57565a2e1a316e1baf2a483f788b258a",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
}
},
"autoload": {
"psr-0": {
"Pimple": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
}
],
"description": "Pimple, a simple Dependency Injection Container",
"homepage": "http://pimple.sensiolabs.org",
"keywords": [
"container",
"dependency injection"
],
"time": "2015-09-11 15:10:35"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}
<?php
require_once __DIR__.'/vendor/autoload.php';
$engineConfig = ['folders' => [__DIR__], 'autoescape' => false];
$engine = Foil\Foil::boot($engineConfig)->engine();
$engine->registerBlock('indent', function($src, $level, $type = " ")
{
$indentedSrc = '';
$dealtWithFirstLine = false;
foreach (explode("\n", $src) as $lineNo => $line)
{
if (!$dealtWithFirstLine)
{
if (!ctype_space($line) && $line != "")
{
if ($lineNo > 0)
{
$indentedSrc .= str_repeat($type, $level).trim($line)."\n";
}
else
{
$indentedSrc .= trim($line)."\n";
}
$dealtWithFirstLine = true;
}
else
{
$indentedSrc .= "\n";
}
}
else
{
$indentedSrc .= str_repeat($type, $level).rtrim($line)."\n";
}
}
return $indentedSrc;
});
return $engine;
namespace Foo
{
<?php $this->block('indent', 4) ?>
<?= $this->insert('class-tpl',
[
'name' => 'Bar',
'methods' =>
[
[
'name' => 'Foo',
'visibility' => 'public',
'body' => 'echo "Foo";'
]
]
]) ?>
<?php $this->endblock() ?>
}
<?php
$engine = require __DIR__.'/engine.php';
file_put_contents('fails-generated.php', $engine->render('fails-tpl'));
<?= $this->v('visibility') ?> function <?= $this->v('name') ?>(<?= $this->v('arguments') ?>)
{
<?php $this->block('indent', 4) ?>
<?= $this->v('body') ?>
<?php $this->endblock() ?>
}
<?php
$engine = require __DIR__.'/engine.php';
file_put_contents('success-not-nested-generated.php', $engine->render('success-tpl'));
namespace Foo
{
<?php $this->block('indent', 4) ?>
<?= $this->insert('a-class-tpl') ?>
<?php $this->endblock() ?>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment