Skip to content
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

Missing good examples of configuration template paths for laminas-view in mezzio #112

Open
tempfirstuser opened this issue Apr 6, 2022 · 3 comments
Labels
Bug Something isn't working Documentation

Comments

@tempfirstuser
Copy link

tempfirstuser commented Apr 6, 2022

Hi guys!
I'm trying to integrate mezzio/mezzio-laminasviewrenderer into our mezzio project which was only api oriented. I need laminas-view templates to generate html code for emails.
I have read documentation from this page https://docs.mezzio.dev/mezzio/v3/features/template/laminas-view/
And there is an example of rendering html like this one

$content = $renderer->render('blog/entry', [
    'layout' => 'layout::blog',
    'entry'  => $entry,
]);

What is 'blog/entry'???
What is 'layout::blog'???
How to configure them and how to attach them to real phtml files?
I was trying to understand source code from /vendor/mezzio/mezzio-laminasviewrenderer/src/LaminasViewRendererFactory.php . It contains this example:

 * <code>
 * 'templates' => [
 *     'extension' => 'default template file extension',
 *     'layout' => 'name of layout view to use, if any',
 *     'map'    => [
 *         // template => filename pairs
 *     ],
 *     'paths'  => [
 *         // namespace / path pairs
 *         //
 *         // Numeric namespaces imply the default/main namespace. Paths may be
 *         // strings or arrays of string paths to associate with the namespace.
 *     ],
 * ]
 * </code>

Can somebody show me and example of configuring 'paths'?
Looks like it should be like this:

'templates' => [
      'paths'  => [
	      [
		      'blog/entry' => '/path/to/blog/entry.phtml'
	      ]
      ],
]

or

'templates' => [
      'paths'  => [
	      'some::namespace' => [
		      'blog/entry' => '/path/to/blog/entry.phtml'
	      ]
      ],
  ]

Am I correct? Is there a good example with source code how to use laminas-view with mezzio?

@tempfirstuser tempfirstuser added the Bug Something isn't working label Apr 6, 2022
@tempfirstuser tempfirstuser changed the title Missing good examples of configuring template paths for laminas-view Missing good examples of configuration template paths for laminas-view Apr 6, 2022
@tempfirstuser tempfirstuser changed the title Missing good examples of configuration template paths for laminas-view Missing good examples of configuration template paths for laminas-view in mezzio Apr 6, 2022
@froschdesign
Copy link
Member

The skeleton application contains examples for the configurations:
https://github.com/mezzio/mezzio-skeleton/tree/3.12.x/src/MezzioInstaller/Resources/src

@tempfirstuser
Copy link
Author

@froschdesign I used this $renderer

$renderer = new LaminasViewRenderer();

But when I've made some investigation of mezzio/mezzio-skeleton package I've found example of using TemplateRendererInterface and passing it from handler factory like this

$container->get(TemplateRendererInterface::class);

Then later in handler I was able to use renderer like this

    public function __construct(private TemplateRendererInterface $templateRenderer)
    {}

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $content = $this->templateRenderer->render('email::comment-created.phtml', [
            'title' => 'Some title',
        ]);
    }

It works for me!
Also here is my config from ConfigProvider.php

    public function getTemplates(): array
    {
        return [
//            'extension' => 'phtml',
//            'layout'    => 'layout::default',
            'paths'     => [
                 'email' => [
                     __DIR__ . '/../Template/Email'
                 ],
                'layout' => [
                    __DIR__ . '/../Template'
                ]

            ],
        ];
    }

Inside of /Template/Email I have file comment-created and inside of /Template/ I have default.phtml.

Thank you again!

@froschdesign
Copy link
Member

@tempfirstuser

@froschdesign I used this $renderer

$renderer = new LaminasViewRenderer();

This creates a separate and isolated renderer and there is no magic, so the configuration of your application is not used here.

But when I've made some investigation of mezzio/mezzio-skeleton package I've found example of using TemplateRendererInterface and passing it from handler factory like this

$container->get(TemplateRendererInterface::class);

And this gives you the prepared renderer of mezzio-laminasviewrenderer:

  • Mezzio\Template\TemplateRendererInterface::class is an alias for Mezzio\LaminasView\LaminasViewRenderer::class
  • Mezzio\LaminasView\LaminasViewRenderer::class is created by the factory Mezzio\LaminasView\LaminasViewRendererFactory::class
  • this factory uses the configuration of your application:
class LaminasViewRendererFactory
{
    public function __invoke(ContainerInterface $container): LaminasViewRenderer
    {
        $config = $container->has('config') ? $container->get('config') : [];
        $config = $config['templates'] ?? [];

        // …
    }
}

https://github.com/mezzio/mezzio-laminasviewrenderer/blob/df463cc65105f54418ce2dfa44be6b9918e7a45b/src/LaminasViewRendererFactory.php#L53-L54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Documentation
Projects
None yet
Development

No branches or pull requests

2 participants