# Adding custom post templates using service container pattern

**URL:** https://discourse.roots.io/t/adding-custom-post-templates-using-service-container-pattern/26171
**Category:** radicle
**Created:** 2023-10-24T23:40:47Z
**Posts:** 2

## Post 1 by @rgdigital — 2023-10-24T23:40:47Z

When using the service container in Radicle, I set up ‘services’ where the service folder contains all the code for registering post types, blocks, etc. Then, I use WordPress template inheritance but with the blade extension (eg, `single-{post-type}.blade.php`) to create templates in the resources/views folder.

What I would like to do is store the blade templates for the service in the _service_ folder instead.

Here is an example of the structure for views inside a service (compared with the default view folder in resources):

```
├── app/
│ └── Services/
│ └── SomeService/
│ ├── resources/
│ │ └── views/
│ │ └── single-somecpt.blade.php <- use this
│ └── SomeServiceProvider.php
└── resources/
    └── views/
        └── single-somecpt.blade.php <- don't use this
```

How could I apply template inheritance for custom post type templates in a service pattern? I figured if I could just use prependLocation() in the view finder to tell Roots to look in the service folder first like so:

```
public function boot()
{
   # Add path to view paths array
   view()->getFinder()->prependLocation(
       base_path('app/Services/SomeService/resources/views'),
   );
}
```

This allows me to render blade templates for custom post types via the service, but **only** if the same template exists in the default view folder.

Is it possible to render the blade template from the resource/views folder in the _service_, without requiring the template to exist in the _default_ folder too?

---

## Post 2 by @rgdigital — 2023-10-25T12:44:57Z

Update - hardcoding the template path works when hooked into `template_include` -

```
public function register()
    {
        // Hook into `template_include`
        add_filter('template_include', [$this, '_somecpt_templates'], 99);
    }

    public function _somecpt_templates($template)
    {
        // Single template for 'somecpt' post type
        if ( get_post_type() == 'somecpt' ) {
            if (is_single()) {
                // Specify full path to template
                $new_template = base_path('app/Services/SomeService/resources/views/single-somecpt.blade.php');
                return ('' !== $new_template) ? $new_template : $template;
            }
        }
```

This bypasses template hierarchy by specifying the full path to the template, so **blade.php** must be included.

If there’s a way to hook this pattern into template hierarchy I’m all ears.
