本文介绍了是否可以在HTML< head>中声明LESS变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在HTML中声明LESS变量,并在单独的.less文件中使用声明的vars.

Is it possible to declare LESS variables in the HTML and use the declared vars in a seperate .less file.

我想做这样的事情:

<html>
<head>

<style type="text/less">
  @MYVAR: #9BC509;
</style>

<!-- use MYVAR inside the main.less file -->
<link rel="stylesheet/less" type="text/css" href="styles/less/main.less">

</head>
...
</html>

由于这不起作用,也不是一个干净的解决方案,因此我重组了项目,现在为所需的变量制作了一个普通的@incude文件. django模板引擎将较少的文件与数据库值一起动态写入磁盘(并进行了打包以提高性能).

Since this did not work and was not a clean solution, I restructured my project, and now do a normal less file @incude for the variables I need. The less file is dynamically written to disc with database values by the django templating engine (and chached for better performance).

致所有反对者.我真的不明白你的意思!您甚至都没有写下投票的原因.如果您没有达到我的目标,那么这就是我应该问的慢思维者"的问题:

To all the downvoters. I don't really get your point! You didn't even write why you downvoted. I case you did not get what I was aiming for, here is the question I should have asked for "slow-thinkers":

如何实现包含在<head></head>中声明的更少变量以在单独的更少文件中可用?但是无论如何,这只适合某些时候点击这篇文章的人.正如我说的,我选择了另一个解决方案.

How can I achieve including less variables declared in the <head></head> to be available in a seperate less file? But anyways this is just for people hitting this post at some point... As I said I chose another solution.

推荐答案

您可以通过在全局less对象的globalVars属性中定义变量来做到这一点.您应在调用less.js编译器之前声明less对象,另请参见: http://lesscss.org/usage/#using-less-in-the-browser

You can do this by defining your variables in the globalVars property of a global less object. You should declare the less object before calling the less.js compiler, see also: http://lesscss.org/usage/#using-less-in-the-browser

<link rel="stylesheet/less" type="text/css" href="global.less">
<script>
    var less = {
    globalVars: {
      color: 'red',
      width: '15px'
    }
    }
</script>
<script src="js/less.min.js" type="text/javascript"></script>

通过上述操作,您可以在global.less的Less代码中使用@color@width.

The preceding enables you to use @color and @width in the Less code of global.less.

这篇关于是否可以在HTML&lt; head&gt;中声明LESS变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:18