1

How do you guys handle different paths between local and production environments regarding css and js files in your template twig files?

I am following the tutorials of nystudio107, which are simply awesome but can't see how this is be solved even in his pre-ready and configured craft3 download packages.

For example, in local dev I'd like to keep the css and js files separated from each other and unminified for better debugging. On production the files are combined and minified with gulp and moved to a /dist/css – /dist/js folder, which then does not matches the layout path I set in the twig files. Do you ran into similar problems and use a performant solution or do you hardcode the paths to the files on production environment?

  • 1
    Do you really use Craft 3? If so you can create different assetBundes for different environments. I can explain how after I know if it's Craft 3 or Craft 2. yiiframework.com/doc-2.0/… Just check your environment and load different sources – Robin Schambach Feb 12 '18 at 18:55
  • Hello @RobinSchambach , yes I moved on to Craft 3.0.0.RC9. I am very new to all sort of dev environments. Before that I modified wordpress themes directly within a child theme over ftp. So I am sorry, if my question is really newbie! Thanks for the help in advance – Pascal G. Feb 12 '18 at 21:56
1

If you are using Craft 3 Multi-Environment then you can add environment specific aliases to those assets in your general config file.

Eg:

// Live (production) environment
'live' => [
    // Craft defined config settings
    'allowUpdates' => false,
    'backupOnUpdate' => false,
    'devMode' => false,
    'enableTemplateCaching' => true,
    'isSystemOn' => true,
    // Aliases parsed in sites’ settings, volumes’ settings, and Local 
    volumes’ settings
    'aliases' => [
      '@assetPath' => '/dist/'
    ],
    // Custom site-specific config settings
    'custom' => [

    ]
],

Then, in your templates you can use {{ alias('@assetPath/js/' }}.

Note, while I've called the alias assetPath, you can name it anything you like.

| improve this answer | |
  • thanks @Seth, yeah I am already using this multi config file, so this should be easy to implement. Although I haven't time to give it a try, I check this answer :-) If I encounter any problems I will comment again, but this seems pretty straightforward! :-) – Pascal G. Feb 14 '18 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.