1

I have a requirement to fetch the linked item from its linked sub item along with some linked item from the parent item's field. Below is the scenario.

I have a "Landing Page" In the "Landing Page", I have a field called "List", In the "List" field, I have linked one page called "Main List". In the Main List page, I have a field called "childrens", In the childrens field, I have linked few pages, I want to get these pages in my scriban along with some other items field value. Am facing difficulties getting the linked items from sub-item.

Below is the code I am trying with. Any suggestions and guidance would be thankful in advance.

{{
myList = i_item["List"]
$consolidatedItems = []

for eachLinkedItem in (sc_followmany i_item "Pages")
    $consolidatedItems = $consolidatedItems | array.add (eachLinkedItem)
end

$consolidatedItems = $consolidatedItems | array.add (myList["children's"])   
}}


{{$consolidatedItems | array.size}}
<br>

{{for i_item in $consolidatedItems}}
   {{i_item.path}}<br>
{{end}}

1 Answer 1

1

First - don't reuse the i_item variable, that should stay as the object passed in. Use a new variable name for your loop.

Next, you can just nest the sc_follow or sc_followmany to get further down the list.

{{for i_pageItem in $consolidatedItems}}
   {{i_pageItem.path}}<br>
   {{ for i_child in (sc_followmany i_pageItem "Childrens") }}
       <h2>{{ sc_field i_child "Title" }}</h2>
   {{ end }}
{{end}}

That will loop through the $consolidatedItems array and render the Title field on the target items.

You can do this as many times as you want, although I would advise that you should probably do something custom in your controller/repository if you have a complex set of rules regarding which items should be displayed. Try not to put too much logic in your scriban.

1
  • 1
    Thank You Richard. It solved my problem!
    – Jey
    Jan 29, 2021 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.