Answers.Body: <p>You need to include your template roots <a href="https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module#2-the-module-class" rel="noreferrer">Enhancing a Craft CMS 3 Website with a Custom Module</a></p>
  
  <pre><code>public function __construct($id, $parent = null, array $config = [])
  {
      Craft::setAlias('@modules/sitemodule', $this-&gt;getBasePath());
      $this-&gt;controllerNamespace = 'modules\sitemodule\controllers';
  
      // Base template directory
      Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) {
          if (is_dir($baseDir = $this-&gt;getBasePath().DIRECTORY_SEPARATOR.'templates')) {
              $e-&gt;roots[$this-&gt;id] = $baseDir;
          }
      });
  
      // Set this as the global instance of this module class
      static::setInstance($this);
  
      parent::__construct($id, $parent, $config);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-02T08:11:57.800Z
Answers.Text: Thank you Robin for your swift reply. I've added the code to the main module file, but I'm not sure what path I should use to render the template. Do you have an idea?
Answers.UserDisplayName: kant312
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-02T08:13:51.980Z
Answers.Text: If your module is called `my-module` in your bootstrap file and contains a file `modules/my-module/src/templates/fields/myField.twig` the path will be `my-module/fields/myField.twig`
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-02T07:56:08.307Z
Answers.LastActivityDate: 2018-08-02T07:56:08.307Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I'm currently developing a module that contains a custom field and I'd like to create a template to be used in the <code>getInputHtml</code> method.</p>
  
  <p>I try using the <code>Craft::$app-&gt;view-&gt;renderTemplate</code> method to render a template in the "resources" folder of my module, but Craft does not seem to find the template. How should I specify the path to the template from this module?</p>
  
Tags: <templating><craft3><modules>
Title: How to render a template from within a module?
ThreadText: # How to render a template from within a module?
  
  (Asked by: username_0 on 2018-08-02)
  
  I'm currently developing a module that contains a custom field and I'd like to create a template to be used in the `getInputHtml` method.
  
  I try using the `Craft::$app->view->renderTemplate` method to render a template in the "resources" folder of my module, but Craft does not seem to find the template. How should I specify the path to the template from this module?
  
  **Tags:** templating, craft3, modules
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-02. Score: 6 | Accepted answer
  
  You need to include your template roots [Enhancing a Craft CMS 3 Website with a Custom Module](https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module#2-the-module-class)
  
  ```
  public function __construct($id, $parent = null, array $config = [])
  {
      Craft::setAlias('@modules/sitemodule', $this->getBasePath());
      $this->controllerNamespace = 'modules\sitemodule\controllers';
  
      // Base template directory
      Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) {
          if (is_dir($baseDir = $this->getBasePath().DIRECTORY_SEPARATOR.'templates')) {
              $e->roots[$this->id] = $baseDir;
          }
      });
  
      // Set this as the global instance of this module class
      static::setInstance($this);
  
      parent::__construct($id, $parent, $config);
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you username_1 for your swift reply. I've added the code to the main module file, but I'm not sure what path I should use to render the template. Do you have an idea?  
  > **username_1**: If your module is called `my-module` in your bootstrap file and contains a file `modules/my-module/src/templates/fields/myField.twig` the path will be `my-module/fields/myField.twig`  
  
  ---
  
  