You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2024-04-24 15:17:00

scarletTornado
Member
Registered: 2024-04-12
Posts: 19

[SOLVED]Plugin development: use TemplateRenderer

I've been using TemplateRenderer with the twig file from the appliance class to display the form for my custom plugin. I wonder if there is a way to indicate the class to use twig files outside the "templates" folder. Tried something like the following but it did not work.

public function showForm($ID, array $options = []) {
        $this->initForm($ID, $options);
        TemplateRenderer::getInstance()->display('../../plugins/assetlist/pages/assetlist.html.twig', [
            'item'   => $this,
            'params' => $options,
        ]);
        return true;
}

Last edited by scarletTornado (2024-04-29 15:13:19)

Offline

#2 2024-04-24 15:31:58

btry
Moderator
Registered: 2015-10-01
Posts: 593

Re: [SOLVED]Plugin development: use TemplateRenderer

Hi

To specify the twig file to use when calling TemplateRenderer::display() you are free to organize your twig files in the way you want.  The twig files MUST be located in a templates/ subdirectory of the plugin.

I see that your plugin is called assertlist (ths is the folter under the plugins/ directory. Then you must call TemplateRendeer::display() this way:

TemplateRenderer::getInstance()->display('@assetlist/pages/assetlist.html.twig', [...]);

@assertlist is resolved into the path to the plugin + template/ subdirectory.

I hope this will help you. It's great to see you using twig templates!


No support with MP - Always run an upgrade task on a testing instance before running it on production! Always backup before any upgrade task!

Offline

#3 2024-04-24 16:55:27

scarletTornado
Member
Registered: 2024-04-12
Posts: 19

Re: [SOLVED]Plugin development: use TemplateRenderer

Thank you

Offline

#4 2024-04-24 19:46:02

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,430
Website

Re: [SOLVED]Plugin development: use TemplateRenderer

Move your assetlist.html.twig file from the pages folder to a "templates" folder.
Then, use the path "@assetlist/assetlist.html.twig" for the template. The "@assetlist" prefix tells Twig to look at the plugin "assetlist" template folder.
You can see how GLPI sets this up in the constructor of TemplateRenderer.
It enumerates all active plugins and adds new paths to the Twig filesystem loader, assigning each plugin path its own namespace (the part with the @).


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

Board footer

Powered by FluxBB