包含一个小部件而不是排除所有其他小部件

Include one widget instead of excluding all others

我在边栏中有几个小部件。 我不希望其中一些小部件显示在我的某些页面上。

不知道怎么写比这个更简单?我有很多例外,所以这样写会是一本充满线条的书。 我只想写得像 "on this page include only this or these widgets" 一样简单(而不是 "on this page exclude this and these widgets")。

<%if( !(page.layout=="alpha" && (name=="w1" || name=="w2" || name=="links" || name=="w3") ) && !(page.layout=="beta" && (name=="w400" || name=="w500" || name=="w600") ) ){ %>

所以问题是如何使页面 "alpha" 仅显示例如 widget895 变得简单?

我假设您对小部件进行了某种循环,并且 name 对应于该循环中的当前小部件名称。

<%
    var pageWidgets = {
        alpha: ['widget895'],
        beta: ['widget900', 'widget123']
    };

    var allowedWidgets = pageWidgets[page.layout];

    // Loop over the widgets and grab the name, I assume
    // you're already doing something similar.
    widgets.forEach(function(widget) {
        var name = widget.name;

        // This is the equivalent of the 'if' in your
        // original code. Note that I've allowed all
        // widgets by default.
        if (!allowedWidgets || allowedWidgets.includes(name)) {
            %>
            ...widget rendering here...
            <%
        }
    });
%>