<p>You can inject HTML to the end of the body using <a href="https://www.yiiframework.com/doc/guide/2.0/en/structure-views#view-events" rel="noreferrer">View Events</a>:</p>
<pre class="lang-php prettyprint-override"><code>use craft\web\View;
use yii\base\Event;

Event::on(
    View::class,
    View::EVENT_END_BODY,
    function() { echo 'Foobar'; }
);
</code></pre>
<p>The output of this function will appear wherever the <a href="https://craftcms.com/docs/3.x/dev/functions.html#endbody" rel="noreferrer"><code>endBody()</code> function</a> is placed in the <code>index.twig</code>. The event will even work if the template doesn't contain this function, because Craft will dynamically insert it before the <code>&lt;/body&gt;</code> end tag.</p>
<p>If you want to register CSS or JS assets, use the methods <code>registerCssFile</code> and <code>registerJsFile</code> on <code>\Craft::$app-&gt;view</code> instead. There's also a <a href="https://docs.craftcms.com/api/v3/craft-web-view.html#method-registerhtml" rel="noreferrer"><code>registerHtml</code> method</a> that achieves the same as the hook above.</p>
<hr />
<p>While there are valid use-cases for injecting HTML, it's a terrible idea for most plugins. As a developer, I won't touch your plugin if it forcibly injects any code into my frontend template without letting me have a say in it. Instead, your plugin could expose a custom variable or function to Twig and allow me to call it myself. This way, I have control over if and where your injected content is placed. Just something to keep in mind as an alternative to the hooks above.</p>

CC BY-SA 4.0
2022-01-04T11:34:11.500Z
We are trying to override the  website index.twig file considering it as home page. We are trying to craete a plugin that will add a layout to home page like some banner with content is it possible with this ?
DTS
CC BY-SA 4.0
2022-01-04T11:40:36.793Z
@DotSquares Yes, but you won't get a step-by-step guide with code to copy and paste here. Please read the documentation articles I linked and consider the caveat below the line. Then build your plugin as far as you can and if you hit specific problems, post it as a question.
MoritzLost
CC BY-SA 4.0
2022-01-05T10:51:17.250Z
Thanks for the reply but with this method it won't override the homepage code it will inject my code with users' code at the end of the body I want to create a 404 page kind of plugin that will show some text to non logged in users.
DTS
CC BY-SA 4.0
2022-01-05T10:57:28.617Z
@DotSquares So do you want to overwrite the homepage or the 404 page? It sounds like you're going down the wrong rabbit hole. Maybe post post some more information in what you're trying to do with this plugin, I'm sure there is a better solution.
MoritzLost
CC BY-SA 4.0
2022-01-05T11:00:30.807Z
I am trying to create an under construction plugin if the user enables the maintenance mode in the plugin the homepage will show some maintenance template text to non-logged users. just like a 404 template page layout
DTS
CC BY-SA 4.0
2022-01-04T10:21:14.113Z
2022-01-04T10:21:14.113Z
MoritzLost
<p>Want to override index.twig content through a custom plugin. I want to create a plugin that appends some content on website homepage. How to Achieve this any reference will be helpful</p>

<craft3><plugin-development><index>
How to override homepage content through plugin?
# How to override homepage content through plugin?

(Asked by: username_0 on 2022-01-04)

Want to override index.twig content through a custom plugin. I want to create a plugin that appends some content on website homepage. How to Achieve this any reference will be helpful

**Tags:** craft3, plugin-development, index


---

## 1 answers

### Answer by username_1 on 2022-01-04. Score: 5

You can inject HTML to the end of the body using View Events:
use craft\web\View;
use yii\base\Event;

Event::on(
    View::class,
    View::EVENT_END_BODY,
    function() { echo 'Foobar'; }
);

The output of this function will appear wherever the endBody() function is placed in the index.twig. The event will even work if the template doesn't contain this function, because Craft will dynamically insert it before the  end tag.
If you want to register CSS or JS assets, use the methods registerCssFile and registerJsFile on \Craft::$app->view instead. There's also a registerHtml method that achieves the same as the hook above.

While there are valid use-cases for injecting HTML, it's a terrible idea for most plugins. As a developer, I won't touch your plugin if it forcibly injects any code into my frontend template without letting me have a say in it. Instead, your plugin could expose a custom variable or function to Twig and allow me to call it myself. This way, I have control over if and where your injected content is placed. Just something to keep in mind as an alternative to the hooks above.

> **Comments:**
> **username_0**: We are trying to override the  website index.twig file considering it as home page. We are trying to craete a plugin that will add a layout to home page like some banner with content is it possible with this ?  
> **username_1**: @DotSquares Yes, but you won't get a step-by-step guide with code to copy and paste here. Please read the documentation articles I linked and consider the caveat below the line. Then build your plugin as far as you can and if you hit specific problems, post it as a question.  
> **username_0**: Thanks for the reply but with this method it won't override the homepage code it will inject my code with users' code at the end of the body I want to create a 404 page kind of plugin that will show some text to non logged in users.  
> **username_1**: @DotSquares So do you want to overwrite the homepage or the 404 page? It sounds like you're going down the wrong rabbit hole. Maybe post post some more information in what you're trying to do with this plugin, I'm sure there is a better solution.  
> **username_0**: I am trying to create an under construction plugin if the user enables the maintenance mode in the plugin the homepage will show some maintenance template text to non-logged users. just like a 404 template page layout  

---

