Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#16 2011-07-05 18:13:07

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,476
Website GitHub

Re: smd_multi_choice: select... case conditional-based output

frickinmuck wrote:

Is there a way to simply check if a value is not empty?

Sure: don’t use this plugin :-)

<txp:variable name="has_content">value or tag to check goes here</txp:variable>

<txp:if_variable name="has_content" value="">
   // do x
<txp:else />
   //do y
</txp:if_variable>

Does that help?


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

Offline

#17 2011-07-05 20:03:37

frickinmuck
Member
Registered: 2008-05-01
Posts: 118

Re: smd_multi_choice: select... case conditional-based output

That would be perfect, except I’m checking against multiple variables, as in this post, and I need to determine if each of those variables has content, then display content accordingly.


The AI does not hate you, nor does it love you, but you are made out of atoms which it can use for something else.

Offline

#18 2011-07-05 20:15:02

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,476
Website GitHub

Re: smd_multi_choice: select... case conditional-based output

Hang on a mo. Just trying to wrap my head round what you’re doing (and failing). Before I go totally bonkers with your logic, can I just ask why the code you posted here isn’t using <txp:smd_case default="1"> to catch the condition when there are no matches?

As it stands, your article tag is always going to display content. What I think you want is for it to output its content if it contains something. If you’re testing for type="empty" then, by definition, everything inside the default <txp:smd_case> must contain something, i.e. NOT empty.

Or have I misunderstood?


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

Offline

#19 2011-07-06 04:23:02

frickinmuck
Member
Registered: 2008-05-01
Posts: 118

Re: smd_multi_choice: select... case conditional-based output

OK, maybe I’m just misunderstanding how to use this, then. I’ve got 4 variables that I need to check, and so far I’ve only gotten so far as testing them separately, as until I can wrap my head around that, I’m not going to have much hope of dealing with nesting them, etc. Here’s the 4 cases I have:

<txp:smd_switch item="?coverageclient">
   <txp:smd_case type="empty">
   </txp:smd_case>
      <txp:article_custom section="coverage" coverageclient='<txp:variable name="coverageclient" />' form="article_listing" limit="999" />
</txp:smd_switch>
<txp:smd_switch item="?coveragemediaoutlet">
   <txp:smd_case type="empty">
   </txp:smd_case>
      <txp:article_custom section="coverage" coveragemediaoutlet='<txp:variable name="coveragemediaoutlet" />' form="article_listing" limit="999" />
</txp:smd_switch>
<txp:smd_switch item="?coveragetype">
   <txp:smd_case type="empty">
   </txp:smd_case>
      <txp:article_custom section="coverage" coveragetype='<txp:variable name="coveragetype" />' form="article_listing" limit="999" />
</txp:smd_switch>
<txp:smd_switch item="?coveragecause">
   <txp:smd_case type="empty">
   </txp:smd_case>
      <txp:article_custom section="coverage" coveragecause='<txp:variable name="coveragecause" />' form="article_listing" limit="999" />
</txp:smd_switch>

I guess that since the values are going to be stuff like, “?coveragemediaoutlet=CNN+Situation+Room”, I got confused as to what to validate for. There won’t be many numbers, but it’s not just letters, either. It doesn’t help that I’m trying to do all this when my brain is at about 70%. I used to be able to reason all this kind of stuff out a lot better, but these days I just get confused so easily, I feel like a turtle that’s rolled over on its back. :^)

Last edited by frickinmuck (2011-07-06 04:23:20)


The AI does not hate you, nor does it love you, but you are made out of atoms which it can use for something else.

Offline

#20 2011-07-06 09:15:06

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,476
Website GitHub

Re: smd_multi_choice: select... case conditional-based output

frickinmuck

Doesn’t this do what you’re after then:

<txp:smd_switch item="?coverageclient">
   <txp:smd_case type="empty">
   </txp:smd_case>
   <txp:smd_case default="1">
      <txp:article_custom section="coverage" coverageclient='<txp:variable name="coverageclient" />' form="article_listing" limit="999" />
   </txp:smd_case>
</txp:smd_switch>

And repeat for the other 4 vars. That code says:

  1. look at coverageclient
  2. is it empty? If so, do nothing
  3. if not, display the article

Your version did this:

  1. look at coverageclient
  2. is it empty? If so, do nothing
  3. display the article, regardless of the outcome of the above test

Subtle difference, but an important one. The plugin only ever takes one branch (one case statement) unless you allow fallthru, so that structure should do your bidding. Ummm, I think.

Last edited by Bloke (2011-07-06 09:15:59)


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

Offline

#21 2011-07-06 16:34:15

frickinmuck
Member
Registered: 2008-05-01
Posts: 118

Re: smd_multi_choice: select... case conditional-based output

That’s not a subtle difference at all! I consider it a fairly blatant one! hehe. Thanks so much. It’s working brilliantly now. You’ve been a tremendous help.


The AI does not hate you, nor does it love you, but you are made out of atoms which it can use for something else.

Offline

#22 2011-07-08 03:08:06

frickinmuck
Member
Registered: 2008-05-01
Posts: 118

Re: smd_multi_choice: select... case conditional-based output

I’ve run into one last issue that I can’t seem to get my head around. I want a few articles to appear by default, but then when someone selects a parameter from the pull-down menus (i.e. the URL variables are in play), the only articles that appear are the ones selected. So, for example, by default the most recent 3 articles in the section appear, but then if the user chooses a media outlet “BBC”, then they only see the BBC articles.

This seemed simple at first, because I have the landing page for that section set to a specific article. I just placed a similar bit of code in the article as is on the page template itself, and it worked great:

<txp:smd_switch item="?coverageclient">
   <txp:smd_case type="empty">
   <txp:article_custom section="coverage" form="article_listing" limit="3" />
   </txp:smd_case>
</txp:smd_switch>

The problem, of course, is that it only works for “coverageclient”. If I select one of the other variables (i.e. “coveragemediaoutlet”), then of course the variable “coverageclient” is now empty, so I get the 3 latest articles, followed by the articles from the media outlet that was selected. (i.e. as in the previous example, the 3 latest articles from the section followed by the articles from the “BBC”). I guess I need to nest them or get it to check against all 4 variables, and if they’re all empty, then show the default 3 recent articles from the section. I just can’t get my head around how. Any ideas?


The AI does not hate you, nor does it love you, but you are made out of atoms which it can use for something else.

Offline

#23 2011-07-08 12:45:36

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,476
Website GitHub

Re: smd_multi_choice: select... case conditional-based output

frickinmuck

I’d be tempted to cheat like this:

<txp:variable name="has_var" value="0" />
<txp:smd_switch item="?coverageclient">
   <txp:smd_case type="empty">
      <txp:article_custom section="coverage" form="article_listing" limit="3" />
      <txp:variable name="has_var" value="1" />
   </txp:smd_case>
</txp:smd_switch>

<txp:if_variable name="has_var" value="0">
   <txp:smd_switch item="?coveragemediaoutlet">
      <txp:smd_case type="empty">
         <txp:article_custom section="coverage" form="article_listing" limit="3" />
         <txp:variable name="has_var" value="1" />
      </txp:smd_case>
   </txp:smd_switch>
</txp:if_variable>

<txp:if_variable name="has_var" value="0">
   <txp:smd_switch item="?coveragetype">
      <txp:smd_case type="empty">
         <txp:article_custom section="coverage" form="article_listing" limit="3" />
         <txp:variable name="has_var" value="1" />
      </txp:smd_case>
   </txp:smd_switch>
</txp:if_variable>

...

That way you only start a new switch if the one before it output nothing. And it saves having to use some hideously complex nesting.

Last edited by Bloke (2011-07-08 12:46:13)


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

Offline

#24 2011-07-08 14:26:48

frickinmuck
Member
Registered: 2008-05-01
Posts: 118

Re: smd_multi_choice: select... case conditional-based output

The only problem with that is, at least 3 of the variables will always be empty, therefore content will always display. The default recent articles list should only ever appear if all 4 of the variables are empty. It’s too bad there isn’t a way to check several variables at once, as in:

<txp:if_variable name="coveragemediaoutlet,coveragetype,coveragecause,coverageclient" value="0">
         <txp:article_custom section="coverage" form="article_listing" limit="3" />   
     </txp:if_variable>

or even

<txp:if_all_variables value="0">
         <txp:article_custom section="coverage" form="article_listing" limit="3" />   
     </txp:if_all_variables>

LOL

Last edited by frickinmuck (2011-07-08 14:30:12)


The AI does not hate you, nor does it love you, but you are made out of atoms which it can use for something else.

Offline

#25 2011-07-08 14:40:36

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,476
Website GitHub

Re: smd_multi_choice: select... case conditional-based output

frickinmuck wrote:

The default recent articles list should only ever appear if all 4 of the variables are empty. It’s too bad there isn’t a way to check several variables at once

smd_if can :-)

Did you say your variables came from the result of a user-submitted url variable? If so:

<txp:smd_if field="urlvar:coveragemediaoutlet,urlvar:coveragetype,urlvar:coveragecause,urlvar:coverageclient" operator="isempty">
   ... 
</txp:smd_if>

If you need to test txp:variables use txpvar instead of urlvar. You may also be better off with operator="undefined" if testing URL vars directly.

I don’t know if this is too little too late (or completely irrelevant) but can people filter by more than one of the variables at once? If not, why don’t you combine them into a single variable, then you can use multiple smd_case statements and fallthru=“1” to test them all in sequence.


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

Offline

#26 2011-07-08 18:59:03

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

Re: smd_multi_choice: select... case conditional-based output

frickinmuck wrote:

It’s too bad there isn’t a way to check several variables at once

I think it’s time to draw a new specialist plugin beneath the street lightings: cbe_if_variable.


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

Offline

#27 2011-07-09 01:38:18

frickinmuck
Member
Registered: 2008-05-01
Posts: 118

Re: smd_multi_choice: select... case conditional-based output

Of COURSE you have a solution up your sleeve, Stef! You just seem to be The Man! Thanks – it worked perfectly:

<txp:smd_if field="txpvar:coveragemediaoutlet,txpvar:coveragetype,txpvar:coveragecause,txpvar:coverageclient" operator="isempty">
<txp:article_custom section="coverage" form="article_listing" limit="3" />
</txp:smd_if>

No, there will only ever be one filter in use at a time. Coverage Type OR Coverage Client, etc. etc. But since this is working as desired now, I’m really happy with it as-is. Unless there’s some benefit to simplifying it besides the abstract value of “doing it correctly”. :^)


The AI does not hate you, nor does it love you, but you are made out of atoms which it can use for something else.

Offline

#28 2012-07-25 22:44:18

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: smd_multi_choice: select... case conditional-based output

Feature request: a trim attribute for txp:smd_switch could come in handy when trying to exactly match (eq) strings that may come with leading/trailing spaces.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB