Forum Discussion

cstone's avatar
8 years ago

Can a variable be used as a Macro parameter?

For example:

<#macro repeat count>
  <#list 1..count as x>
    <#nested>
  </#list>
</#macro>

 

<#assign boardCount= rest("categories/id/sales/boards/count").value/>

<@repeat count={boardCount} > <li class="detail-tile-shim"></li> </@repeat>

What is the correct way to achieve this?

  • cike's avatar
    cike
    8 years ago

    Hi cstone,

     

    simply remove the curly braces from the variable name you want to use in your macro:

     

    <#assign count = boardCount % 3 />
    <@repeat count=count >
    <li class="detail-tile-shim"></li>
    </@repeat>

     

    Cheers,

    Christian

4 Replies

  • Hi cstone  yes, variable can be used as a macro parameter. Can you please explain what's your requirement. Might be that can be achieved in a easier way.

  • cstone's avatar
    cstone
    Ace
    8 years ago

    Hey VikasB, thanks for the reply. The example code I added results in errors. What is the correct syntax for passing in a variable as parameter to a macro?

     

    Here is what I am trying to achieve:

    • I am creating a listing of our Boards for the CategoryPage, in the form of "tiles"
    • The tiles are arranged in rows of 3, depending on how many Boards are present in a given Category, the final row may have 1 tile, 2 tiles, or 3 titles (if the total count is divisible by 3)
    • When there are less than 3 tiles in the final row, I need to add a "shim", so the final tile is the same width as the rest of the tiles.

    I plan to achieve this by getting a count of the boards and calculating the remainder when divided by 3:

     

    <#assign boardCount= rest("categories/id/{coreNode.title}/boards/count?restapi.response_style=view").value/>

    <#macro repeat count>
    <#list 1..count as x>
    <#nested>
    </#list>
    </#macro>

    <@repeat count={boardCount % 3} >
    <li class="detail-tile-shim"></li>
    </@repeat>

    If I hard code the count to be something like "2" it works just fine but I keep getting errors when adding a variable as the value of "count" in that macro. 

  • cike's avatar
    cike
    Champion
    8 years ago

    Hi cstone,

     

    simply remove the curly braces from the variable name you want to use in your macro:

     

    <#assign count = boardCount % 3 />
    <@repeat count=count >
    <li class="detail-tile-shim"></li>
    </@repeat>

     

    Cheers,

    Christian

  • Thanks cike! This did the trick for me. Here was my final solution, in case this post helps out someone else:

    <#assign boardCount= rest("categories/id/sales/boards/count?restapi.response_style=view").value?number/> // apparently this value is a string
    <#assign shimCount = 3 - (boardCount % 3) />
    
    <#macro repeat count>
      <#if count != 3>
        <#list 1..count as x>
          <#nested>
        </#list>
      </#if>
    </#macro>
    
    <@repeat count=shimCount >
      <li class="detail-tile-shim"></li>
    </@repeat>