Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,159
Website

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.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
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: 651
Website

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,313
Website

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: 651
Website

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: 8,159
Website

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.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
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,313
Website

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: 651
Website

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: 651
Website

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

#469 2018-04-19 14:59:54

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,637
Website

Re: smd_if: Generic multiple if condition tests

Nesting is the only way to go at the moment, and is the most efficient. If I introduced some way to indicate which value from a set of things matched, you could do it all in one tag call, but it’s not easy right now (I tried and got 90% of the way there).

If you’re on 4.7.0, you can use the not attribute to switch the logic around and test for that first:

<txp:variable name="social"><txp:custom_field name="social media" /></txp:variable>

<txp:smd_if field="txpvar:social" operator="contains, contains, contains" not value="instagram, facebook, twitter" logic="or">
    <span>Other Social Media</span>
<txp:else />
    <txp:smd_if field="txpvar:social" operator="contains" value="instagram">
        <span>Instagram</span>
    </txp:smd_if>
    <txp:smd_if field="txpvar:social" operator="contains" value="facebook">
        <span>Facebook</span>
    </txp:smd_if>
    <txp:smd_if field="txpvar:social" operator="contains" value="twitter">
        <span>Twitter</span>
    </txp:smd_if>
</txp:smd_if>

Not very scalable :-(

You really are best off nesting. And if you are running 4.7, remember you can use short tags!

<txp:variable name="social"><txp:custom_field name="social media" /></txp:variable>

<smd::if field="txpvar:social" operator="contains" value="instagram">
    <span>Instagram</span>
<smd::else />
    <smd::if field="txpvar:social" operator="contains" value="facebook">
        <span>Facebook</span>
    <smd::else />
        <smd::if field="txpvar:social" operator="contains" value="twitter">
            <span>Twitter</span>
        <smd::else />
            <span>Other social media</span>
        </smd::if>
    </smd::if>
</smd::if>

The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#470 2018-04-19 15:05:56

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

Re: smd_if: Generic multiple if condition tests

michaelkpate wrote #311219:

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?

Michael, yes, what i do is store complete URLs to social media sites in a custom field first.
Then i look for partial strings like “facebook” or “tumblr” etc.
If one of those returns true, i use the link from the custom field to link a custom text to the respective website.
That way “facebook” links to Facebook, “tumblr” to Tumblr etc.
If a social media website doesn’t appear in the predefined list of partial strings, then i simply use the custom field content to display “social media site” and link to that respective site.


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

Offline

Board footer

Powered by FluxBB