Answers.Body: <p>With your plugin you can check for a front-end request, and if so inject an <a href="https://docs.craftcms.com/v3/extend/asset-bundles.html" rel="nofollow noreferrer">asset bundle</a></p>
  
  <pre><code>
  public function init()
  {
      // Check if front end request
  
      $request = Craft::$app-&gt;getRequest();
      if (
          $this-&gt;isInstalled
          &amp;&amp; !$request-&gt;isCpRequest
          &amp;&amp; !$request-&gt;isConsoleRequest
       ) {
          $this-&gt;registerAssetBundles();
      }
  }
  
  protected function registerAssetBundles()
  {
      // Include CSS on front end
      $view = Craft::$app-&gt;getView();
      $view-&gt;registerAssetBundle('vendor\\plugin-handle\\assetbundles\\YourpluginFrontendAssets');
  }
  
  </code></pre>
  
  <p>I'm not sure it's possible to inject html, I believe you would need to write a <a href="https://docs.craftcms.com/v3/extend/extending-twig.html#extend-the-global-craft-variable" rel="nofollow noreferrer">new variable</a> to render your html.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-12T13:16:15.863Z
Answers.Text: This worked like a charm. Thank you kind sir. :)
  Small question, if I had to add attributes to my js and css files, how to do that in the asset bundle?
Answers.UserDisplayName: Rohan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-12T13:25:28.857Z
Answers.Text: $jsOptions Figured it out :)
Answers.UserDisplayName: Rohan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-09T14:09:22.113Z
Answers.LastActivityDate: 2020-06-09T14:15:36.493Z
Answers.LastEditDate: 2020-06-09T14:15:36.493Z
Answers.UserDisplayName: dmatthams
Answers.Body: <p>You should be able to listen for the <a href="https://docs.craftcms.com/api/v3/craft-web-view.html#event-before-render-page-template" rel="nofollow noreferrer">before render event</a> on the view class.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-08T21:38:00.440Z
Answers.LastActivityDate: 2020-06-08T21:38:00.440Z
Answers.UserDisplayName: billythekid
Answers.Body: <p>If you are able to place a hook tag in all templates then you could <a href="https://docs.craftcms.com/v3/extend/template-hooks.html#app" rel="nofollow noreferrer">register a new template hook method</a> in your plugin.</p>
  
  <p>Twig:</p>
  
  <pre><code>{% hook 'my-custom-hook-name' %}
  </code></pre>
  
  <p>Your plugin:</p>
  
  <pre class="lang-php prettyprint-override"><code>Craft::$app-&gt;view-&gt;hook('my-custom-hook-name', function(array &amp;$context) {
      $context['foo'] = 'bar';
      return '&lt;p&gt;Hey!&lt;/p&gt;';
  });
  </code></pre>
  
  <p>For injecting CSS or JS you would use </p>
  
  <pre class="lang-php prettyprint-override"><code>\Craft::$app-&gt;getView()-&gt;registerCssFile("file.css");
  \Craft::$app-&gt;getView()-&gt;registerJsFile("file.js");
  </code></pre>
  
  <p>If you can't modify the template files to place a hook tag there you could dynamically generate the markup in your JS file, depending on your use case.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-08T18:22:10.100Z
Answers.LastActivityDate: 2020-06-08T18:22:10.100Z
Answers.UserDisplayName: Johannes
Body: <p>I am developing a plugin for Craft 3. And I am trying to add some content to all pages in the CMS using this plugin. How can I do that? Is there a method or class call I can use for this? I am not sure where this is mentioned in the docs either.</p>
  
  <p>Basically I want to add a few lines of HTML, CSS and JS in all the pages of the CMS if my plugin is installed and enabled in that Craft installation. Any ideas, anyone?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-06-08T18:22:17.100Z
Comments.Text: Could you explain your use case for this? There could be a more suitable solution for this.
Comments.UserDisplayName: Johannes
Tags: <plugin-development><craft3><php>
Title: How to add content to pages using a plugin?
ThreadText: # How to add content to pages using a plugin?
  
  (Asked by: username_0 on 2020-06-08)
  
  I am developing a plugin for Craft 3. And I am trying to add some content to all pages in the CMS using this plugin. How can I do that? Is there a method or class call I can use for this? I am not sure where this is mentioned in the docs either.
  
  Basically I want to add a few lines of HTML, CSS and JS in all the pages of the CMS if my plugin is installed and enabled in that Craft installation. Any ideas, anyone?
  
  **Tags:** plugin-development, craft3, php
  
  > **Comments:**
  > **username_1**: Could you explain your use case for this? There could be a more suitable solution for this.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2020-06-09. Score: 1 | Accepted answer
  
  With your plugin you can check for a front-end request, and if so inject an [asset bundle](https://docs.craftcms.com/v3/extend/asset-bundles.html)
  
  ```
  
  public function init()
  {
      // Check if front end request
  
      $request = Craft::$app->getRequest();
      if (
          $this->isInstalled
          && !$request->isCpRequest
          && !$request->isConsoleRequest
       ) {
          $this->registerAssetBundles();
      }
  }
  
  protected function registerAssetBundles()
  {
      // Include CSS on front end
      $view = Craft::$app->getView();
      $view->registerAssetBundle('vendor\\plugin-handle\\assetbundles\\YourpluginFrontendAssets');
  }
  
  ```
  
  I'm not sure it's possible to inject html, I believe you would need to write a [new variable](https://docs.craftcms.com/v3/extend/extending-twig.html#extend-the-global-craft-variable) to render your html.
  
  > **Comments:**
  > **username_0**: This worked like a charm. Thank you kind sir. :) Small question, if I had to add attributes to my js and css files, how to do that in the asset bundle?  
  > **username_0**: $jsOptions Figured it out :)  
  
  ---
  
  ### Answer by username_3 on 2020-06-08. Score: 1
  
  You should be able to listen for the [before render event](https://docs.craftcms.com/api/v3/craft-web-view.html#event-before-render-page-template) on the view class.
  
  
  ---
  
  ### Answer by username_1 on 2020-06-08. Score: 1
  
  If you are able to place a hook tag in all templates then you could [register a new template hook method](https://docs.craftcms.com/v3/extend/template-hooks.html#app) in your plugin.
  
  Twig:
  
  ```
  {% hook 'my-custom-hook-name' %}
  
  ```
  
  Your plugin:
  
  ```php
  Craft::$app->view->hook('my-custom-hook-name', function(array &$context) {
      $context['foo'] = 'bar';
      return '<p>Hey!</p>';
  });
  
  ```
  
  For injecting CSS or JS you would use
  
  ```php
  \Craft::$app->getView()->registerCssFile("file.css");
  \Craft::$app->getView()->registerJsFile("file.js");
  
  ```
  
  If you can't modify the template files to place a hook tag there you could dynamically generate the markup in your JS file, depending on your use case.
  
  
  ---
  
  