3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php interface TemplateInterface { } class HtmlTemplateEngine { function process(HtmlTemplate $template) { $outerTemplate = $template->getOuterTemplate(); $templateContent = $template->getTemplateContent(); if (!is_null($outerTemplate)) { $outerTemplate->setInnerTemplateContent($templateContent); return $this->process($outerTemplate); } return $templateContent; } } abstract class HtmlTemplate implements TemplateInterface { private $outerTemplate; private $innerTemplateContent; abstract public function render(); public function getTemplateContent() { ob_start(); $this->render(); return ob_get_clean(); } public function getOuterTemplate() { return $this->outerTemplate; } public function setOuterTemplate(HtmlTemplate $template) { $this->outerTemplate = $template; } public function getInnerTemplateContent() { return $this->innerTemplateContent; } public function setInnerTemplateContent($templateContent) { $this->innerTemplateContent = $templateContent; } } class MyOuterHtmlTemplate extends HtmlTemplate { public function render() { ?> <!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <h1>This is My Outer Template</h1> <article> <?= $this->getInnerTemplateContent(); ?> </article> </body> </html> <?php } } class MyInnerHtmlTemplate extends HtmlTemplate { private $name; public function render() { ?> <h1>This is My Inner Template</h1> <section> <h2>Welcome <?= $this->name; ?></h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam quis dictum urna, at gravida ipsum. In sed bibendum tortor, quis hendrerit ante. Sed auctor imperdiet pretium. Praesent metus nisi, molestie in diam et, hendrerit accumsan nibh. Sed elementum ante a adipiscing rhoncus. Nullam aliquam fringilla mi, nec iaculis diam lobortis vel. Aliquam erat volutpat. Interdum et malesuada fames ac ante ipsum primis in faucibus.</p> <p>Sed et dolor nec dui aliquam pretium. Fusce id arcu metus. Suspendisse ultrices risus vel libero iaculis cursus. Phasellus adipiscing adipiscing tincidunt. Nunc metus eros, imperdiet nec ultricies a, dictum vitae justo. Donec id turpis rhoncus, viverra magna vel, vehicula tellus. Nam in pretium dolor. In at quam volutpat velit aliquam malesuada vitae eu turpis. Nam eget blandit tellus. In rutrum massa enim. Praesent et nisl auctor, pharetra sapien in, pretium ligula. Integer quis turpis elementum mauris cursus ornare id vitae enim. Duis fermentum quis justo et rhoncus. In venenatis lorem et congue ultricies. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse vel ipsum imperdiet, vestibulum est sit amet, viverra purus.</p> </section> <?php } public function __construct($name) { $this->name = $name; $this->setOuterTemplate(new MyOuterHtmlTemplate()); } } $templateEngine = new HtmlTemplateEngine(); echo $templateEngine->process(new MyInnerHtmlTemplate('Robert Noodles'));
Output for 5.4.0 - 7.1.0
<!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <h1>This is My Outer Template</h1> <article> <h1>This is My Inner Template</h1> <section> <h2>Welcome Robert Noodles</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam quis dictum urna, at gravida ipsum. In sed bibendum tortor, quis hendrerit ante. Sed auctor imperdiet pretium. Praesent metus nisi, molestie in diam et, hendrerit accumsan nibh. Sed elementum ante a adipiscing rhoncus. Nullam aliquam fringilla mi, nec iaculis diam lobortis vel. Aliquam erat volutpat. Interdum et malesuada fames ac ante ipsum primis in faucibus.</p> <p>Sed et dolor nec dui aliquam pretium. Fusce id arcu metus. Suspendisse ultrices risus vel libero iaculis cursus. Phasellus adipiscing adipiscing tincidunt. Nunc metus eros, imperdiet nec ultricies a, dictum vitae justo. Donec id turpis rhoncus, viverra magna vel, vehicula tellus. Nam in pretium dolor. In at quam volutpat velit aliquam malesuada vitae eu turpis. Nam eget blandit tellus. In rutrum massa enim. Praesent et nisl auctor, pharetra sapien in, pretium ligula. Integer quis turpis elementum mauris cursus ornare id vitae enim. Duis fermentum quis justo et rhoncus. In venenatis lorem et congue ultricies. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse vel ipsum imperdiet, vestibulum est sit amet, viverra purus.</p> </section> </article> </body> </html>
Output for 5.0.0 - 5.3.29
<!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <h1>This is My Outer Template</h1> <article> <?= $this->getInnerTemplateContent(); ?> </article> </body> </html>
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING in /in/RlACG on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING in /in/RlACG on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/RlACG on line 3
Process exited with code 255.