Page 1 of 1

[LISE] check if field definition content empty

Posted: Fri Aug 14, 2020 12:12 pm
by 10010110
OK, I’m displaying entries by category with LISE like this:

Code: Select all

<!-- category template -->
{if $categories > 0}
<section>
	<h2>Title</h2>
	<div class="categories">
	{foreach $categories as $cat}
		{$level = $cat->depth}
		{$prevlevel = $cat->prevdepth}
		{* close section *}
		{if $level === 1 && $prevlevel === 2}
		</section>
		{/if}
		{if $cat->fielddefs}
			{$item = $cat}
			{$fielddef = $cat->fielddefs}
		<a href="{$item->url}">{CGSmartImage src="{$fielddef.image->GetImagePath(true)}/{$fielddef.image.value}" alt="{$item->title}" title="{$item->title}" filter_resize='h,100'}</a>
		{else if $cat->items|count > 0}
		<section class="category-{$cat->alias}">
			<h3>{$cat->name}</h3>
		{/if}
	{/foreach}
	{* close last section *}
		</section>
	</div>
</section>
{/if}
<!-- end category template -->
I also have a field definition called “description” which is a WYSIWYG field. I’m now trying to check whether that field has any value and display the results differently depending on the condition, like in this example:

Code: Select all

{$fielddef = $cat->fielddefs}
{if !empty($fielddef.description)}
	has description
{/if}
But whatever I do, it never seems to be empty, even if there is no content. Is there any special trick to this?

Re: [LISE] check if field definition content empty

Posted: Fri Aug 14, 2020 2:04 pm
by DIGI3
Try changing

Code: Select all

{$fielddef = $cat->fielddefs}
{if !empty($fielddef.description)}
   has description
{/if}
to

Code: Select all

{$fielddef = $cat->fielddefs}
{if !empty($fielddef.description->value)}
   has description
{/if}
I haven't tested in a category view, but in a summary/detail view you need to include the ->value part to test for it, e.g. {$item->fielddefs.description->value}

Re: [LISE] check if field definition content empty

Posted: Sat Aug 15, 2020 5:09 pm
by 10010110
Hm, it doesn’t seem to work. Now it always sees the value as empty. Simplified code:

Code: Select all

{foreach $categories as $cat}
	{if $cat->fielddefs}
		{$cat->fielddefs.description->value}
		{if empty($cat->fielddefs.description->value)}
		empty
		{else}
		full
		{/if}
	{/if}
{/foreach}
If I put some text into the description field, it will output the text from {$cat->fielddefs.description->value} but still print “empty”. ???

Re: [LISE] check if field definition content empty

Posted: Sat Aug 15, 2020 5:13 pm
by DIGI3
maybe try just {if $cat->fielddefs.description->value}full{else}empty{/if}

or you could test the length or something. It does seem off, but perhaps something unique to category templates.

Re: [LISE] check if field definition content empty

Posted: Sat Aug 15, 2020 5:17 pm
by 10010110
Wow, thanks for the super quick reply. :) Indeed, just {if $cat->fielddefs.description->value} seems to do the trick.