Search code examples
ejshexo

Include one widget instead of excluding all others


I have a couple of widgets in the sidebar. I do not want that some of these widgets show on some of my pages.

I do not know how to write it more simple than this? I have many exclusions, so writing it like this would be a book-work full of lines. I just want to write as simple as "on this page include only this or these widgets" (instead of "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") ) ){ %>

So question is how to make it simple that page "alpha" shows only widget895 for example?


Solution

  • I've assumed that you've got some sort of loop over the widgets and that name corresponds to the current widget name within that loop.

    <%
        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...
                <%
            }
        });
    %>