3

I'm making a DWT component template that loops multivalued componentlinked components. I want to check if a textfield in a linked component is empty, but it always gives errors. I need to use the Dreamweaver Get extention, because otherwise I can't get to my deepest linked properties:

<!-- TemplateBeginRepeat name="Component.Fields.AfbeeldingLink" -->

<img  src="@@Get("Fields.AfbeeldingLink[${TemplateRepeatIndex}].Fields.Afbeelding")@@" alt="@@Get("Fields.AfbeeldingLink[${TemplateRepeatIndex}].Fields.Afbeelding.Metadata.AltTekst")@@" />

<!-- TemplateEndRepeat -->

This works fine. But now I want to check if a property like

@@Get("Fields.AfbeeldingLink[${TemplateRepeatIndex}].Fields.Afbeelding.Metadata.AltTekst")@@

is empty to do something different. How can I do that? This always gives errors:

<!-- TemplateBeginIf cond="StringLength(@@Get("Fields.AfbeeldingLink[${TemplateRepeatIndex}].Fields.Afbeelding.Metadata.AltTekst")@@) > 0" --> 

Like ' a ; is needed'.

Does anybody has an idea ?

Kind regards, Herman

3

When calling TemplateCallable methods from within Conditions, don't use @@ . Simply use the method name as :

<!-- TemplateBeginIf cond="Get(...) !=''" -->

I had few TemplateCallable custom methods implemented as well and the below syntax always worked for checking if the AltText field is Empty or NOT

<!-- TemplateBeginIf cond="GetMetaDataFieldValue(Component.ArticleImage,'AltText') !=''" -->

The method GetMetaDataFieldValue has return type of Stringand using !='' always worked. Note that there is NO space inbetween the single quotes in !=''

The Dreamweaver's Get() method's return type is String as well.

| improve this answer | |
  • I tried this solution, and I got no error, but it gives always TRUE, so the if statement is always executed: <!-- TemplateBeginIf cond="Get("Fields.AfbeeldingLink[${TemplateRepeatIndex}].Fields.Link.ExterneLink")!=''" --> – Herman Desmet Sep 9 '15 at 14:23
  • Is the Field Empty ? – R.C Sep 9 '15 at 14:41
  • As a side check - you can try printing the Field value using @@Get()@@ somewhere else in DWT to see if it really prints/has some value or is Empty – R.C Sep 9 '15 at 15:00
1

Generally speaking checking for empty fields or null values in DWT TBBs means you need to check if the value contains an empty string, so:

<!-- TemplateBeginIf cond="@@Get("Fields.AfbeeldingLink[${TemplateRepeatIndex}].Fields.Afbeelding.Metadata.AltTekst")@@ == ''" -->

But its been too long for me to remember if the Get extension actually was able to return you empty strings for non existing fields (since when a field is empty, it doesn't have an element in the XML)

You might need to extend the Get extension with a IsEmpty() method, which is something I would actually do anyways, since that will clean up your Template code in a great way:

<!-- TemplateBeginIf cond="@@IsEmpty("Fields.AfbeeldingLink[${TemplateRepeatIndex}].Fields.Afbeelding.Metadata.AltTekst")@@" -->

Well okay not "great", but slightly better, if you want something decent, readable and understandable, I would take a look at DXA and ditch DWT Templating alltogether ;o).

UPDATE

Good point to mention you got the error when trying to use @@ inside a condition, now that my memory of DWT Templating is refreshing, I seem to recall there was a limitation we had with custom functions, being that they could not be used in a condition. So looks like my answer will not help you at all, apart from the last bit ;o)

But since I'm digging from memory of years ago (which is likely to be only partially correct), I might still be wrong on this, you could give it a try without the @@ signs in the condition.

| improve this answer | |
  • Unfortunately it doesn't work. It seems that if I try to acces a @@ inside a cond, it always gets the error: "; expected". – Herman Desmet Sep 9 '15 at 11:12
1

Thanks all for your help, I finally got it working!

It seems that when my value was empty, the get-function returned 'undefined'. Also, the single quote-solution from above helped, then I got no errors when I surrounded the @@Get@@ with single quotes. The working example is this one:

<!-- TemplateBeginIf cond="'@@Get("Fields.AfbeeldingLink[${TemplateRepeatIndex}].Fields.Link.ExterneLink")@@'!='undefined'" -->

Kind regards and big thanks! Herman

| improve this answer | |
0

I think this is just due to missing quotes.

StringLength(@@Get("your_field_path")@@)

will likely render to:

StringLength(This is my field value)

This will error since the rendered string isn't in quotes.

Try adding single quotes:

StringLength('@@Get("your_field_path")@@')

Also you may not even need StringLength, you could probably get away with

<!-- TemplateBeginIf cond="'@@Get("your_field_path")@@'" -->

I'll pre-empt a "gotcha". If your field contains a quote then this will likely break (since you're effectively rendering into a condition then trouble is likely to follow!).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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