例如:考虑路线/主题 路径应该在指定为路径/查询参数的主题(读取:LESS颜色变量)中呈现自身。 基于theme参数,可能还需要注入自定义JS脚本。
脚本和样式可能包含也可能不包含,具体取决于提供的参数(排除预先配置套索或使用bower.json)。这也意味着必须在路由呈现模板之前指定依赖项。
我目前正在使用Marko v4 + ExpressJS + Lasso + Less + lasso-marko + lasso-less
我没有发布代码,因为在尝试了很多这样的事情之后它已经遍布整个地方了。如果描述不够清楚,请告诉我。将尝试将沙箱放在一起用于演示目的。
更新:添加核心文件和目录结构
sandbox
|- components
| |- app-main.marko
|- dependencies
| |- theme1
| |- main.js
| |- variables.less
| |- theme2
| |- main.js
| |- variables.less
|- node_modules
|- public
|- templates
| |- base
| |- index.marko
| |- style.less
| |- browser.json
|- index.js
|- package.json
//index.js
var markoExpress = require('marko/express');
require('marko/node-require');
var express = require('express');
var app = express();
var compression = require('compression');
var isProduction = process.env.NODE_ENV === 'production';
var lasso = require('lasso');
lasso.configure({
plugins: [
'lasso-marko',
'lasso-less'
],
outputDir: __dirname + '/public',
bundlingEnabled: isProduction,
minify: isProduction,
fingerprintsEnabled: isProduction,
});
app.use(express.static('public'));
app.use(markoExpress());
app.use(compression());
app.use(require('lasso/middleware').serveStatic());
var template = require('./templates/base');
app.get('/:pub', function (req, res) {
var pub = req.params.pub || "theme1";
res.marko(template, {
theme:pub
});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
if (process.send) {
process.send('online');
}
});
//browser.json
{
"dependencies": [
{
"if-flag": "theme1",
"dependencies": [
"less-import: ../../dependencies/theme1/variables.less",
"../../dependencies/theme1/main.js"
]
},
{
"if-flag": "theme2",
"dependencies": [
"less-import: ../../dependencies/theme2/variables.less",
"../../dependencies/theme2/main.js"
]
}
]
}
<!-- index.marko -->
<lasso-page package-path="./browser.json" flags="['${input.theme}']"/>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0"/>
<title>Test Template</title>
<!-- CSS includes -->
<lasso-head/>
</head>
<body>
<!-- Top-level UI component: -->
<include('../../components/app-main',input) />
<lasso-body/>
</body>
</html>
//style.less
main {
background-color: @bgcolor;
color: @fgcolor;
width: 100%;
height: 100%;
}
// ~/dependencies/theme1/variables.less
@bgcolor: red;
@fgcolor: white;
// ~/dependencies/theme1/main.js
alert("theme1");
<!-- app-main.marko -->
<main>TADAAA</main>
答案 0 :(得分:0)
Lasso支持基于&#34; flags&#34;:https://github.com/lasso-js/lasso#conditional-dependencies
的条件依赖项如果您将主题作为其中一个标记,则可以使用该标记有条件地在 <!DOCTYPE html>
<html>
<head>
<style>
body {font-family: "Lato", sans-serif;}
/* Style the tab */
div.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
div.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
div.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
div.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
/* Style the close button */
.topright {
float: right;
cursor: pointer;
font-size: 20px;
}
.topright:hover {color: red;}
</style>
</head>
<body>
<p>Click on the x button in the top right corner to close the current tab:</p>
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen1">London</button>
<button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>
<div id="London" class="tabcontent">
<span onclick="this.parentElement.style.display='none'" class="topright">x</span>
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div id="Paris" class="tabcontent">
<span onclick="this.parentElement.style.display='none'" class="topright">x</span>
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>
<div id="Tokyo" class="tabcontent">
<span onclick="this.parentElement.style.display='none'" class="topright">x</span>
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<p>Click on the x button in the top right corner to close the current tab:</p>
<div class="tab">
<button class="tablinks" onclick="openCity(event, '1')" id="defaultOpen2">1</button>
<button class="tablinks" onclick="openCity(event, '2')">2</button>
<button class="tablinks" onclick="openCity(event, '3')">3</button>
</div>
<div id="1" class="tabcontent">
<span onclick="this.parentElement.style.display='none'" class="topright">x</span>
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div id="2" class="tabcontent">
<span onclick="this.parentElement.style.display='none'" class="topright">x</span>
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>
<div id="3" class="tabcontent">
<span onclick="this.parentElement.style.display='none'" class="topright">x</span>
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<script>
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen1").click();
document.getElementById("defaultOpen2").click();
</script>
</body>
</html>
中引入Less变量或其他依赖项:
browser.json
您可以预先构建所有主题,也可以在运行时使用Lasso动态构建页面JS / CSS。
希望能解决你的问题。
答案 1 :(得分:0)
在尝试了很多东西之后,我不得不采用编程方式将样式表构建为字符串并将其插入到样式标记内的模板中。
<style>${input.computedStyleString}</style>
这显然不太理想(考虑到套索如何处理其他所有事情)但它确实有效。