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

CC BY-SA 4.0
2018-08-02T08:11:57.800Z
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?
kant312
CC BY-SA 4.0
2018-08-02T08:13:51.980Z
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`
Robin Schambach
CC BY-SA 4.0
2018-08-02T07:56:08.307Z
2018-08-02T07:56:08.307Z
Robin Schambach
<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>

<templating><craft3><modules>
How to render a template from within a module?
# 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
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 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?  
> **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`  

---

