# How to combine twig-assets and Slim4?

**URL:** <https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459>\
**Category:** Questions\
**Created:** [September 20, 2019, 10:54am UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459 "2019-09-20T10:54:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [September 20, 2019, 10:54am UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/1 "2019-09-20T10:54:25Z")

</div>

Hello,  
I’ve recently started to work with Slim4 and are a bit thrilled about it.

How thou, do I add twig-assets so that i can add CSS and JS-files to the templates that i am using with Twig-View?

Meaning, registering the container and using it in the route…  
Cant find any help on google at the moment :/.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 20, 2019, 1:08pm UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/2 "2019-09-20T13:08:50Z")

</div>

Welcome @nadirian

You mean this library? [https://github.com/odan/twig-assets](https://github.com/odan/twig-assets)

---

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [September 20, 2019, 2:30pm UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/3 "2019-09-20T14:30:27Z")

</div>

Hi @odan , for example that one.  
I am not experienced enough to figure it out myself.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 21, 2019, 12:04pm UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/4 "2019-09-21T12:04:30Z")

</div>

You have multiple options:

- Use Twig with [Webpack](https://webpack.js.org/) to bundle (compile, combine and minimize) web assets.[Slim 4 - Compiling Assets with Webpack](https://odan.github.io/2019/09/21/slim4-compiling-assets-with-webpack.html)
- Use a Twig assets extension like [odan/twig-assets](https://github.com/odan/twig-assets) (inspired by [Symfony Web Assets](https://symfony.com/doc/3.0/best_practices/web-assets.html))
- Use Twig (without extensions). Just create static CSS and JS files in your `public/` directory and include them in your templates.

---

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [September 23, 2019, 8:08am UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/5 "2019-09-23T08:08:41Z")

</div>

Could i bother you with some code samples for option number 2?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 23, 2019, 1:41pm UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/6 "2019-09-23T13:41:32Z")

</div>

If you open the link, you can find some examples.

---

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [October 3, 2019, 8:39am UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/7 "2019-10-03T08:39:42Z")

</div>

@odan yes, i see them, but i simply cannot understand. I’d like an update of the “slim framework” section that works with slim 4 (or a more detailed basic start).

Maybe im the stupidest programmer on the earth, or i am not alone with these questions 🙂 . Someone has to ask the first question :).

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [October 3, 2019, 9:12am UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/8 "2019-10-03T09:12:04Z")

</div>

Okay, I see. I should update the documentation for Slim 4 🙂  
Thanks for the hint. 👍

---

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [October 3, 2019, 9:19am UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/9 "2019-10-03T09:19:46Z")

</div>

Awesome! Thanks for understanding!  
I’d like to put either my CSS/JS under /public , or even under …/templates (outside of public), and use functions like “assets({files: [‘Login/login.css’]})” to get them into the .html files. So everything is neat and pretty. That would be fun to learn! 🙂

Probably , when i understand it more. I’d laugh at even asking this… I dont know.

---

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [October 4, 2019, 6:51am UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/10 "2019-10-04T06:51:24Z")

</div>

Thanks for the updated manual! It clarified some things.

1. $settings[‘public’] is not set, i guess this points to the /public-dir, but still, may confuse someone
2. Since im using the slim-skeleton with PHP-DI, i am using the “addDefinitions”-function to add Twig::class, i am failing at “-\>get(App::class)-\>getBasePath()” since its not instantiated here. What do i do here?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [October 4, 2019, 8:21am UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/11 "2019-10-04T08:21:27Z")

</div>

1. Yes, the `public` key should point to your `public/` directory.

```php
// Path settings
$settings['root'] = dirname( __DIR__ );
$settings['temp'] = $settings['root'] . '/tmp';
$settings['public'] = $settings['root'] . '/public';

```

1. This is just optional. I replaced this part from the documentation with a simplified approach.  
How to setup Twig with PHP-DI is already documented in the [Twig-View documentation](https://github.com/slimphp/Twig-View/tree/3.x#usage).

---

<div class="post-metadata">

**Author:** ![nadirian](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nadirian](https://discourse.slimframework.com/u/nadirian)\
**Post date:** [October 4, 2019, 10:56am UTC](https://discourse.slimframework.com/t/how-to-combine-twig-assets-and-slim4/3459/12 "2019-10-04T10:56:57Z")

</div>

Thanks alot! I’ve actually got it all working! Although i do not know if its 100% properly setup, but it does generate cached css/js and i do receive the styles and js in the html with assets(), so … i am happy for now
