Magento 2 : How can I use LESS variables in Modules' LESS files? (not theme)












4















I try to include specific LESS variables in my Modules' LESS stylesheet. Note: my module, not my theme.



For example have a file called app/code/Vendor/Module/view/frontend/web/css/stylesheet.less. How can I use the parameters from lib/web/css/source/lib/variables/_layout.less in this stylesheet (like @layout-indent__width for example)?



Edit #1



I figured out I could do this like so:



@import '../../../../../../../../lib/web/css/source/lib/_variables.less';
@import '../../../../../../../../lib/web/css/source/lib/_responsive.less';


But somehow this doesn't look like the correct way to do this. Any suggestions on this?



Edit #2



I figured that including the LESS files like this would omit my variable overrides in my themes' _theme.less. Since the stylesheet in question is located in var/view_preprocessed/css/frontend/ThemeVendor/ThemeName/en_US/Vendor_Module/css I had to change the @import-directives to:



@import '../../css/source/lib/_variables.less';
@import '../../css/source/_theme.less';
@import '../../css/source/lib/_responsive.less';


Still seems a bit quirky, but at least it works :-/



I'm still open for suggestions though. This solution doesn't look right to me.



Edit #3:



I stand corrected, none of the above works anymore. I knew it was unstable to start with. :-/ Any suggestions anyone?










share|improve this question

























  • I don't think you can do it. But why would you want to do it in the first place? Magento's official position is that it will remove less in the next versions and include sass. That means your module will probably not work in next versions. I think the best practice for now it is to have pure css in your module.

    – Claudiu Creanga
    Jul 20 '16 at 10:13













  • I would like to do this so that my module could have a basic style, but still make use of variables like the primary color or the breakpoints that are used. Interesting to hear that they're dropping less. Do you have any source article confirming that?

    – Giel Berkers
    Jul 20 '16 at 11:17











  • alankent.me/2016/05/21/…

    – Claudiu Creanga
    Jul 20 '16 at 12:06
















4















I try to include specific LESS variables in my Modules' LESS stylesheet. Note: my module, not my theme.



For example have a file called app/code/Vendor/Module/view/frontend/web/css/stylesheet.less. How can I use the parameters from lib/web/css/source/lib/variables/_layout.less in this stylesheet (like @layout-indent__width for example)?



Edit #1



I figured out I could do this like so:



@import '../../../../../../../../lib/web/css/source/lib/_variables.less';
@import '../../../../../../../../lib/web/css/source/lib/_responsive.less';


But somehow this doesn't look like the correct way to do this. Any suggestions on this?



Edit #2



I figured that including the LESS files like this would omit my variable overrides in my themes' _theme.less. Since the stylesheet in question is located in var/view_preprocessed/css/frontend/ThemeVendor/ThemeName/en_US/Vendor_Module/css I had to change the @import-directives to:



@import '../../css/source/lib/_variables.less';
@import '../../css/source/_theme.less';
@import '../../css/source/lib/_responsive.less';


Still seems a bit quirky, but at least it works :-/



I'm still open for suggestions though. This solution doesn't look right to me.



Edit #3:



I stand corrected, none of the above works anymore. I knew it was unstable to start with. :-/ Any suggestions anyone?










share|improve this question

























  • I don't think you can do it. But why would you want to do it in the first place? Magento's official position is that it will remove less in the next versions and include sass. That means your module will probably not work in next versions. I think the best practice for now it is to have pure css in your module.

    – Claudiu Creanga
    Jul 20 '16 at 10:13













  • I would like to do this so that my module could have a basic style, but still make use of variables like the primary color or the breakpoints that are used. Interesting to hear that they're dropping less. Do you have any source article confirming that?

    – Giel Berkers
    Jul 20 '16 at 11:17











  • alankent.me/2016/05/21/…

    – Claudiu Creanga
    Jul 20 '16 at 12:06














4












4








4








I try to include specific LESS variables in my Modules' LESS stylesheet. Note: my module, not my theme.



For example have a file called app/code/Vendor/Module/view/frontend/web/css/stylesheet.less. How can I use the parameters from lib/web/css/source/lib/variables/_layout.less in this stylesheet (like @layout-indent__width for example)?



Edit #1



I figured out I could do this like so:



@import '../../../../../../../../lib/web/css/source/lib/_variables.less';
@import '../../../../../../../../lib/web/css/source/lib/_responsive.less';


But somehow this doesn't look like the correct way to do this. Any suggestions on this?



Edit #2



I figured that including the LESS files like this would omit my variable overrides in my themes' _theme.less. Since the stylesheet in question is located in var/view_preprocessed/css/frontend/ThemeVendor/ThemeName/en_US/Vendor_Module/css I had to change the @import-directives to:



@import '../../css/source/lib/_variables.less';
@import '../../css/source/_theme.less';
@import '../../css/source/lib/_responsive.less';


Still seems a bit quirky, but at least it works :-/



I'm still open for suggestions though. This solution doesn't look right to me.



Edit #3:



I stand corrected, none of the above works anymore. I knew it was unstable to start with. :-/ Any suggestions anyone?










share|improve this question
















I try to include specific LESS variables in my Modules' LESS stylesheet. Note: my module, not my theme.



For example have a file called app/code/Vendor/Module/view/frontend/web/css/stylesheet.less. How can I use the parameters from lib/web/css/source/lib/variables/_layout.less in this stylesheet (like @layout-indent__width for example)?



Edit #1



I figured out I could do this like so:



@import '../../../../../../../../lib/web/css/source/lib/_variables.less';
@import '../../../../../../../../lib/web/css/source/lib/_responsive.less';


But somehow this doesn't look like the correct way to do this. Any suggestions on this?



Edit #2



I figured that including the LESS files like this would omit my variable overrides in my themes' _theme.less. Since the stylesheet in question is located in var/view_preprocessed/css/frontend/ThemeVendor/ThemeName/en_US/Vendor_Module/css I had to change the @import-directives to:



@import '../../css/source/lib/_variables.less';
@import '../../css/source/_theme.less';
@import '../../css/source/lib/_responsive.less';


Still seems a bit quirky, but at least it works :-/



I'm still open for suggestions though. This solution doesn't look right to me.



Edit #3:



I stand corrected, none of the above works anymore. I knew it was unstable to start with. :-/ Any suggestions anyone?







magento2 magento-2.1 less






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 14 '16 at 11:57







Giel Berkers

















asked Jul 14 '16 at 7:51









Giel BerkersGiel Berkers

6,82423975




6,82423975













  • I don't think you can do it. But why would you want to do it in the first place? Magento's official position is that it will remove less in the next versions and include sass. That means your module will probably not work in next versions. I think the best practice for now it is to have pure css in your module.

    – Claudiu Creanga
    Jul 20 '16 at 10:13













  • I would like to do this so that my module could have a basic style, but still make use of variables like the primary color or the breakpoints that are used. Interesting to hear that they're dropping less. Do you have any source article confirming that?

    – Giel Berkers
    Jul 20 '16 at 11:17











  • alankent.me/2016/05/21/…

    – Claudiu Creanga
    Jul 20 '16 at 12:06



















  • I don't think you can do it. But why would you want to do it in the first place? Magento's official position is that it will remove less in the next versions and include sass. That means your module will probably not work in next versions. I think the best practice for now it is to have pure css in your module.

    – Claudiu Creanga
    Jul 20 '16 at 10:13













  • I would like to do this so that my module could have a basic style, but still make use of variables like the primary color or the breakpoints that are used. Interesting to hear that they're dropping less. Do you have any source article confirming that?

    – Giel Berkers
    Jul 20 '16 at 11:17











  • alankent.me/2016/05/21/…

    – Claudiu Creanga
    Jul 20 '16 at 12:06

















I don't think you can do it. But why would you want to do it in the first place? Magento's official position is that it will remove less in the next versions and include sass. That means your module will probably not work in next versions. I think the best practice for now it is to have pure css in your module.

– Claudiu Creanga
Jul 20 '16 at 10:13







I don't think you can do it. But why would you want to do it in the first place? Magento's official position is that it will remove less in the next versions and include sass. That means your module will probably not work in next versions. I think the best practice for now it is to have pure css in your module.

– Claudiu Creanga
Jul 20 '16 at 10:13















I would like to do this so that my module could have a basic style, but still make use of variables like the primary color or the breakpoints that are used. Interesting to hear that they're dropping less. Do you have any source article confirming that?

– Giel Berkers
Jul 20 '16 at 11:17





I would like to do this so that my module could have a basic style, but still make use of variables like the primary color or the breakpoints that are used. Interesting to hear that they're dropping less. Do you have any source article confirming that?

– Giel Berkers
Jul 20 '16 at 11:17













alankent.me/2016/05/21/…

– Claudiu Creanga
Jul 20 '16 at 12:06





alankent.me/2016/05/21/…

– Claudiu Creanga
Jul 20 '16 at 12:06










3 Answers
3






active

oldest

votes


















2














If you want to use lib variables.



You don't need to include lib file or do not modified it.



you need just put code as below example,



.classname {
.lib-css(color, @color-white);
}





share|improve this answer





















  • 1





    I know this is true for themes, but I want to include lib variables in my modules' LESS files. So not app/design/... but app/code/Vendor/Module/view/frontend/web/css/styles.less. If I try your solution I get the following error in the log: variable @color-white is undefined in file ....

    – Giel Berkers
    Jul 15 '16 at 10:01



















0














You don't have to import any lib core.
If you want use less in your module and want to extend from styles of base module, just create folder source/module in your css folder in your module



Structured in module will be:


web

---css

------source

---------module

---------------_minicart.less

---------_module.less



For example you want to extend styles of minicart. Copy file minicart.less in magento-blank-theme inside folder Magento_Checkout






share|improve this answer































    0














    I had an approximate issue.
    The same variables for theme & module *.less files must be used.
    During production mode static content deployment Magento Blank theme didn't saw my _theme.less variables for my module (Magento 2.3).



    Compilation from source: /app/source/vendor/magento/theme-frontend-blank/web/css/styles-m.less
    variable @custom-style is undefined in file /app/source/var/view_preprocessed/pub/static/frontend/Magento/blank/en_US/Test_Module/css/source/widgets/_test.less in _test.less



    So, the only way I saw to wrote in _theme.less (to prevent duplication)



    //@magento_import 'source/_variables.less';


    And add file app/code/../../view/frontend/web/css/source/_variables.less






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "479"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f125673%2fmagento-2-how-can-i-use-less-variables-in-modules-less-files-not-theme%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      If you want to use lib variables.



      You don't need to include lib file or do not modified it.



      you need just put code as below example,



      .classname {
      .lib-css(color, @color-white);
      }





      share|improve this answer





















      • 1





        I know this is true for themes, but I want to include lib variables in my modules' LESS files. So not app/design/... but app/code/Vendor/Module/view/frontend/web/css/styles.less. If I try your solution I get the following error in the log: variable @color-white is undefined in file ....

        – Giel Berkers
        Jul 15 '16 at 10:01
















      2














      If you want to use lib variables.



      You don't need to include lib file or do not modified it.



      you need just put code as below example,



      .classname {
      .lib-css(color, @color-white);
      }





      share|improve this answer





















      • 1





        I know this is true for themes, but I want to include lib variables in my modules' LESS files. So not app/design/... but app/code/Vendor/Module/view/frontend/web/css/styles.less. If I try your solution I get the following error in the log: variable @color-white is undefined in file ....

        – Giel Berkers
        Jul 15 '16 at 10:01














      2












      2








      2







      If you want to use lib variables.



      You don't need to include lib file or do not modified it.



      you need just put code as below example,



      .classname {
      .lib-css(color, @color-white);
      }





      share|improve this answer















      If you want to use lib variables.



      You don't need to include lib file or do not modified it.



      you need just put code as below example,



      .classname {
      .lib-css(color, @color-white);
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jul 14 '16 at 12:13









      Rushvi

      2,4091729




      2,4091729










      answered Jul 14 '16 at 11:59









      SuryakantSuryakant

      466




      466








      • 1





        I know this is true for themes, but I want to include lib variables in my modules' LESS files. So not app/design/... but app/code/Vendor/Module/view/frontend/web/css/styles.less. If I try your solution I get the following error in the log: variable @color-white is undefined in file ....

        – Giel Berkers
        Jul 15 '16 at 10:01














      • 1





        I know this is true for themes, but I want to include lib variables in my modules' LESS files. So not app/design/... but app/code/Vendor/Module/view/frontend/web/css/styles.less. If I try your solution I get the following error in the log: variable @color-white is undefined in file ....

        – Giel Berkers
        Jul 15 '16 at 10:01








      1




      1





      I know this is true for themes, but I want to include lib variables in my modules' LESS files. So not app/design/... but app/code/Vendor/Module/view/frontend/web/css/styles.less. If I try your solution I get the following error in the log: variable @color-white is undefined in file ....

      – Giel Berkers
      Jul 15 '16 at 10:01





      I know this is true for themes, but I want to include lib variables in my modules' LESS files. So not app/design/... but app/code/Vendor/Module/view/frontend/web/css/styles.less. If I try your solution I get the following error in the log: variable @color-white is undefined in file ....

      – Giel Berkers
      Jul 15 '16 at 10:01













      0














      You don't have to import any lib core.
      If you want use less in your module and want to extend from styles of base module, just create folder source/module in your css folder in your module



      Structured in module will be:


      web

      ---css

      ------source

      ---------module

      ---------------_minicart.less

      ---------_module.less



      For example you want to extend styles of minicart. Copy file minicart.less in magento-blank-theme inside folder Magento_Checkout






      share|improve this answer




























        0














        You don't have to import any lib core.
        If you want use less in your module and want to extend from styles of base module, just create folder source/module in your css folder in your module



        Structured in module will be:


        web

        ---css

        ------source

        ---------module

        ---------------_minicart.less

        ---------_module.less



        For example you want to extend styles of minicart. Copy file minicart.less in magento-blank-theme inside folder Magento_Checkout






        share|improve this answer


























          0












          0








          0







          You don't have to import any lib core.
          If you want use less in your module and want to extend from styles of base module, just create folder source/module in your css folder in your module



          Structured in module will be:


          web

          ---css

          ------source

          ---------module

          ---------------_minicart.less

          ---------_module.less



          For example you want to extend styles of minicart. Copy file minicart.less in magento-blank-theme inside folder Magento_Checkout






          share|improve this answer













          You don't have to import any lib core.
          If you want use less in your module and want to extend from styles of base module, just create folder source/module in your css folder in your module



          Structured in module will be:


          web

          ---css

          ------source

          ---------module

          ---------------_minicart.less

          ---------_module.less



          For example you want to extend styles of minicart. Copy file minicart.less in magento-blank-theme inside folder Magento_Checkout







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 19 '17 at 5:22









          mrtuvnmrtuvn

          1,7351729




          1,7351729























              0














              I had an approximate issue.
              The same variables for theme & module *.less files must be used.
              During production mode static content deployment Magento Blank theme didn't saw my _theme.less variables for my module (Magento 2.3).



              Compilation from source: /app/source/vendor/magento/theme-frontend-blank/web/css/styles-m.less
              variable @custom-style is undefined in file /app/source/var/view_preprocessed/pub/static/frontend/Magento/blank/en_US/Test_Module/css/source/widgets/_test.less in _test.less



              So, the only way I saw to wrote in _theme.less (to prevent duplication)



              //@magento_import 'source/_variables.less';


              And add file app/code/../../view/frontend/web/css/source/_variables.less






              share|improve this answer




























                0














                I had an approximate issue.
                The same variables for theme & module *.less files must be used.
                During production mode static content deployment Magento Blank theme didn't saw my _theme.less variables for my module (Magento 2.3).



                Compilation from source: /app/source/vendor/magento/theme-frontend-blank/web/css/styles-m.less
                variable @custom-style is undefined in file /app/source/var/view_preprocessed/pub/static/frontend/Magento/blank/en_US/Test_Module/css/source/widgets/_test.less in _test.less



                So, the only way I saw to wrote in _theme.less (to prevent duplication)



                //@magento_import 'source/_variables.less';


                And add file app/code/../../view/frontend/web/css/source/_variables.less






                share|improve this answer


























                  0












                  0








                  0







                  I had an approximate issue.
                  The same variables for theme & module *.less files must be used.
                  During production mode static content deployment Magento Blank theme didn't saw my _theme.less variables for my module (Magento 2.3).



                  Compilation from source: /app/source/vendor/magento/theme-frontend-blank/web/css/styles-m.less
                  variable @custom-style is undefined in file /app/source/var/view_preprocessed/pub/static/frontend/Magento/blank/en_US/Test_Module/css/source/widgets/_test.less in _test.less



                  So, the only way I saw to wrote in _theme.less (to prevent duplication)



                  //@magento_import 'source/_variables.less';


                  And add file app/code/../../view/frontend/web/css/source/_variables.less






                  share|improve this answer













                  I had an approximate issue.
                  The same variables for theme & module *.less files must be used.
                  During production mode static content deployment Magento Blank theme didn't saw my _theme.less variables for my module (Magento 2.3).



                  Compilation from source: /app/source/vendor/magento/theme-frontend-blank/web/css/styles-m.less
                  variable @custom-style is undefined in file /app/source/var/view_preprocessed/pub/static/frontend/Magento/blank/en_US/Test_Module/css/source/widgets/_test.less in _test.less



                  So, the only way I saw to wrote in _theme.less (to prevent duplication)



                  //@magento_import 'source/_variables.less';


                  And add file app/code/../../view/frontend/web/css/source/_variables.less







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 21 at 15:16









                  FlamboyantFlamboyant

                  112




                  112






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f125673%2fmagento-2-how-can-i-use-less-variables-in-modules-less-files-not-theme%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      An IMO inspired problem

                      Management

                      Has there ever been an instance of an active nuclear power plant within or near a war zone?