1

I have a plugin which embed a forum to front end website. For that i need a .twig template file to be created when craft admin install my plugin.

The template file will have html code <div id="myDivId"></div>, that template will be appear as website page, which can be open by click on one of menu item on top header.

I have research a lot but didn't find a way to create template for front end. And that template must load on front end side only when user click on menu item linked with that template.

1 Answer 1

1

I wouldn't recommend this, as you're taking control away from the developer by forcing a specific template to exist. Craft allows for complete freedom in the way I structure my templates. As a site developer, if I install your plugin and see it creating some random template in my templates folder, I will uninstall your plugin immediately and never touch it again.

Instead, you can provide a function to Twig that I can include in my own code, wherever I choose to. This will allow me to integrate your plugin's output into my own templates on my own terms – for example, by only showing your plugin's output in specific circumstances. For example, you can provide a function myPluginHtml() function that I can call in my Twig template and that returns the HTML code required by your plugin:

{{ myPluginHtml() }}

Look at Extending Twig for details. You can still write your plugin templates in Twig, but put them in the plugin's template folder, not the site's template folder. See template roots for more details.

2
  • MoritzLost, You are right. I don't want to create a physical template to template forlder. I have created template folder in my plugin and render it to the front end and set route using URLManager and EVENT_REGISTER_SITE_URL_RULES Dec 15, 2022 at 11:21
  • @KishanMistry That sounds like a good solution! If your question is solved, don't forget to mark the answer accepted. Thanks!
    – MoritzLost
    Dec 15, 2022 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.