duanpingzu7194 2011-12-19 02:01
浏览 1761
已采纳

将变量传递给LESS.js样式表

I have several variables (hex colors) defined in a database. I need to pass these variables from MySQL to a LESS.js stylesheet via PHP. Possible?

If not, any advice on a way to do something similar? The lighten and darken variables are key.

  • 写回答

2条回答 默认 最新

  • du6jws6975 2012-01-25 14:14
    关注

    The best approach that come in my mind is to dynamic generate a LESS file using PHP (including your vars).

    1\ You will need to include a new style sheet in your HTML pages.

    <link rel='stylesheet/less' href='css/style.php' />
    

    2\ In your style.php include your PHP vars as follows:

    <?php header("Content-type: text/css; charset: UTF-8"); ?>
    
    @brand_color_1 = <?php echo $brand_color_1; ?>;
    /* Add all other vars do you need. */
    

    3\ Then at the bottom (or after you LESS var declaration) of this style.php add all your needed imports as follows:

    <?php
      header("Content-type: text/css; charset: UTF-8");
    
      @brand_color_1 = <?php echo $brand_color_1; ?>;
      /* Add all other vars do you need. */
    
      @import "style.less";
    ?>
    

    This will works like a clock.

    There is an article you can read about CSS Variables with PHP written by Chris Coyier.


    Another not recommended alternative is compiling you LESS files in client-side, you could manually compile them and pass PHP vars doing the following:

    <script type="text/javascript">
    var colors = '';
    colors += '@brand_color_1: <?php echo $brand_color_1 ?>;'
    colors += '@brand_color_2: <?php echo $brand_color_2 ?>;'
    colors += '@import "style.less"';
    // Add other imports.
    var parser = new (less.Parser)();
    parser.parse(colors, function(err, tree) { 
      var css = tree.toCSS();
      // Add it to the DOM maybe via jQuery
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿