We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22827
    • 129 Posts
    Hello all,

    I am implementing a pattern that comes up pretty regularly for me, which is a sidebar where there are a number of positions at the side of a page where the content editor can choose what appears in each.

    I am taking a different approach this time, and am looking for a second set or eyes to perhaps make the approach more elegant, but otherwise to fix a problem.

    The sidebar content is contained in resources under a Sidebar parent in the resource tree. These just have a title and content defined.

    Sidebar
    - Sidebar-a
    - Sidebar-b
    - Sidebar-c
    - Sidebar-etc

    The sidebar content is flexible - could be anything,

    The resources that support sidebars have three Resource TVs, which points to the Sidebar parent. So far so good, the content editor can select a sidebar resource via the TV. There are also three list-box TVs that define the class that will be used to style the sidebar element. So sidebar1 TV contains the resource to be displayed, and sidebar1-colour defines the class that styles it (red||green||blue for example).

    The template contains this:
    [[*sidebar1:notempty=`
      [[!getResources? &parents=`31` &resources=`[[*sidebar1]]` &tpl=`sidebarbox` &includeContent=`1`]]
    `]]
    [[*sidebar2:notempty=`
      [[!getResources? &parents=`31` &resources=`[[*sidebar2]]` &tpl=`sidebarbox` &includeContent=`1`]]
    `]]
    
    [[*sidebar3:notempty=`
      [[!getResources? &parents=`31` &resources=`[[*sidebar3]]` &tpl=`sidebarbox` &includeContent=`1`]]
    `]]
    


    Note that there is a single template for displaying the sidebar elements, and it looks like this:

          <div id="red-box">
            <div class="header">[[+longtitle]]</div>
            <div class="content">[[+content]]</div>
          </div>
    


    Now rather than only having one template per colour and hardcoding the id, I want to use the sidebar1-colour - like this:

          <div id="[[*sidebar1-colour]]-box">
            <div class="header">[[+longtitle]]</div>
            <div class="content">[[+content]]</div>
          </div>
    


    The problem here is that that this can't work for all three sidebars without a separate template again.

    It would need to look something like:

          <div id="[[*sidebar[[+sidebarid]]-colour]]-box">
            <div class="header">[[+longtitle]]</div>
            <div class="content">[[+content]]</div>
          </div>
    


    And then get sidebarid somehow passed through from getResources. I can't think of a way of doing this...

    I know I could create a custom snippet and solve this fairly easily, however I wanted to see if I could get an out-of -the-box solution with just standard modx components.

    Any suggestions would be welcome - sometimes with modx you need to think differently and I get the feeling I am missing a simpler approach.

    Cheers,

    Paul

    [ed. note: paulkoan last edited this post 15 years, 1 month ago.]
      • 3749
      • 24,544 Posts
      At first glance, it seems wasteful to use getResources to get a single resource. It will do a lot of unnecessary processing. How about using this for the TV's value:
      @RESOURCE 31

      Unless you need the &tpl to display the resource. In that case, I'd use a simple snippet like this:
      [[ShowResource? &docId=`31` &tpl = `someChunk`]]

      <?php
      /*ShowResource Snippet */
      $resource = $modx->getObject('modResource', $scriptProperties['docId']);
      return $mod->getChunk($scriptProperties['tpl'], $resource->toArray() );
      

      As for the color, I think I would create separate CSS rules and put the class name in the sidebar-colour TV.
      <div class="[[*sidebar-colour]]">
      [ed. note: BobRay last edited this post 15 years, 1 month ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 22827
        • 129 Posts
        Thanks, yes I was thinking getResources was overkill, but at least is a "standard" snippet, which minimises maintenance.

        However I can see that a custom snippet is trivial.

        With the colour part of the sidebar module, it seemed tricky because there are three - but yes, I can put the div for each one into a chunk holding the three sidebars separate from tpl used for the snippet.

        What I would really like is a tv / chunk solution that allowed any number of sidebar modules to be selectable rather than the artificial cap of three that my approach takes.
          • 3749
          • 24,544 Posts
          I think my idea would work for that if you created all the CSS rules:

          div.red{}
          div.blue{}
          div.green{}
          etc.


          One caveat is that the custom snippet will not respect permissions or published/deleted status unless you modify it to do so. This will take care of the published/deleted status:

          $modx->getObject('modResource', array('id'=>$scriptProperties['docId'], 'published'=>'1', 'deleted'=>'0'));
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting