Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Garpur Dagsson 29 posts 164 karma points
    Feb 20, 2015 @ 10:53
    Garpur Dagsson
    0

    MacroContainer

    Hi all,

    I am not sure this is the correct place to ask, but here goes :)

     

    I am using 7.2.1 and razor.

    What I need is to be able to access the length of the MacroContainer so that I can choose layouts,

    eg. I have a different layout for 2 vs 4 macros in the container :)

     

    Is there any way to access the macrocontainer itself?

     

    regards,

    garpur

  • Mike Chambers 636 posts 1253 karma points c-trib
    Feb 20, 2015 @ 11:29
    Mike Chambers
    100

    we use something like along these lines.. to calc length of macroContainer.. (note the "_" is just for recursion and you might not be parameterising the MacroContainerName... so you may or may not need) 

     //split into the separate macros
          int macroCount = 0;
          string[] macros = new string[] {};
          try{
            string macro = Model.GetProperty("_"+Parameter.macroContainerName).Value.Replace("><", ">$<");
            macros = macro.Split(new string[] { "$" }, StringSplitOptions.RemoveEmptyEntries);
            macroCount = macros.Length;
          }catch{}
  • Mike Chambers 636 posts 1253 karma points c-trib
    Feb 20, 2015 @ 11:38
    Mike Chambers
    0

    Just thought.. you might also want this... for then rendering macros...

    @(Html.Raw(umbraco.library.RenderMacroContent(macros[0], Model.Id)))
  • Garpur Dagsson 29 posts 164 karma points
    Feb 20, 2015 @ 12:23
    Garpur Dagsson
    0

    Thank you for the trick Mike,

    this however depends on no part of my macro partial html having "><" and is therefore brittle :)

    I will use it and submit a feature request for the team to provide more access to the container.

     

    cheers

    Garpur

  • Mike Chambers 636 posts 1253 karma points c-trib
    Feb 20, 2015 @ 12:55
    Mike Chambers
    0

    Not sure we are talking about the same thing here then... the "><" has nothing to do with content in the macro partial...

    I thought you were talking about use of the MacroContainer Datatype..

    if you write out the rawtext... of a doctype param set to use the macroContainer datatype

    @Html.Raw(Model.GetProperty("_"+Parameter.macroContainerName).Value)

    you end up with something like this (a page with 8 macros in the MacroContainer...)

    <?UMBRACO_MACRO macroalias="AbrAbstract" node="1065" hideImage="1" hideMore="1" hideMainHeading="1" wrapperClass="abstract-feature intro" spanClass="span6" thumbnailType="Square Crop" btnClass="0" altHeading="" /><?UMBRACO_MACRO macroalias="AbrAbstract" node="1246" hideImage="1" hideMore="1" hideMainHeading="1" wrapperClass="abstract-feature-info" spanClass="span6" thumbnailType="Small Rectangle Crop" btnClass="0" altHeading="" /><?UMBRACO_MACRO macroalias="AbrAbstract" node="1253" hideImage="0" hideMore="1" hideMainHeading="0" wrapperClass="abstract-feature-image" spanClass="span4" thumbnailType="Small Rectangle Crop" btnClass="0" altHeading="" /><?UMBRACO_MACRO macroalias="AbrAbstract" node="1254" hideImage="0" hideMore="1" hideMainHeading="0" wrapperClass="abstract-feature-image" spanClass="span4" thumbnailType="Small Rectangle Crop" btnClass="0" altHeading="" /><?UMBRACO_MACRO macroalias="InvestorFeedsTabs" wrapperClass="" spanClass="span4" /><?UMBRACO_MACRO macroalias="AbrAbstract" node="1251" hideImage="1" hideMore="1" hideMainHeading="0" wrapperClass="abstract-feature-text-only" spanClass="span3" thumbnailType="Large Rectangle Crop" btnClass="0" altHeading="" /><?UMBRACO_MACRO macroalias="UsefulLinks" spanClass="span3" wrapperClass="list abstract-feature-links" altHeading="" /><?UMBRACO_MACRO macroalias="AbrList" node="1245" hideListHeading="0" altHeading="" aggregated="0" limitItemsToX="" hideDate="0" hideImage="1" thumbnailType="Large Rectangle Crop" spanClass="span3" wrapperClass="list abstract-feature" hideAbstract="1" abrLength="" hideRSS="1" showParentAbstract="0" gridSize="" /><?UMBRACO_MACRO macroalias="ContentSlider" node="2301" sliderId="highlights" hideImage="0" hideTable="0" hideMore="0" hideMainHeading="0" hideArrows="0" hidePills="0" spanClass="span3" altHeading="" panelClass="" wrapperClass="highlights-panel" />

    so each macro... added to the macroContainer ends up as <?UMBRACO_MACRO macroalias="XXX" {...All my params} />

    So in effect all i'm doing is intercepting this and parsing.. and then rendering outside of the standard render method from the umbraco core for a macroContainer.

    I hope that explains it a  little more?

    (ps this was from umb 4, maybe things have changed in u7?)

  • Garpur Dagsson 29 posts 164 karma points
    Feb 20, 2015 @ 13:32
    Garpur Dagsson
    0

    Hi, 

    Things have changed :( :p :)

    All I get is the html, I can't find any GetProperty on the Model, but I can still make this work (just a bit brittle).

    I'll take a look at the C# source for the container and see if I can just extend that, I was just hoping to not have to do that :)

     

    thx

    garpur

  • Mike Chambers 636 posts 1253 karma points c-trib
    Feb 20, 2015 @ 13:54
    Mike Chambers
    1

    had a look into it.. and had to go a little round the houses...

    In a template.... @CurrentPage.macroContainer and GetProperty etc... as you say returns the rendered content of all the macros..

    However, second gives me that <?Umbraco_Macro /> list that I can use... (still using the xml cache and not DB acess as using the nodeFactory)

     

       @{
                @CurrentPage.macrocontainer
    
                umbraco.NodeFactory.Node n = new umbraco.NodeFactory.Node(CurrentPage.id);
                @(n.GetProperty("macrocontainer").Value)
    
            }

    test

  • Garpur Dagsson 29 posts 164 karma points
    Feb 20, 2015 @ 16:59
    Garpur Dagsson
    0

    Posting what I ended up doing for others to find :p

     

    @{

    .. other stuff

       var rawNode = new umbraco.NodeFactory.Node(CurrentPage.id);

      var macros = rawNode.GetProperty("MacroContainerName").Value.Replace("><", ">$<").Split('$');

      var macroCount = macros.Length;

      var macroWidth = "medium-3";

      var macroLineWidth = "";

    }

    @switch(macroCount){
      case 2:
        macroWidth = "medium-6";
        macroLineWidth = "medium-6";
        break;
     ...
    }
    <section class="foo padding">
      <row>
        <column class="col-centered">
          <h3 class="text-center">@Umbraco.Field("TITLE")</h3>
        </column>
        <column class="col-centered @macroLineWidth">
          <row equalize-wrap>
            @foreach(var m in macros) {
              <column class="@macroWidth">
                @(Html.Raw(umbraco.library.RenderMacroContent(m, CurrentPage.Id)))
              </column>
            }
          </row>
        </column>
      </row>
    </section>
    This way I can both set the widh of the line containing the macros and the macros themselves from the number of macros found.
    Thanks to Mike for the help.
    cheers
    garpur
Please Sign in or register to post replies

Write your reply to:

Draft