Page 1 of 1

Help with an IF statement in Company Directory

Posted: Tue Jul 04, 2017 8:32 pm
by CapereSpiritum
Hi Folks

I'm using Company Directory for a Music Venue and Bands Artists Directory.

As CD does not support 'Pretty URLs' for some of the features.
i.e. detailtemplate, I'm having to use a single detail template for several types of details.

One of the problems I have is this.
Venues have facilities: Car Park, Pool Table, Jukebox, Baby Changing etc

Bands and Artists don't.

I need to enter an 'IF' statement that, if none of the facilities have an entry, nothing is created (that's easy, even I can do that), but...

If there is one or more of the facilities added, I want to create a div with an h4 header and with all the entries in.

What is the correct syntax for, in my case 14 possible entries?
Is there a way or adding all 14 Field Definitions to a single IF statement and if just one has an entry create the div etc.

Here's just one of the Field Definitions

Code: Select all

{if isset($entry->fields.BeerGarden) && $entry->fields.BeerGarden->value != ''}<div class="VBeerGdn row col-tweleve cf">{$entry->fields.BeerGarden->value}</div>
{/if}
I know the following is wrong, but am I way off or near the mark?

Code: Select all

{if isset($entry->fields.Jukebox,LiveSportBigScreen,PoolTable,SnookerTable,DartBoard,FruitMachine,DisabledAccessToilet,SmokingArea,BabyChanging,DogFriendly,WiFi,CarPark,DanceFloor) && $entry->fields.Jukebox,LiveSportBigScreen,PoolTable,SnookerTable,DartBoard,FruitMachine,DisabledAccessToilet,SmokingArea,BabyChanging,DogFriendly,WiFi,CarPark,DanceFloor->value != ''}{/if}
Any help appreciated for an amateur in php. I'm way better at CSS and Graphic Design ;D

Re: Help with an IF statement in Company Directory

Posted: Wed Jul 05, 2017 8:27 am
by CapereSpiritum
Ignore the request above.
I added an IF statement that relates only to one type of entry that has premises... {if $entry->address ne ''}

As Bands and Artists don't have a fixed address, I am able to 'not show' other entries based upon no address entered.

Long winded, but it works...

If anyone can show the actual answer to my request, I'd be interested anyway.

Thanks

Re: Help with an IF statement in Company Directory

Posted: Wed Jul 05, 2017 9:46 am
by velden

Code: Select all

{if (isset($entry->fields.BeerGarden) && $entry->fields.BeerGarden->value != '') || (isset($entry->fields.Jukebox) && $entry->fields.Jukebox->value != '') || (...etc..)}
Show header
{/if}
That would be one solution. Just as an example.

Note the parantheses around the AND 'statements'.

IF (A AND B) OR (C AND D) OR (E AND F)
&& = AND
|| = OR

http://www.smarty.net/docs/en/language.function.if.tpl

In real life you might want to do the check per field once and safe the result in variables. But it's not very easy to do in Smarty and doubt if it will be more readable.

pseudo code:

Code: Select all

{$result=''}
{if (isset($entry->fields.BeerGarden) && $entry->fields.BeerGarden->value != '')}
{$result=$result+<div class="VBeerGdn row col-tweleve cf">{$entry->fields.BeerGarden->value}</div>} <== this won't work
{/if}
{if (isset($entry->fields.JukeBox) && $entry->fields.JukeBox->value != '')}
{$result=$result+<div class="JukeBox row col-tweleve cf">{$entry->fields.JukeBox->value}</div>} <== this won't work
{/if}
...
{if $result != ''}
  <div>
    <h1>header</h1>
    {$result}
  </div>
{/if}


Re: Help with an IF statement in Company Directory

Posted: Wed Jul 05, 2017 2:15 pm
by calguy1000
In real life you might want to do the check per field once and safe the result in variables. But it's not very easy to do in Smarty and doubt if it will be more readable.
Incorrect, in any situation where an expression is long and has to be repeated, you execute the expression once, and save the results. You can also store the results in an array.

Also remember the empty() function which is shorter in this case and easy to use.

{$results=[]}
{* if you have more than 3 or 4 fields here I'd use a loop, it's cleaner *}
{if !empty($entry->fields.LiveMusic->value)}{$result[]='Live Music'}{/if}
{if !empty($entry->fields.JukeBox->value)}{$result[]='Juke Box'}{/if}
{if !empty($entry->fields.BeerGarden->value)}{$result[]='Beer Garden'}{/if}
...
{if !empty($results)}
<div class="results">
<h4>Results</h4>
<ul>
{foreach $results as $item}
<ul>{$item}</ul>
{/forach}
</ul>
</div>
{/if}

This is the simplest syntax I can think of and this is ugly and not recommended. So I suggest you spend a bit of time learning a bit more about CMSMS as it will save you alot of time and headache.

You really need to learn about some smarty concepts like:
- template inheritance
- include
and some cmsms concepts like the {cms_action_hint}

IN NO WAY should your templates try to accomplish more than one goal and be filled with all kinds of silly logic statements. CMSMS is smarter than that. you just have to learn a bit.

Re: Help with an IF statement in Company Directory

Posted: Wed Jul 05, 2017 3:55 pm
by calguy1000
As CD does not support 'Pretty URLs' for some of the features.
i.e. detailtemplate, I'm having to use a single detail template for several types of details.
This is where you need to learn about:
a: template inheritance
b: {cms_module_hint}

You could use a separate detail page for each category
on that detail page use {cms_action_hint module=CompanyDirectory detailtemplate='category_specific_detail_template'}

Your category_specific_detail_template could 'extend' a base category detail template that has all of the common functionality, and just changes one or two custom 'blocks'.

Easy, and clean.

Re: Help with an IF statement in Company Directory

Posted: Tue Aug 15, 2017 5:05 pm
by CapereSpiritum
Hi Calguy

Many thanks for the replies.
I hope to put them to good use.

The website is nearing completion temp address http://teessiders.artworknetwork.co.uk/

This is likely to be my last website for a client as my wife and I are taking over a pub very soon. Other than a website for the pub, there is little likelihood of my needing to create any more.

CMSMS is a brilliant CMS and I have really enjoyed using it. Even the time I have pulled my hair out ;D

I'd like to thank you and all the other people that have responded to my queries and wish you well for the future.
Best regards
Simon Fall (Lunatic Chef)

Re: Help with an IF statement in Company Directory

Posted: Wed Aug 16, 2017 11:43 am
by paulbaker
Good luck with the pub! O0