Textpattern CMS support forum

You are not logged in. Register | Login | Help

#31 2008-01-13 18:18:38

the_ghost
Plugin Author
From: Minsk, The Republic of Belarus
Registered: 2007-07-26
Posts: 907
Website

Re: smd_if: Generic multiple if condition tests

Yes. It works now :)

I think you should make operator="contains" default for checking parent category.

And you know, I use mostly logic="or", so I prefer it to be default value. But i think i can modify plugin for this. I mentioned this because maybe most users like “or” action?


Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?

Offline

#32 2008-01-13 18:37:53

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,621
Website

Re: smd_if: Generic multiple if condition tests

the_ghost wrote:

Yes. It works now :)

Excellent!

I think you should make operator="contains" default for checking parent category.

Good idea. I’ll look into doing that.

maybe most users like “or” action?

Personally I use AND more than OR with this plugin but if any other people agree with the_ghost that OR makes a more sensible default I can certainly change it. Anyone?


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

#33 2008-01-13 18:54:53

the_ghost
Plugin Author
From: Minsk, The Republic of Belarus
Registered: 2007-07-26
Posts: 907
Website

Re: smd_if: Generic multiple if condition tests

P.S. I think it would be pleasant to perform all manipulations after lowercasing checking values.


Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?

Offline

#34 2008-01-14 11:03:58

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,621
Website

Re: smd_if: Generic multiple if condition tests

Version 0.5 is out (compressed) that takes into account most of the features requested by the_ghost:

  • Default operator for parent is now contains (it is eq for everything else)
  • New attribute: case_sensitive. 0=ignore case; 1=case-sensitive. NOTE that all comparisons now ignore case by default
  • New attribute: param_delim. Allows you to override the comma when specifying lists (useful if you have a comma in the name of one of your custom fields or you just plain hate commas!)
  • New attribute: mod_delim. Allows you to override the colon when specifying field and parameter modifiers (like urlvar:varname or gt:NUM)
  • Improved the help to (hopefully) make the ‘parent’ explanation clearer; fixed some late-night typos

Many thanks to the_ghost for highlighting these modifications. I have NOT yet changed the default logic to OR. I will wait for a consensus on that before I forge ahead and do it, but in the meantime if it really bothers anyone to specify logic="or" in every smd_if call, modify line 8 of the plugin and change the ‘and’ to ‘or’.

EDIT: arghhhh! defined and undefined are brain damaged in this version and give an error. I’ve found an easy fix but I’ve decided I want to make the distinction between defined/isused and undefined/isempty a little wider so they become more useful. I’m hacking the code to do that before releasing the fix so for now please avoid defined and undefined as operators. Apologies.

Last edited by Bloke (2008-01-15 00:34:41)


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

#35 2008-01-15 00:38:10

the_ghost
Plugin Author
From: Minsk, The Republic of Belarus
Registered: 2007-07-26
Posts: 907
Website

Re: smd_if: Generic multiple if condition tests

If i understand correctly operator="or" or operator="and" is used for all checking statements? I mean:

<txp:smd_if field="parent,section,article_image" value="metro,about,2">
  something
</txp:smd_if>

This will output something only if parent contains metro AND section equal to about AND article_image equal to 2. This is nice, but what if we have to make coditional checking like this – I’ll write it in usual programming style, I think you will understand ;) :

if ( (parent=='metro' AND $section=='about) OR article_image==2) {
    echo "something";
}

The only problem – now i cannot imaging where this construction can be used… But if you like this idea we should think out how to group statements. It can be something like this:

<txp:smd_if field="parent,section,article_image" value="metro,about,2" and_group="1,2" or_group="3">
  something
</txp:smd_if>

This can help to make not very deep conditions (only 1 level as i see – like example’s deepness)

P.S. I thought over this method and it seems to be buggy :) I need some sleep to refill my imagination\inspiration tank.


Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?

Offline

#36 2008-01-15 01:08:49

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,621
Website

Re: smd_if: Generic multiple if condition tests

the_ghost wrote:

If i understand correctly operator="or" or operator="and" is used for all checking statements?

Yes, absolutely correct. To be honest I did consider being able to split the logic as you suggested because I knew someone would ask for it one day :-p

But when I originally did it I took the path of least resistance and just made it easy on myself. Now, your solution might work — I have not thought it through yet — but I wonder if just allowing the logic field to take a list of ANDs and ORs (with parentheses) might work? Of course, the complexity of the plugin goes up about 10 times because I then have to parse brackets and I am, in effect, partway towards writing a Reverse Polish calculator!

So something like :

<txp:smd_if field="parent, section, article_image" value="metro, about, 2" logic="(and), or">
  // something
</txp:smd_if>

What do you think??? You will always have one logic operator less than the number of comparisons.

The logic checking part of the plugin is currently very simple, done right at the very end. If I did it this new way I would have to parse the brackets, make up the replacement expressions as normal, plug them in the right places in the “logic” then probably execute the whole lot as one giant eval() statement. I may still be able to build it up in chunks (which I would prefer — easier on my brain and less likely to cause syntax errors from bogus options) but it’ll require some serious thought.

Of course, for backwards-compatibility if you specify a single “and” (or “or”) it will have to “expand” it and apply that to all the expressions; that bit’s easy.

I will have a think about it and see if it can be done. When you are feeling refreshed, if you come up with any cool way of doing it, please let me know…

Thanks!

Last edited by Bloke (2008-01-15 01:10:41)


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

#37 2008-01-15 02:07:54

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,621
Website

Re: smd_if: Generic multiple if condition tests

Bug fix alert: v0.51 (compressed) addresses the syntax error when using defined/undefined with case_sensitive=“0” and also makes more distinction between defined/undefined and isused/isempty. See Example 5 in the help for more.


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

#38 2008-01-15 19:05:37

draganbabic
Member
From: Novi Sad, Serbia
Registered: 2006-09-27
Posts: 115
Website

Re: smd_if: Generic multiple if condition tests

I have a feeling this plugin could solve the problem that is making me rip out my non-existent hair. Here goes…

I am having a client insert an URL of a resource inside four custom field (depending on how much he needs, it can be one, it can be all four) that is either an image – or a flash object (.swf). He is inserting ONLY the URL, not html (because he doesn’t like HTML). Somehow I need to know what the file is, so I can output the appropriate code – img src… or object…

Now, the first thing I am doing is that I’m surrounding the divs that hold the file with if_custom_field (so TXP doesn’t output them if nothing is entered), but what I can’t do is know which file is it so I can feed that URL (the value of the custom field) into appropriate HTML. Here’s the code:

          <txp:if_custom_field name="projecten_item_3"><div class="media invisible">
            <img src="<txp:custom_field name="projecten_item_3" />" alt="" />
          </div></txp:if_custom_field>

See how it’s locked with an img? Basically now it’s impossible to insert the flash movie into the page because the URL will be fed in to the img element. For that purpose – I have another custom field named file_type, and I have tried something like this:

         <txp:if_custom_field name="projecten_item_3"><div class="media invisible">
            <txp:if_custom_field name="file_type" val="swf">...flash stuff here
            <txp:else />
            <img src="<txp:custom_field name="projecten_item_3" />" alt="" />
            </txp:if_custom_field>
          </div></txp:if_custom_field>

But I can’t nest if_custom_field tags. Is there maybe a way this plugin can check for values of both these custom fields and output appropriate code?

Offline

#39 2008-01-15 20:41:03

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,621
Website

Re: smd_if: Generic multiple if condition tests

draganbabic wrote:

I am having a client insert an URL of a resource inside four custom field… that is either an image – or a flash object (.swf).

Looks like you might be in luck if I’ve understood this correctly. Since swf objects tend to have the least variation in file extension (and assuming the client sticks to a decent naming convention), the easiest thing is to just test the URL directly:

<txp:smd_if "projecten_item_3" operator="contains" value=".swf">
  // Output swf container
<txp:else />
  // Output img container
</txp:smd_if>

And repeat that block for each of your custom fields. Of course, if you wanted to use you “type” custom field you could perhaps do:

<txp:smd_if "file_type, projecten_item_3" operator="eq, isused" value="swf">
  // Output swf container
<txp:else />
  // Output img container
</txp:smd_if>

Depending on whether the client can mix and match img and swf types in a single article depends on whether the file_type custom field is of any use or not!

Does that help?


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

#40 2008-01-16 09:43:31

draganbabic
Member
From: Novi Sad, Serbia
Registered: 2006-09-27
Posts: 115
Website

Re: smd_if: Generic multiple if condition tests

Thank you very much for the response Bloke, but the first code (which seems much simpler to use for both me and the client) always seems to return positive for operator=“contains” value=”.swf” – so it always outputs the swf container. Here’s the code:

         <txp:if_custom_field name="projecten_item_1"><div class="media">
<txp:smd_if "projecten_item_1" operator="contains" value=".swf">
            <object type="application/x-shockwave-flash"
data="<txp:custom_field name="projecten_item_1" />" 
width="650" height="180">
              <param name="movie" 
value="<txp:custom_field name="projecten_item_1" />" />
            </object>
<txp:else />
            <img src="<txp:custom_field name="projecten_item_1" />" alt="" />
          </txp:smd_if></div></txp:if_custom_field>
          <txp:if_custom_field name="projecten_item_2"><div class="media invisible">
<txp:smd_if "projecten_item_2" operator="contains" value=".swf">
            <object type="application/x-shockwave-flash"
data="<txp:custom_field name="projecten_item_2" />" 
width="650" height="180">
              <param name="movie" 
value="<txp:custom_field name="projecten_item_2" />" />
            </object>
<txp:else />
            <img src="<txp:custom_field name="projecten_item_2" />" alt="" />
          </txp:smd_if></div></txp:if_custom_field>
<!-- I have two more of these since I have four projecten_item_x custom fields --> 

Note that it doesn’t work when I remove the if_custom_field conditionals that are wrapping the smd_if as well. Any thoughts?

EDIT:

Value for projecten_item_1 was http://mysite.com/dir/images/SKP0701_1.jpg
Value for projecten_item_2 was http://mysite.com/dir/files/SKP07.swf

Last edited by draganbabic (2008-01-16 09:44:54)

Offline

Board footer

Powered by FluxBB