2

I have this Scriban template:

{{
  mediaId = sc_field i_item 'DropLinkField'

  item_By_Id = (sc_getitem mediaId)

  item_harcoded = "{ADAF43DF-BFD6-4444-9FE9-A030AB70B873}"

  get_the_url = (sc_getitem item_harcoded)

}}

<p>MediaId = {{mediaId}}</p>

<p>item_By_Id = {{item_By_Id}}</p>

<p>get_the_url = {{get_the_url}}</p>

<img src = "{{item_By_Id.media_url}}"/>

<img src="{{get_the_url.media_url}}"/>

Output

  • mediaId: {ADAF43DF-BFD6-4444-9FE9-A030AB70B873}
  • item_By_Id:
  • get_the_url: /sitecore/media library/Project/Experiment/KV/First/linkedin_icon
  • Image is displayed for the get_the_url one

If I put the 2nd last image tag (the one with the item_By_Id) then I get this error:

Scriban(11,26) : error : Cannot get the member item_By_Id.media_url for a null object.

Sitecore Scriban doesn't seem to support dynamic values. As from the output I can see that item_By_Id is empty.

5
  • What is the main purpose of the above code? Are you trying to get the media URL from an image field? Commented Jul 7 at 19:16
  • The droplink field populates the mediaItems. Once the content author choses the option, I want to render the image. So what I am doing is getting the item id of the chosen value. And then trying to get the media item as the src of img field. Commented Jul 7 at 19:33
  • Which version of SXA are you using? Did you try any of the options in here? sitecore.stackexchange.com/questions/33340/… Commented Jul 7 at 20:03
  • In that case, you could use sc_follow command: doc.sitecore.com/xp/en/developers/sxa/latest/… Commented Jul 7 at 21:33
  • @MarcelGruber I am using SXA 10.0. I have tried the options there. The problem I am facing is when I execute item_By_Id = (sc_getitem mediaId), it returns null which it shouldn't right? Commented Jul 8 at 4:50

3 Answers 3

1

Whenerver we use sc_field inside Scriban, it uses FieldRenderer pipeline in the background to get field value, therefore when you are in experience editor it will return the editable html as well.

sc_field function:

enter image description here

Editable HTML:

enter image description here

You can use below snippet directly to read the media url from the droplink using the help of sc_follow function.

{{ 
mediaItem = sc_follow i_item 'DropLinkField'
}}
{{ if mediaItem }} 
   <img src = "{{mediaItem.media_url}}"/>
{{ end }}

Using above way, you will be able to read droplink item id inside the CD server as well as experience editor.

Hope this helps!!!

1

The issue seems to be that item_By_Id is coming out as null, which indicates that the sc_getitem function is not able to retrieve an item with the ID mediaId. Here are a few steps to check the issue:

  1. Ensure the mediaId is correct: Verify that sc_field i_item 'DropLinkField' correctly retrieves the ID you expect. You can output mediaId to check its value.

  2. Check if the item exists: Ensure that an item with the ID mediaId exists in the Sitecore database.

If mediaId is not null and correctly formatted, but sc_getitem still returns null, there might be an issue with how sc_getitem retrieves the item.

Alternatively, you can try below mentioned code and check:

{{
   mediaId = sc_field i_item 'DropLinkField'
}}
<p>MediaId = {{mediaId}}</p>
{{
   item_By_Id = null
   # Check if mediaId is not empty or null
   if mediaId != empty_string && mediaId != null {
       item_By_Id = sc_getitem mediaId
   }
}}
<p>item_By_Id = {{item_By_Id}}</p>
{{
   item_harcoded = "{ADAF43DF-BFD6-4444-9FE9-A030AB70B873}"
   get_the_url = sc_getitem item_harcoded
}}
<p>get_the_url = {{get_the_url}}</p>
<img src="{{item_By_Id.media_url}}" alt="Image from item_By_Id" />
<img src="{{get_the_url.media_url}}" alt="Image from get_the_url" />
  1. mediaId is set from the DropLinkField.
  2. Before using sc_getitem, we check if mediaId is not empty or null to avoid the null object error.
  3. Only if mediaId is valid, we use sc_getitem to retrieve the item.
  4. This part remains unchanged as it was working correctly.
  5. Ensure that image tags use the media_url property.
1

Can you check updating 1st line as

mediaId = sc_raw i_item 'DropLinkField'

Hope this helps!!!

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