Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

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: 687
Website Mastodon

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

#471 2018-04-19 15:16:19

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

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

#472 2018-04-19 15:31:59

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

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.

Txp Builders – finely-crafted code, design and Txp

Offline

#473 2018-04-19 21:55:36

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

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

#474 2018-04-27 12:36:43

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

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

#475 2018-04-27 12:51:12

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

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.

Txp Builders – finely-crafted code, design and Txp

Offline

#476 2018-05-27 19:44:30

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

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

#477 2018-08-27 03:29:11

lazlo
Member
Registered: 2004-02-24
Posts: 110

Re: smd_if: Generic multiple if condition tests

Hey Stef

Not sure if this affects anyone else but I upgraded a 4.5.7 site to smd_if 1.00 it didn’t work for me, I had to downgrade to 0.92. I didn’t see a version listing on the front page of plugin support or your page.

- L

Last edited by lazlo (2018-08-27 07:40:12)

Offline

#478 2018-10-27 16:56:16

Destry
Member
From: Haut-Rhin
Registered: 2004-08-04
Posts: 4,909
Website

Re: smd_if: Generic multiple if condition tests

Wondering if this is possible, or if there’s another way to get to it…

I have a partial of snake teeth that checks if there’s an article image or not and does its slither dance either way. The image is used as a hero and marked up as a figure so I can make use of the caption.

<txp:if_article_image>
      <hr>
      <p class="excerpt"><txp:excerpt escape="trim, tidy" /></p>
      <figure class="hero">
         <txp:article_image height="0" width="0" />
         <figcaption>
              <txp:image_info id='<txp:custom_field name="article_image" />' type="caption" escape="tidy,textile" />
          </figcaption>
      </figure>
    <txp:else />
      <hr class="startrule">
      <p class="excerpt"><txp:excerpt escape="trim, tidy" /></p>
      <hr class="endrule">
</txp:if_article_image>

Now, of course, I’ve run into a possible situation where I might not want every hero to have a caption. I can not add a caption in the field, but the figcaption pair still outputs. I couldn’t think how to get around this with core tags, so I thought maybe smd_if would be the ticket, but the docs are a tad obtuse for this peckerwood.

First I tried and hoped the new not functionality would work:

<txp:smd_if field="caption" not value="NULL">
     <figcaption>
         <txp:image_info id='<txp:custom_field name="article_image" />' type="caption" escape="tidy,textile" />
     </figcaption>
</txp:smd_if>

No dice.

Then I tried what I though was a shoe-in (note the else tag):

<txp:smd_if field="caption" value="NULL"><txp:else />
     <figcaption>
         <txp:image_info id='<txp:custom_field name="article_image" />' type="caption" escape="tidy,textile" />
     </figcaption>
</txp:smd_if>

Still nothing. An empty <figcaption></figcaption> still outputs.

Am I using the ‘NULL’ correctly?

Harlp?

Offline

#479 2018-10-27 17:11:32

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: smd_if: Generic multiple if condition tests

How about not using smd_if (sorry Stef) and using either the attribute wraptag="figcaption":

<txp:image_info id='<txp:custom_field name="article_image" />'
                type="caption" escape="tidy,textile"
                wraptag="figcaption" />

(you can write that all in one line) or if that doesn’t work as desired, using txp:evaluate to only output the code block when the tag returns a value:

<txp:evaluate test="image_info">
    <figcaption>
        <txp:image_info id='<txp:custom_field name="article_image" />' type="caption" escape="tidy,textile" />
    </figcaption>
</txp:evaluate>

TXP Builders – finely-crafted code, design and txp

Offline

#480 2018-10-27 17:15:27

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

Re: smd_if: Generic multiple if condition tests

Hi Destry,
Just to let you know that you can also do what you are trying to achieve without a plugin.

<txp:variable name="capt" value='<txp:images id='<txp:custom_field name="article_image" />' /></txp:images>' />
<txp:if_variable name="capt" value="">
<txp:else />
<figcaption>
<txp:image_info id='<txp:custom_field name="article_image" />' type="caption" escape="tidy,textile" />
</figcaption>
</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

Board footer

Powered by FluxBB