Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#451 2018-04-19 14:02:23
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
#452 2018-04-19 14:06:03
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
#453 2018-04-19 14:19:45
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
#454 2018-04-19 14:31:58
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
#455 2018-04-19 14:44:41
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
#456 2018-04-19 14:47:01
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
#457 2018-04-19 14:54:03
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
#458 2018-04-19 14:59:54
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.
Hire Txp Builders – finely-crafted code, design and Txp
Online
#459 2018-04-19 15:05:56
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
#460 2018-04-19 15:16:19
Re: smd_if: Generic multiple if condition tests
Stef, thanx for that.
Alternatively, couldn’t/shoudn’t if_variable be able to also test for parts of strings, similar to operator="contains" in smd_if?
In fact i use smd_if solely for that feature (yet).
And regarding the short tags in 4.7: Do i HAVE to use them or will the old tags still work? I suppose the latter.
Last edited by jayrope (2018-04-19 15:18:28)
A hole turned upside down is a dome, when there’s also gravity.
Offline
#461 2018-04-19 15:31:59
Re: smd_if: Generic multiple if condition tests
jayrope wrote #311224:
Alternatively, couldn’t/shoudn’t if_variable be able to also test for parts of strings
It can. Look at the match attribute. But in this case it won’t help you (much) because you still have to nest calls. However, it will mean you don’t need the plugin.
And regarding the short tags in 4.7: Do i HAVE to use them or will the old tags still work?
Entirely optional. It can make things more readable and shorter to type, that’s all.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Online
#462 2018-04-19 21:55:36
Re: smd_if: Generic multiple if condition tests
jayrope wrote #311214:
social media… that’s just what glz_cf does to my custom fields.
Is that with the new version? It should actively encourage you to alter any already existing custom fields like that to social_media (though it won’t force you to so as not to break code) when you save or edit the custom field. For new custom fields it automatically renames social media to social_media when it’s created and informs you.
You can now use custom field titles, so you can make it display on your write pane as “Social media” but name the custom field social_media. For your txp:tags you should use the name not the title.
jayrope wrote #311221:
Solution is to nest smd_if tags with a txp:else at the very end.
A couple more thoughts: With smd_if you can also test directly the content of a custom field so don’t need the variable method. I don’t know how well it copes with custom fields with a space in them, though.
Another alternative when things start to get “nesty” is to use smd_multi_choice to test for each case and if none are met to use the default. That can check a custom field directly and also supports contains as a test criteria.
TXP Builders – finely-crafted code, design and txp
Offline
#463 2018-04-27 12:36:43
Re: smd_if: Generic multiple if condition tests
Jakob, i cannot alter custom field names after i created them with glz_cf 1.5.0 on 4.6.2.
I guess i have to wait for 4.7.
The glz_cf 2.0 beta version there does support the change of the name.
As of smd_if or smd_multi_choice: which of those two do you think is more efficient in terms of calls to the database?
A hole turned upside down is a dome, when there’s also gravity.
Offline
#464 2018-04-27 12:51:12
Re: smd_if: Generic multiple if condition tests
jayrope wrote #311472:
As of smd_if or smd_multi_choice: which of those two do you think is more efficient in terms of calls to the database?
Neither touch the database: they both use Txp’s globals to do things. smd_multi_choice has a slightly convoluted syntax, but it does work to reduce the amount of nesting if you have a series of independent cases.
It could be argued as being more efficient than smd_if because it’ll only execute “once” for all your choices, whereas smd_if will get called multiple times for each test unless you nest them. But the best advice, if you can possibly get away with using native tags, is use those. That’s by far the preferred option.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Online
#465 2018-05-27 19:44:30
Re: smd_if: Generic multiple if condition tests
Thanx Stef.
On a minor note smd_if needs a
if (class_exists('\Textpattern\Tag\Registry')) { Txp::get('\Textpattern\Tag\Registry') ->register('smd_if'); } to stop complaining in debug mode. Just noting.
A hole turned upside down is a dome, when there’s also gravity.
Offline