Skip to content

Commit

Permalink
When view is already a valid HTML page, just displayed it
Browse files Browse the repository at this point in the history
  • Loading branch information
loicsapone committed Apr 24, 2024
1 parent 8a774db commit 3f82b24
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
15 changes: 14 additions & 1 deletion src/Controller/IframeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use IQ2i\StoriaBundle\View\ViewBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Twig\Environment;

Expand All @@ -33,8 +34,20 @@ public function __invoke(Request $request, ?Profiler $profiler): Response
$profiler->disable();
}

$view = $this->viewBuilder->createFromRequest($request);
if (null === $view) {
throw new NotFoundHttpException();
}

$content = $view->getCurrentVariant()->getHtmlContent();

$pos = strripos((string) $content, '</body>');
if (false !== $pos) {
return new Response($content);
}

return new Response($this->twig->render('@IQ2iStoria/iframe.html.twig', [
'view' => $this->viewBuilder->createFromRequest($request),
'content' => $content,
]));
}
}
10 changes: 5 additions & 5 deletions templates/iframe.html.twig
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{# @var view \IQ2i\StoriaBundle\View\Dto\View #}

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>UI Storia</title>
<link rel="apple-touch-icon" sizes="180x180" href="{{ asset('/bundles/iq2istoria/apple-touch-icon.png') }}">
<link rel="icon" type="image/png" sizes="32x32" href="{{ asset('/bundles/iq2istoria/favicon-32x32.png') }}">
<link rel="icon" type="image/png" sizes="16x16" href="{{ asset('/bundles/iq2istoria/favicon-16x16.png') }}">
<meta name="theme-color" content="#ffffff">

{% block stylesheets %}{% endblock %}
{% block javascripts %}{% endblock %}
</head>

<body>
{% block body %}
{{ view.currentVariant.htmlContent|raw }}
{% endblock %}
{% block body %}{{ content|raw }}{% endblock %}
</body>
</html>
5 changes: 4 additions & 1 deletion tests/TestApplication/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return function (RoutingConfigurator $routes) {
$routes->add('home', '/')->controller(TemplateController::class)->defaults(['template' => 'home.html.twig']);
$routes->add('home', '/')
->controller(TemplateController::class)
->defaults(['template' => 'pages/homepage.html.twig']);

$routes->import('@IQ2iStoriaBundle/config/routes.php')->prefix('/storia');

if ('dev' === $routes->env()) {
Expand Down
12 changes: 11 additions & 1 deletion tests/TestApplication/storia/pages/homepage/homepage.html.twig
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
This is a homepage template
{% extends 'base.html.twig' %}

{% block body %}
{% block hero %}
<p>This is the homepage hero</p>
{% endblock %}

{% block content %}
<p>This is the main content of the page</p>
{% endblock %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
Hello world!
{% block body %}{% endblock %}

{% block footer %}
<p>This is a footer</p>
{% endblock %}
</body>
</html>
11 changes: 11 additions & 0 deletions tests/TestApplication/templates/pages/homepage.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}

{% block body %}
{% block hero %}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla viverra dignissim mi, eget egestas mi tincidunt vitae. Nunc nulla neque, sodales at rhoncus eget, pulvinar quis orci. Nulla facilisi.</p>
{% endblock %}

{% block content %}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla viverra dignissim mi, eget egestas mi tincidunt vitae. Nunc nulla neque, sodales at rhoncus eget, pulvinar quis orci. Nulla facilisi. Nam vel orci nec augue scelerisque lacinia. Phasellus pellentesque leo nulla. Ut neque urna, ultricies eu tincidunt at, mollis sagittis dui. Suspendisse ac diam sed lectus mollis dapibus. Donec dui ipsum, scelerisque at rutrum ac, molestie sed ex. Praesent congue laoreet justo at porttitor. Vestibulum non rutrum tortor, ut laoreet libero. Vivamus ut neque at odio finibus venenatis.</p>
{% endblock %}
{% endblock %}

0 comments on commit 3f82b24

Please sign in to comment.