Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#457 2017-03-16 22:45:22

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,306

Re: smd_if: Generic multiple if condition tests

Alicson, here’s a plugin-less solution. Plus several other approaches discussed in a specific topic.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#458 2017-03-17 01:26:16

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: smd_if: Generic multiple if condition tests

I gather you’re trying to cut back on plugins, but while uli is of course right that you can do section front-page checks with built-in tags, it does tend to require a rather big stack of them, so here’s another option to consider (with apologies for promoting my own plugin in a different plugin support thread!):

soo_if_frontpage


Code is topiary

Offline

#459 2017-03-18 04:00:12

alicson
Member
Registered: 2004-05-26
Posts: 465
Website

Re: smd_if: Generic multiple if condition tests

Thank you very much to both of you! I am cutting down on plugins, however have also been trying to cut down on extensive nested conditionals—of which I have (too) many. @uli, I really appreciate being pointed to resources; I’ve only just discovered txp:variable and I don’t think I fully understand its attributes yet; I am digesting and learning. And @jsoo your plugin offer is greatly appreciated, and will probably be the right answer for me; very practical functionality to have.


textpattern.org :: find and share Textpattern resources
docs.textpattern.io :: Textpattern user documentation

Offline

#460 2018-04-19 13:42:26

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: smd_if: Generic multiple if condition tests

I use

<txp:variable name="social" value='<txp:custom_field name="social media" />' />	
<txp:smd_if field="txpvar:social" operator="contains" value="instagram.com"> 
	<span>instagram</span> 
</txp:smd_if>
<txp:smd_if field="txpvar:social" operator="contains" value="twitter.com"> 
	<span>twitter</span> 
</txp:smd_if>
<txp:smd_if field="txpvar:social" operator="contains" value="facebook.com"> 
	<span>facebook</span> 
<txp:else />
	<span>Other Social Media</span> 
</txp:smd_if>

Problem is, that if any but the last smd_if clauses return true, the last one still returns the code after the txp:else. How can i return this code only, when none of the above options returned true?

Last edited by jayrope (2018-04-19 13:43:50)


A hole turned upside down is a dome, when there’s also gravity.

Offline

#461 2018-04-19 13:48:44

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,071
Website GitHub Mastodon Twitter

Re: smd_if: Generic multiple if condition tests

I think that the name of the social media custom field should not contain any spaces.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#462 2018-04-19 14:02:23

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: smd_if: Generic multiple if condition tests

Colak, that’s just what glz_cf does to my custom fields.

However it isn’t the source of error.
Just imagine the field would be named social-media instead. The result still is the same. I don’t see how to check for all values with individual returns, when true, and only return the exception, when none of the other checks returned true.

Last edited by jayrope (2018-04-19 14:02:53)


A hole turned upside down is a dome, when there’s also gravity.

Offline

#463 2018-04-19 14:06:03

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: smd_if: Generic multiple if condition tests

Reading through the code:

<txp:variable name=“social” value=’<txp:custom_field name=“social media” />’ />	

<txp:smd_if field=“txpvar:social” operator=“contains” value=“instagram.com”>
	<span>instagram</span> 
</txp:smd_if>

<txp:smd_if field=“txpvar:social” operator=“contains” value=“twitter.com”>
	<span>twitter</span> 
</txp:smd_if>

<txp:smd_if field=“txpvar:social” operator=“contains” value=“facebook.com”>
	<span>facebook</span> 
<txp:else />
	<span>Other Social Media</span> 
</txp:smd_if>

If social contains instagram.com output instagram.

If social contains twitter.com output twitter.

If social contains facebook.com output facebook else output Other Social Media.

So if social contains something other than facebook.com (like instagram.com or twitter.com) you will get Other Social Media.

I assume that isn’t what you want. But something more like this.

<txp:variable name=“social” value=’<txp:custom_field name=“social media” />’ />	
<txp:variable name=“other” value="true" />	

<txp:smd_if field=“txpvar:social” operator=“contains” value=“instagram.com”>
	<span>instagram</span> 
	<txp:variable name=“other” value="false" />	
</txp:smd_if>

<txp:smd_if field=“txpvar:social” operator=“contains” value=“twitter.com”>
	<span>twitter</span> 
	<txp:variable name=“other” value="false" />	
</txp:smd_if>

<txp:smd_if field=“txpvar:social” operator=“contains” value=“facebook.com”>
	<span>facebook</span> 
	<txp:variable name=“other” value="false" />	
</txp:smd_if>

<txp:if_variable name="other" value="true">
	<span>Other Social Media</span> 
</txp:if_variable>

Offline

#464 2018-04-19 14:19:45

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: smd_if: Generic multiple if condition tests

Thank you for your reply, Michael.
I assume your last if_variable check should be named “other” instead of “homepage”?
Testing your script i get “Other Social Media” returned on every smd_if clause that returned true.


A hole turned upside down is a dome, when there’s also gravity.

Offline

#465 2018-04-19 14:31:58

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,071
Website GitHub Mastodon Twitter

Re: smd_if: Generic multiple if condition tests

What happens if you use native tags?

<txp:variable name="social" value='<txp:custom_field name="social media" />' />	
<txp:if_variable name="social" value="instagram.com" /> 
	<span>instagram</span> 
<txp:else />
<txp:if_variable name="social" value="twitter.com"> 
	<span>twitter</span> 
<txp:else />
<txp:if_variable name="social" value="facebook.com"> 
	<span>facebook</span> 
<txp:else />
	<span>Other Social Media</span> 
</txp:if_variable>
</txp:if_variable>
</txp:if_variable>

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#466 2018-04-19 14:44:41

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: smd_if: Generic multiple if condition tests

jayrope wrote #311216:

I assume your last if_variable check should be named “other” instead of “homepage”?

I need to do better editing when copying thing from the docs.

Testing your script i get “Other Social Media” returned on every smd_if clause that returned true.

Weird.

One thing I am not quite sure of: are you storing multiple social networks in the same variable? Is that why you are using the contains operator rather than the native comparison as colak mentions?

Offline

#467 2018-04-19 14:47:01

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: smd_if: Generic multiple if condition tests

Yannis, the reason why i didn’t use native tags is, because txp:if_variable doesn’t seem to be able to test just for parts of a string.
My custom field contains complete links, which i only want to test for partials strings,
like testing https://www.facebook.com/janek.kruszynski.58 for facebook.com.

Edit: However, i think the nesting is a good idea. I’ll try that again.

Last edited by jayrope (2018-04-19 14:47:28)


A hole turned upside down is a dome, when there’s also gravity.

Offline

#468 2018-04-19 14:54:03

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: smd_if: Generic multiple if condition tests

Solution is to nest smd_if tags with a txp:else at the very end.

Thanx everybody!


A hole turned upside down is a dome, when there’s also gravity.

Offline

Board footer

Powered by FluxBB