Skip to content

Why is Twig missing and what is the point of classes MVCTemplateViewer and PHPTemplateViewer? #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
padrecedano opened this issue May 8, 2025 · 1 comment

Comments

@padrecedano
Copy link

padrecedano commented May 8, 2025

Thank you for this magnificent framework, which has helped me a lot in learning how PHP works internally.

I recently discovered Symfony, and in the future I intend to enroll my applications in that framework. While I prepare my infrastructure for that migration, I'm using your framework, with which I'm quickly migrating a website that runs on WordPress to pure PHP code.

Forgive this long introduction...

I have a question regarding templates management. On the one hand, I see two classes called MVCTemplateViewer and PHPTemplateViewer. In some methods of those classes, I see code that appears to include or format content based on Twig templates, but Twig doesn't appear anywhere in the project files.

What is the point of these two classes and why is there nothing about Twig in the project?

Incorporating Twig

Since I'm using Twig in my code, I created this class:

<?php

declare(strict_types=1);

namespace Core;

class TwigTemplateViewer implements TemplateViewerInterface
{
	public function render(string $template, array $args = []): string
	{
		static $twig = null;
		if ($twig === null) {
		    $loader = new \Twig\Loader\FilesystemLoader(dirname(__DIR__) . '/App/Views');
		    $options =[
				'cache' => 'compilation_cache',
				'debug' => $_ENV["ENVIRONMENT"]=='dev'
			];
		    $twig = new \Twig\Environment($loader,$options);

		    if($_ENV["ENVIRONMENT"]=='dev')
		    {
		        $twig->addExtension(new \Twig\Extension\DebugExtension());
		    }
		    return $twig->render($template, $args);
	    }
	}
}

Then, I created files with the Twig templates, and it works perfectly in the controllers:

class Products extends Controller
{
    public function __construct(private Product $model)
    {
    }

    public function index(): Response
    {
        $products = $this->model->findAll();
        
        //Note the use of index.html.twig instead of .php index.mvc.php
        return $this->view("Products/index.html.twig", [
            "products" => $products,
            "total" => $this->model->getTotal()
        ]);
    }
    
    // ...
}

How to use Twig in error messages?

Basically, my question is: how can I also integrate Twig to display error messages? Is this a good practice or is it better to display 500 error messages in a .php page, independent of the theme (Twig).

I see that they are displayed from the class ErrorHandler, but how can I incorporate Twig to also display error messages within the application theme, and not in a separate .php file?

@daveh
Copy link
Owner

daveh commented May 9, 2025

I'm glad you found it useful.

Full details of how this framework works is in the accompanying course.

What you've done is the correct way to use Twig with the framework. (I don't think I included any content on that in the course yet but I probably have it on my list)

It's difficult to use Twig in error messages as the error message bypasses a lot of the framework code - I would keep it simple and just use plain PHP in the error messages. This has the added advantage of reducing the possibility of an error in the error handling code itself, which might obscure the original error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants