# Extending a template file

As you already learned, hostware uses the [twig template engine](https://twig.symfony.com/) under the hood. This makes extending anything storefront and theme related really easy.

## Get familiar

First, get familiar with the core template files of the hostware storefront. Take a look in the directory `/resources/views/storefront`. You will see all of the base twig templates.

The directory layout is extremely important and should be considered any time. You will need the path structure whenever you want to extend anything.

## Extending process

### What do you want to change?

Figure out the template you want to change. Do you really need to change the html layout to adjust the theme to the design? Maybe you can accomplish the goal with just CSS.

Lets say you want to adjust following product card which is likely capsulated in a separate component.&#x20;

<figure><img src="https://4059027548-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnJHcdSq7NNv7Ws704nzw%2Fuploads%2Fbf0yAvaG8aZcZ6IdkfNr%2Fimage.png?alt=media&#x26;token=185365c8-d571-4cb0-b7fa-f2476868ad2a" alt=""><figcaption></figcaption></figure>

### Find the template

You can find this specific card in /storefront/components/product/card.twig.

{% hint style="info" %}
If you have trouble finding a twig template, try finding a unique html attribute (like a class) in the rendered page and search for that in the project directory.
{% endhint %}

In this case the template file looks like this. As you can see, at every strategic point is a {% block %} wrapper around the HTML. This process ensures that you can change as minimal as possible.

The goal for this example is to a) add another class to the .card class and b) add a prefix to the card title.

<figure><img src="https://4059027548-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnJHcdSq7NNv7Ws704nzw%2Fuploads%2F31Sm0y5jE2HYAr9sTSYK%2Fimage.png?alt=media&#x26;token=51f12044-d8c7-471c-af12-bb8f5619ebdd" alt=""><figcaption></figcaption></figure>

### Create your overwrite template

To overwrite this template, you have to create a template name with the same name and path structure in your theme / module directory.

For this example, create following template: `custom\themes\hw\hostx\resources\views\storefront\component\product\card.twig`

{% hint style="info" %}
Remember to adjust the theme / module path!
{% endhint %}

The first line in your custom card.twig file should contain the path to the file you want to overwrite. In this case, we have to prepend the `storefront::` namespace because we want to change a core template.\
This tells the hostware framework in which order the templates should be rendered.

```twig
{% hw_extends 'storefront::component/product/card.twig' %}
```

### Overwrite single blocks

In our example, we want to overwrite the part with `class="card"` to add our additional class. We need to find a {% block %} in the original template file which is before the thing we want to change. In this example the block `product_item_card` is perfect for us.

<figure><img src="https://4059027548-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnJHcdSq7NNv7Ws704nzw%2Fuploads%2FeIGLjOc2sIh5Lh14rmxt%2Fimage.png?alt=media&#x26;token=ccd98a89-9137-49cf-8850-fe207dbdb889" alt=""><figcaption></figcaption></figure>

Lets just write following code to overwrite the whole product card for now:

<figure><img src="https://4059027548-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnJHcdSq7NNv7Ws704nzw%2Fuploads%2FhMOujwY7tPLV6JkfE73q%2Fimage.png?alt=media&#x26;token=5ecc95fc-5bd2-4377-aede-d23ace528d71" alt=""><figcaption></figcaption></figure>

The new content "Overwrite!" will now be rendered INSTEAD of the original content inside of that specific block. Insteadwe should just change what we need and then render the next child block to overwrite as less core-code as possible. This should do the trick:

<figure><img src="https://4059027548-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnJHcdSq7NNv7Ws704nzw%2Fuploads%2FfXCcHakLjCZgx5VlfsJQ%2Fimage.png?alt=media&#x26;token=e7b9061a-e01d-4e16-820b-1c12ae3c868b" alt=""><figcaption></figcaption></figure>

We also want to change the title. Each block you want to change MUST be on the root level of your twig file. Nested blocks are not valid if you want to overwrite them.  Take a look at the [original file](#find-the-template) to find the best block to overwrite. In our case, the block `product_item_card_head_inner` seems the best.

Our overwrite file now looks like this:

<figure><img src="https://4059027548-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnJHcdSq7NNv7Ws704nzw%2Fuploads%2FGPePqZAOQMTdzK2YC8GL%2Fimage.png?alt=media&#x26;token=84c9d2ec-ca7d-4c37-8838-31cda208dba3" alt=""><figcaption></figcaption></figure>

### Take a look

After you saved the file, you should view your changes. (If not, try clearing the cache!)

<figure><img src="https://4059027548-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnJHcdSq7NNv7Ws704nzw%2Fuploads%2FtavaYrBNRlLYV5x1CaNY%2Fimage.png?alt=media&#x26;token=12e8a7fe-282c-434e-97a2-9f7bb63ecd16" alt=""><figcaption></figcaption></figure>

## Troubleshooting

If your changes do not get rendered, check following:

1. Is your template path the same as in the core template?
2. Have you correctly inserted the [hw\_extends flag](#create-your-overwrite-template) in the first line of your template?
3. Do you overwrite the correct block on the first level of your twig file?
4. Have you cleared the cache? (Administration -> Settings -> Debug *OR* `php artisan twig:cache`)

If you have additional issues, please contact us.
