Question:

Using a template from a plugin

Ryder: 5 days ago

I'm building a simple contact form plugin and I would like to be able to provide a default form that the developer can include in his own template.

Most plugins I've seen provide the controller and all the code to process a submitted form but you still have to create your own form.

At the moment I tried 2 methods, using a hook and a template variable. While both work I was wondering if there wasn't a better/cleaner method.

The downside of using a hook is that you can't pass variables to the template (or am I wrong?) and the template variable feels like a misuse of the functionality.

Isn't there a way to use the existing twig import function? Maybe by registering a namespace twig path in the plugin?(https://symfony.com/doc/current/templating/namespaced_paths.html)

Answer:
Madelyn: 5 days ago

You can include a property to your form called $templatePath and users can insert whatever they want in this path with a default value to your own template.

When someone executed your render() function you check if the template file exists in the public template path, if so render it, it not render one of your plugin templates.

If the validation fails, you want to include the validated data with errors to the url manager, so you can access it later. During your render() function you can check if there is data and if it matches your current form element, if so render the data in your url manager instead of the element to display errors.