简体   繁体   中英

How to check if the field in embedded schema is empty in Dreamweaver Template Building Block

I am using SDL Tridion 2011 SP1. I am creating the Dreamweaver TBB for a component. In my component some of the fields are empty. But in my component TBB, I want to check if the field is empty and I should not render it. If field is not empty then I should render and display the value.I am facing the problem when checking the content of subfield in embedded field.

On my component there is one multi-value embedded schema field with name "EMBFIELD". The EMBFIELD schema has a text field with the name "text". I want to check if the text field is empty or not. If it is not empty I have to iterate over the field to render the values.

I have to render the field by "RenderComponentField" only. When I tried rendering it is showing some error that the field doesn't exist.

I thought that this can be done using If block.

 <!-- TemplateBeginIf cond="Component.Fields.EMBFIELD" --> 
     <!-- TemplateBeginRepeat name="Component.Fields.EMBFIELD" -->
           <!-- TemplateBeginIf cond="Component.Fields.EMBFIELD.text" --> 
                 <div>@@RenderComponentField("Component.Fields.EMBFIELD.text",TemplateRepeatIndex)@@</div>
            <!-- TemplateEndIf -->      
     <!-- TemplateEndRepeat -->
 <!-- TemplateEndIf -->

But it's giving error like

Internal error: Context component Component does not have field Component.Fields.conditionalText.text

You should be able to use the Dreamweaver conditional regions to check for a value before you attempt to render it.

For example:

<!-- TemplateBeginIf cond="Component.Fields.Field" -->
    @@Component.Fields.Field@@
<!-- TemplateEndIf -->

You could use StringLength(object parameter) function, it will return 0 if the field is empty or if the string length of the parameter could not be determined. So all in all it should look like this:

<!-- TemplateBeginIf cond="StringLength(Component.Fields.Field) > 0" -->
 <b>Value is not empty<b>
 <p>@@Component.Fields.Field@@</p>
<!-- TemplateEndIf -->

This might be the answer to your updated question:

<!-- TemplateBeginIf cond="Component.Fields.EMBFIELD.text" --> 
     <!-- TemplateBeginRepeat name="Component.Fields.EMBFIELD" -->
            @@RenderComponentField("EMBFIELD[${TemplateRepeatIndex}].text", 0)@@       
     <!-- TemplateEndRepeat -->
 <!-- TemplateEndIf -->

We got the same issue with the same error while accessing the embedded schema field and after tons of back and forth with the builder and debugging dwt understood that the error is about NOT putting a space between the last double qoute and the --> in the TemplateBeginIf statement. : Errors out by DWT Mediator:

 <!-- TemplateBeginIf cond="EMBEDFIELD.internalLink || EMBEDFIELD.externalLink || EMBEDFIELD.label"--> 

The below doesn't error out:

 <!-- TemplateBeginIf cond="EMBEDFIELD.internalLink || EMBEDFIELD.externalLink || EMBEDFIELD.label"<PUT_A_SPACE_HERE>--> 

It may be any TemplateBeginIf surrounding the RenderComponentField statement getting errored out. Hope this helps someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM