How to combine twig-assets and Slim4?

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 :/.

Welcome @nadirian

You mean this library? https://github.com/odan/twig-assets

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

You have multiple options:

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

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

@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 :slight_smile: . Someone has to ask the first question :).

Okay, I see. I should update the documentation for Slim 4 :slight_smile:
Thanks for the hint. :+1:

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! :slight_smile:

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

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?
  1. Yes, the public key should point to your public/ directory.
// 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.

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

1 Like