Trying to add am 'if statement' to Co Directory and failing Topic is solved

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
CapereSpiritum
Forum Members
Forum Members
Posts: 223
Joined: Wed Dec 28, 2011 12:11 pm

Trying to add am 'if statement' to Co Directory and failing

Post by CapereSpiritum »

Hello

I have added the following statement ina Company Directory details template.
<h1>{$entry->company_name} {$categorytext}</h1>

There several categories that an entry might be included in but I don't want to be included in what '{$categorytext}' displays.
Say there are categories that apply to an entry (A,B,C,D,E,F,G and H) but I don't want '{$categorytext}' to display categories (D & F).
Is there a way to exclude displaying specified categories from the <h1> tag above?

I've tried writing an {if} statement but I simply don't understand how.

I hope someone can shed some light.

I am using:
CMSMS Version 2.1.6

Installed Modules
AdminSearch 1.0.2
Album 1.11
CGCalendar 2.4.4
CGExtensions 1.55.6
CGFeedback 1.8.3
CGGoogleMaps2 1.1.1
CGSimpleSmarty 2.1.6
CGSmartImage 1.21.9
CGStaticMaps 1.0.2
CMSContentManager 1.1.4
CMSMailer 6.2.14
Captcha 0.5.5
CompanyDirectory 1.23.5
DesignManager 1.1.1
FileManager 1.5.2
FormBuilder 0.8.1.6
JQueryTools 1.4.0.3
MicroTiny 2.0.3
ModuleManager 2.0.5
Navigator 1.0.3
News 2.50.6
Search 1.50.2
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1627
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Trying to add am 'if statement' to Co Directory and failing

Post by DIGI3 »

I think we'll need more information, like how you're creating the $categorytext variable.

If you're doing an if statement on $item->categories it's a bit more complicated because that's an array. Without actually knowing what you're attempting, here's a few possible options that may help:

Code: Select all

{if array_key_exists('D', $entry->categories) || array_key_exists('F', $entry->categories)}
  don't do the thing
{else}
  do the thing
{/if}
(permissive smarty will need to be enabled for this one)

or

Code: Select all

{foreach from=$entry->categories item=category}
  {if $category->name != 'D' && $category->name != 'F'}
    do thing
  {/if}
{/foreach}
Not getting the answer you need? CMSMS support options
CapereSpiritum
Forum Members
Forum Members
Posts: 223
Joined: Wed Dec 28, 2011 12:11 pm

Re: Trying to add am 'if statement' to Co Directory and failing

Post by CapereSpiritum »

Hi DIGI3

Many thanks for your reply.

Here's a page from the site that shows what I'm trying to do
https://idocanals.co.uk/p-and-s-marine.html

The relevant categories are shown here in white text underneath the <h1> page title (P and S Marine)

The categories (part of the <h1>page title), comma separated, immediately below are:
Marinas and Moorings, Boat Builders and Fitters, Boat Blacking and Anodes, Crane Lift In or Out, Docking Hardstanding and Storage, Heating and Cooling, Boat Handling, Of Interest

I have deliberately added 'Of Interest', shown last.

How do I NOT show 'Of Interest' and any other category I desire.

I plan to add every Canal Name (as categories) in the UK. There will be a canal name (category) for every entry but I DON'T want to show them in the <h1> page title.

Which is currently this '<h1>{$entry->company_name} {$categorytext}</h1>'

Hope this helps
Thanks
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1627
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Trying to add am 'if statement' to Co Directory and failing

Post by DIGI3 »

I was going to suggest using one of the Extra Data fields to specify which categories are to not display, but those seem to be not working on the version I tested. If you're not using the description field you could use that, just put a value in the description field of any category you don't want to display, then something like:

Code: Select all

{foreach from=$entry->categories item=category}
  {if !$category->description}
    {$category->name}{if !$category@last}, {/if}
  {/if}
{/foreach}
Not getting the answer you need? CMSMS support options
CapereSpiritum
Forum Members
Forum Members
Posts: 223
Joined: Wed Dec 28, 2011 12:11 pm

Re: Trying to add am 'if statement' to Co Directory and failing

Post by CapereSpiritum »

Perfect
That works perfectly, thank you.
Now just trying to find css that will hide last un-needed commar.

Thanks Lots - I will mark as SOLVED
8)
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1627
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Trying to add am 'if statement' to Co Directory and failing

Post by DIGI3 »

Right - my code doesn't quite work for hiding the comma unless the last category is one you want to show. Easiest fix would be to get rid of the {if !$category@last}, {/if} and do a <span class="comma">, </span> and hide last-of-type in your css. There are ways to do it in Smarty too but gets complicated in this case.
Not getting the answer you need? CMSMS support options
CapereSpiritum
Forum Members
Forum Members
Posts: 223
Joined: Wed Dec 28, 2011 12:11 pm

Re: Trying to add am 'if statement' to Co Directory and failing

Post by CapereSpiritum »

Cracking
That works too

Thanks very much ;D
CapereSpiritum
Forum Members
Forum Members
Posts: 223
Joined: Wed Dec 28, 2011 12:11 pm

Re: Trying to add am 'if statement' to Co Directory and failing

Post by CapereSpiritum »

Hi DIGI3

Sorry to bug you again.

I've tried a dozen variations of your $category script to do something else but just get errors or wrong result.

I have created 28 icons to represent service categories. I'm trying to use your script so that if a certain category is valid that an icon will show.

Rather like this

Code: Select all

{if isset($entry->fields.Marinas) && $entry->fields.Marinas->value != ''}<div class="ResEntrySvcIcon"><img src="http://idocanals.co.uk/uploads/IDoCanals/SvcIcons/Icon-Marina-40.png"></div>{/if}
I know the above is wrong, but I think you'll get the intent.

The issue here is there are already 530+ entries and many with a dozen categories or more. The time to get it done is several days and it's very inefficient.

Can your script be adapted?

I've tried various things like this

Code: Select all

{if !$category->name && $category->name->value != 'Marinas and Moorings'}Do a Thing{else}Do nothing{/if}
This only does the {else} no matter what.

Thanks lots for your help already
Simon
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1627
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Trying to add am 'if statement' to Co Directory and failing

Post by DIGI3 »

$category->name->value isn't a thing, for that last bit you'd just want to test if $category->name.

Personally I think I would go about this differently. What about creating a <span class="icon_{$category->name}"></span> for each category the item is part of, then for any that you want an icon for you do that in your css. For any with no icon you can do nothing.

Another option would be to put the icon in the description field for the category. No description, no icon.
Not getting the answer you need? CMSMS support options
CapereSpiritum
Forum Members
Forum Members
Posts: 223
Joined: Wed Dec 28, 2011 12:11 pm

Re: Trying to add am 'if statement' to Co Directory and failing

Post by CapereSpiritum »

Hi DIGI3

I can see how that will work. Many thanks.

I'm search the net for a CSS function that will hide all underscores in the H1 '_'
This is because spaces are not allowed in a css class. So if I modify the relevant category names to include underscores instead of spaces.

They don't look too good in the H1 <title>

So far, I can only affect the first or last. It does not look like there is CSS to color ALL underscores in a specific class.

So, trying a javascript

Code: Select all

let elements = document.querySelectorAll('.tagrem');   
elements.forEach(e => e.innerText = e.innerText.replaceAll('_', ' '))
I added the id 'tagrem' into the span that covers the categories in the H1 title.
My experience in JS is very little. Reading up on W3schools. Slow going

I'll keep looking ;D
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1627
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Trying to add am 'if statement' to Co Directory and failing

Post by DIGI3 »

Do a <pre>{$category|print_r}</pre> in your foreach to see if there's another field you could use that's more suitable for an id/class name. If not, you can do something like:

Code: Select all

{$category->name|replace:' ':'_'}
if spaces are the only issue, or

Code: Select all

{$category->name|escape:'url'}
if there's other entities as well as spaces (I haven't tested this, see https://www.smarty.net/docs/en/language ... escape.tpl for more info)

I wouldn't suggest javascript for making invalid html into valid.
Not getting the answer you need? CMSMS support options
Post Reply

Return to “Modules/Add-Ons”