Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#193 2008-11-21 09:51:52

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

Re: smd_if: Generic multiple if condition tests

nabrown78 wrote:

Wow. How the hell did you figure that out?

Hehe, equal measures of luck and being a geek I guess. If someone gives me a tool, I find a way to (ab)use it ;-)

I just got wise to txp:variable… Are there any docs anywhere about how this guy works?

You are correct, it’s a 4.0.7 feature. That code I posted would be nigh on impossible without the new parser (or dropping into PHP). As for docs, the forum thread you found and also one on empty vars have good tips in them. In addition, there’s the TXPQ interview with the tags that I wrote. It contains a few techie gremlins (kindly pointed out in the comments below the article) but gives some more grounding in how to use the tags.

Can you set it in the admin interface as well? Or only in page templates, forms, etc?

Well, it’s a public tag so Forms and Pages are where it normally resides. If you want something more permanent, check out the not-quite-as-full-featured-as-I-hoped-but-still-useful smd_vars. There are a few mini bugs still to fix (must get round to it one day) and it needs expanding in scope a little, but it’s stable enough to play with.

I guess I should look at the smd_query you recommend too!

Recommend is perhaps over-selling it. Last resort is probably better ;-) But by all means take a look if you’re an SQL afficionado. Thinking about it again I’m not sure it’ll help because it (currently) cannot take stuff from the URL line so couldn’t grab the ?q= thing that the person searched for. Future versions may offer this ability and then it’d be a cinch to use.


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

#194 2008-11-22 13:16:35

roelof
Member
Registered: 2005-03-27
Posts: 647

Re: smd_if: Generic multiple if condition tests

hai Bloke,

I need your help one more time :

I have made this :

<txp:smd_if field="urlvar:month" operator="eq" value="2005-04">
       <txp:smd_if field="urlvar:pg" operator="ge" value="2">
              <txp:article limit="2" form="dagboek" sort="Posted asc" />
              <txp:ob1_pagination outputlastfirst ="0" outputnextprevious="0" />
          <txp:else />
            <txp:article limit="1" form="dagboek" sort="Posted asc" />
            <txp:ob1_pagination outputlastfirst ="0" outputnextprevious="0" maximumtabs="4"/>
        </txp:smd_if>
<txp:else />   <-----------------------------------------------------------------------------------------------------
       <txp:smd_if field="urlvar:month" operator="eq" value="2005-05">
         <txp:article limit="2" form="dagboek" sort="Posted asc" />
              <txp:ob1_pagination outputlastfirst ="0" outputnextprevious="0" />
         </txp:smd_if>
         <txp:else />
     <txp:smd_if field="urlvar:month" operator="defined"> <-----------------------------------------------------
              <txp:article limit="5" form="dagboek" sort="Posted asc" />
    </txp:smd_if>
</txp:smd_if>

But txp keeps complening about the else between the <———
So you see why ?

Roelof

Offline

#195 2008-11-22 13:59:24

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

Re: smd_if: Generic multiple if condition tests

You have a random, extra <txp:else /> on line 14 of the code you posted above. Remove that and it’ll stop complaining.


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

#196 2008-11-22 14:36:17

roelof
Member
Registered: 2005-03-27
Posts: 647

Re: smd_if: Generic multiple if condition tests

No, according to what i thought that’s not a random else.

First i look if month = 2004-05 if it’s not then i look if month=2005-05 and if it’s also not then if month is declared.
It’s another month.
So in my opion i need this to look if month is not 2005-05 and also not 2005-05.

If i delete this else txp can’t make a difference between 2005-02 and all the months i didn’t declare because they fitt on one page.

Here’s a tag trace to illustrate this :

<txp:smd_if field="urlvar:month" operator="eq" value="2005-05">
				[<txp:smd_if field="urlvar:month" operator="eq" value="2005-05">: true]
				<txp:article limit="2" form="dagboek" sort="Posted asc" />
					[SQL (0,000226020812988): select name from txp_section where on_frontpage != '1']
					[SQL (0,000527143478394): select count(*) from textpattern where 1=1 and Status = 4 and Posted <= now() and (now() <= Expires or Expires = 0) and (Category1 IN ('zwangerschap') or Category2 IN ('zwangerschap')) and Posted like '2005-05%' and Section != 'about' and Section != 'contact' and Section != 'gallery' and Section != 'voorpagina']
					[SQL (0,00105500221252): select *, unix_timestamp(Posted) as uPosted, unix_timestamp(Expires) as uExpires, unix_timestamp(LastMod) as uLastMod from textpattern where 1=1 and Status = 4 and Posted <= now() and (now() <= Expires or Expires = 0) and (Category1 IN ('zwangerschap') or Category2 IN ('zwangerschap')) and Posted like '2005-05%' and Section != 'about' and Section != 'contact' and Section != 'gallery' and Section != 'voorpagina' order by Posted asc limit 0, 2]
					[artikel 15]
					[SQL (0,000202894210815): select Form from txp_form where name='dagboek']
					[Module: dagboek]
					<txp:posted format="%B" />
					<txp:posted format= "%d" />
					<txp:title />
					<txp:body />
					[artikel 16]
					[Module: dagboek]
					<txp:posted format="%B" />
					<txp:posted format= "%d" />
					<txp:title />
					<txp:body />
				<txp:ob1_pagination outputlastfirst ="0" outputnextprevious="0" />
			</txp:smd_if>
			<txp:article limit="5" form="dagboek" sort="Posted asc" />

Roelof

Last edited by roelof (2008-11-22 14:54:51)

Offline

#197 2008-11-22 14:46:57

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

Re: smd_if: Generic multiple if condition tests

roelof wrote:

No, according to what i thought that’s not a random else.

In which case, the <txp:smd_if /> on line 13 is in the wrong place. You can’t have a <txp:else /> outside a corresponding conditional; it must be between a pair, so check through your code and make sure that is the case.

If I write it out like this and match up the pairs you’ll see what I mean:

<txp:smd_if field="urlvar:month" operator="eq" value="2005-05">   <------------ IF #1
   <txp:article limit="2" form="dagboek" sort="Posted asc" />
   <txp:ob1_pagination outputlastfirst ="0" outputnextprevious="0" />
</txp:smd_if>   <------------ END IF #1

         <txp:else />   <------------ THIS ELSE IS NOT BETWEEN AN OPENING/CLOSING smd_if PAIR

<txp:smd_if field="urlvar:month" operator="defined">   <------------ IF #2
   <txp:article limit="5" form="dagboek" sort="Posted asc" />
</txp:smd_if>   <------------ END IF #2

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

#198 2008-11-22 16:40:45

roelof
Member
Registered: 2005-03-27
Posts: 647

Re: smd_if: Generic multiple if condition tests

Hai bloke,

Im now totally confused.
What i want is a script what does this.

If month=2004-04 then if page is greater or egual as 2 then 2 articles must shown.
If month=2004 -04 and the page is not 2 or greater then 1 articles.
If month=2005-05 then 2 articles must be shown.
if another month then all the articles of that month must be shown.
If no month then it must be the frontpage then the article of the section frontpage must be schown.

Can you help me making such a script.

Roelof

Offline

#199 2008-11-22 19:32:40

roelof
Member
Registered: 2005-03-27
Posts: 647

Re: smd_if: Generic multiple if condition tests

hello Bloke,

I made a picture how i think the smd-if must look like.
Is this a correct decision tree or have i made a error.

The picture can be found at : http://plaatje.tamarawobben.nl/decision.jpg

Regards,

Roelof

Offline

#200 2008-11-23 07:54:27

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

Re: smd_if: Generic multiple if condition tests

roelof wrote:

Is this a correct decision tree or have i made a error.

Well you haven’t specified what should happen if the section is NOT articles but if we ignore that…

<txp:if_section name=", default">
  // frontpage article
<txp:else />
   <txp:if_section name="articles">
      <txp:smd_if field="urlvar:month" operator="eq" value="2005-04">
         <txp:smd_if field="urlvar:pg" operator="ge" value="2">
            // Show one article
         <txp:else />
            // Show 2 articles
         </txp:smd_if>
      <txp:else />
         <txp:smd_if field="urlvar:month" operator="eq" value="2005-05">
            // Show 2 articles
         <txp:else  />
            // Show all articles
         </txp:smd_if>
      </txp:smd_if>
   <txp:else />   
      // You don't say what should happen here in your drawing
   </txp:if_section>
</txp:if_section>

Don’t just copy it and use it blind (it’s untested for starters) but try and understand the logic behind it. Trace it through and work out what’s going on at each line compared to your drawing. Pay particular attention to how everything is indented so that all the corresponding tags and <txp:else /> statements line up. If you lay your code out like this, it makes it easier to spot any out-of-place tags or dangling else tags (it is difficult to see this in forms and pages because the text wraps in the textarea box, but copying your template into a text editor, editing it there and then pasting the final thing back into TXP helps me in these situations).

BTW, all this seems very specific to a particular date and section. It’s not very extensible and I’m sure your logic could be optimized somewhere…


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

#201 2008-12-01 20:06:43

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

Moved post to another thread :) (Block – you can move your own here too)

Some features:
1. Check images width \ height ?
2. Check for file’s extension. I think this should work as modifier, like this: <txp:smd_if field=“txpvar:file_name” operator=“eq:EXT” value=“doc”></txp:smd_if>

P.S. Sorry for messing :)


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

#202 2008-12-01 20:16:19

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

Re: smd_if: Generic multiple if condition tests

the_ghost wrote:

Check images width \ height ?

I don’t think this will be possible until some form of image context is included in TXP. For example, how would you know which image to test? There is (currently) no such concept as “current image” (unlike current article, current file, current link, which all exist right now).

However, if you try out the image patch you will get this functionality and can actually use smd_if right now by, say, assigning an image’s width or height to a txp:variable and testing if it is gt, lt or eq a value. Or using tag-in-tag to put the output of <txp:image_size /> directly into the field attribute.

Check for file’s extension

Not necessary because you can use operator="ends" value="doc".

Last edited by Bloke (2008-12-01 20:17:27)


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

#203 2008-12-03 00:03:56

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

Re: smd_if: Generic multiple if condition tests

A couple of new features in this upgrade.

v0.75 | compressed adds:

  • Operator divisible (thanks gomedia)
  • Ability to short circuit the field so you only need to specify it once if you are comparing many things to a single field (thanks redbot)

Example 6 in the help demonstrates the latter, and here’s a quick example of how you might use the former:

<txp:smd_gallery category="pics">
   <txp:smd_if field="{counter}" operator="divisible" value="2">
      <txp:image id="{id}" class="even" />
   <txp:else />
      <txp:image id="{id}" class="odd" />
   </txp:smd_if>
</txp:smd_gallery>

Stripe-tastic :-)


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

#204 2008-12-03 10:53:07

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: smd_if: Generic multiple if condition tests

Thanks ;)

Offline

Board footer

Powered by FluxBB