<p>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.</p>
<p>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 <code>myPluginHtml()</code> function that I can call in my Twig template and that returns the HTML code required by your plugin:</p>
<pre><code>{{ myPluginHtml() }}
</code></pre>
<p>Look at <a href="https://craftcms.com/docs/4.x/extend/extending-twig.html" rel="nofollow noreferrer">Extending Twig</a> for details. You can still write your plugin templates in Twig, but put them in the plugin's template folder, not the <em>site's</em> template folder. See <a href="https://craftcms.com/docs/4.x/extend/template-roots.html" rel="nofollow noreferrer">template roots</a> for more details.</p>

CC BY-SA 4.0
2022-12-15T11:21:42.450Z
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
Kishan Mistry
CC BY-SA 4.0
2022-12-15T14:40:25.980Z
@KishanMistry That sounds like a good solution! If your question is solved, don't forget to mark the answer accepted. Thanks!
MoritzLost
CC BY-SA 4.0
2022-12-14T13:51:19.690Z
2022-12-14T13:51:19.690Z
MoritzLost
<p>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.</p>
<p>The template file will have html code <code>&lt;div id=&quot;myDivId&quot;&gt;&lt;/div&gt;</code>, that template will be appear as website page, which can be open by click on one of menu item on top header.</p>
<p>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.</p>

<craft3><plugin-development>
Create a template for front end when plugin install
# Create a template for front end when plugin install

(Asked by: username_0 on 2022-12-14)

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 , 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.

**Tags:** craft3, plugin-development

---

## 1 answers

### Answer by username_1 on 2022-12-14. Score: 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.

> **Comments:**
> **username_0**: 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  
> **username_1**: @KishanMistry That sounds like a good solution! If your question is solved, don't forget to mark the answer accepted. Thanks!  

---

