Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#301 2010-03-03 12:18:30
Re: smd_if: Generic multiple if condition tests
I’m using smd_if within smd_each to customise the separator much like the example that finds the last item except that I want the last separator which equates to the penultimate item. I tried comparing with {smd_var_total}-1
but no dice:
<txp:smd_if field="{smd_var_counter}" operator="eq" value="{smd_var_total}-1">This is the last item</txp:smd_if>
How would I get the penultimate item? The desired outcome is “this, that, the other and one extra”, e.g. a comma as normal separator but an “and” for the last separator. Or is there a better way of achieving this with smd_each?
TXP Builders – finely-crafted code, design and txp
Offline
#302 2010-03-03 13:55:25
Re: smd_if: Generic multiple if condition tests
Using value='<txp:php>echo {smd_var_total}-1;</txp:php>'
does it :-)
Summary: to get “link-a, link-b, link-c and link-d” out of a comma-separated list of article-ids in my-custom-field e.g. “12,22,45,32” use:
<txp:smd_each include="my-custom-field" subset="2">
<txp:permlink id="{smd_var_value}" />
<txp:smd_if field="{smd_var_counter}" operator="not,not" value='{smd_var_total},<txp:php>echo {smd_var_total}-1;</txp:php>'>, </txp:smd_if>
<txp:smd_if field="{smd_var_counter}" operator="eq" value='<txp:php>echo {smd_var_total}-1;</txp:php>'> and </txp:smd_if>
</txp:smd_each>
(line wrapping is for better readability but causes extra spaces before the commas. Run the tags together without line breaks to obtain clean output).
I guess adi_calc and setting a variable would work here too. But perhaps there’s a better way altogether?
Last edited by jakob (2010-03-03 14:11:32)
TXP Builders – finely-crafted code, design and txp
Offline
#303 2010-03-03 23:50:37
Re: smd_if: Generic multiple if condition tests
speeke wrote:
Am I using the plugin in the best possible configuration here?
That’s a pretty good approach, yeah. It’ll work for valid input data at least. You might be able to short circuit the logic if you assume the data is invalid and only let stuff through if it matches. I think that might save a test (though I could be wrong: not tried it).
If you want to stick with this kind of thing I would suggest using between
or range
instead of eq
because if they use 42
as the day then it will let it through the 1st test — don’t use the fact that the dropdown in your form only goes up to 31 as a reason not to be more robust here; if some tyke can alter the URL line, they will!
However, in this case you might actually be better off not using smd_if at all (gasp!). How about letting PHP do the work? You can either use that function directly in the ol’ <txp:php>
tags, or (perhaps better because you may be able to use <txp:else />
) use adi_gps to grab the URL values and throw them at rah_function to do the deed :-)
Last edited by Bloke (2010-03-03 23:52:02)
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
#304 2010-03-04 01:16:51
Re: smd_if: Generic multiple if condition tests
jakob wrote:
perhaps there’s a better way altogether?
That’s pretty succinct already. I don’t really have a better way, but an alternative that only uses one smd_if. You can decide if it’s better / more efficient than two:
<txp:variable name="dlm" value="," />
<txp:smd_each include="my_custom_field" subset="2">
<txp:permlink id="{smd_var_value}" />
<txp:smd_if field="{smd_var_counter}" operator="eq" value='<txp:php>echo {smd_var_total}-1;</txp:php>'> and <txp:variable name="dlm" value="" /><txp:else /><txp:variable name="dlm" /></txp:smd_if>
</txp:smd_each>
When you reach the ‘and’ case, it merely silences the variable so the next iteration prints nothing. *shrug* swings and roundabouts I guess.
EDIT: nuts, my version borks if you only have one entry in your CF. Maybe you do need the 2nd test after all…
Last edited by Bloke (2010-03-04 01:19:43)
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
#305 2010-03-06 01:17:12
Re: smd_if: Generic multiple if condition tests
Is it possible to use txp:if_article_section via this plugin? And/or can anyone suggest a way to use it to just create a list of sections?
Offline
#306 2010-03-06 02:25:27
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,311
Re: smd_if: Generic multiple if condition tests
Probably best done with smd_query:
<txp:smd_query query="SELECT name FROM txp_section WHERE name REGEXP '^issue-'" break=", ">
<txp:section />
</txp:smd_query>
(Problem)
Last edited by uli (2010-03-06 02:27:20)
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
#307 2010-03-06 05:17:09
Re: smd_if: Generic multiple if condition tests
Thanks, Uli. I ended up following your advice, as follows:
<txp:smd_query query="SELECT name FROM txp_section WHERE name REGEXP '^issue-'" break=",">
{name}
</txp:smd_query>
After a lot of failure getting that to parse directly as a txp:variable value, I just made it a form and am passing it around that way.
(I’m also posting this in other spot I asked about this larger problem).
Offline
#308 2010-04-13 20:46:14
- lazlo
- Member
- Registered: 2004-02-24
- Posts: 110
Re: smd_if: Generic multiple if condition tests
Help please.
I have an “Events” section (smd_calendar) that has an custom field call ‘Event_Authors” which is a comma delimited list {Author One, Author Two, Author Three}.
I also have an Author Bio page where Title = Author Name eg: /authors/author-one
What I need is when I go to any author page I want to loop through the articles in the event section and then list the events where Author Bio Page “title” is contained in “Event_Authors”.
I had been using the following code to match a fields with single entries in other instances for looping.
<txp:article_custom section=‘Authors’ form=‘author_bio_excerpt’ Author_1=’<txp:custom_field name=“Author_2” />’ limit=‘1’ />
but now since I have to check delimited field for matches I don’t now what I should be using smd_if or smd_each or both.
So can some one match this plain english statement to code?
In Author section, If individual article loop a custom article call which grabs all “Event” articles that match “Events_Authors” = Title of the current page.
or
/authors/george-bowering page returns all section “Event” articles that have George Bowering listed in the “Event_Authors” field.
I hope this makes sense.
Thanks
Les
Offline
#309 2010-04-13 21:54:02
Re: smd_if: Generic multiple if condition tests
lazlo
Tricky one this. It’s compounded by the fact that the /author/author+name
style of URL doesn’t use the author’s login name but their real name and (I think) the only native tag that can understand it is txp:article. So the first problem is getting hold of the author name in some form we can use. A few options spring to mind in terms of plugins. I think jmd_author can get at the required info but I don’t know if it can read the URL author. smd_bio might be able to get it too (I can’t remember!) but it’s complete overkill unless you happen to have it installed already.
Once you have that value stashed somewhere (in a txp:variable perhaps?) you then need to do the 2nd phase which is comparing it to your custom field. smd_each is probably over the top for this application. If you want to simply iterate over the values I’d opt for the less expensive rah_repeat. But, since we’re talking about an author name and its unlikely to be duplicated ín your site, how about cheating?
<txp:if_author>
<txp:if_individual_article>
<txp:article_custom section="events">
<txp:smd_if field="event_authors" operator="contains" value="?your_author_name_variable">
// output some article info here
</txp:smd_if>
</txp:article_custom>
</txp:if_individual_article>
</txp:if_author>
I think that should do the trick.
One further random thought about getting the author from the URL: smd_if can read svrvars so you can perhaps use svrvar:REQUEST_URI directly instead of stashing it in a variable first. It may be possible to lop everything off except the stuff after the last slash (perhaps rah_function to the rescue using PHPs basename
can do it?)
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
#310 2010-04-17 20:28:41
- sereal
- Member
- From: Indonesia
- Registered: 2010-02-18
- Posts: 55
Re: smd_if: Generic multiple if condition tests
hi Stef
I try to modify this with no luck
<txp:if_individual_article>
<txp:article_custom limit="1">
<txp:smd_if field="smallORbig" operator="contains" value="big">
<txp:article class="sec1" form="article_full_big" limit="2" />
<txp:else />
<txp:article class="sec1" form="article_full" limit="2" />
</txp:smd_if>
</txp:article_custom>
<txp:else />
<txp:article_custom limit="1">
<txp:smd_if field="smallORbig" operator="contains" value="big">
<txp:article class="sec1" form="article_short_big" limit="2" />
<txp:else />
<txp:article class="sec1" form="article_short" limit="2" />
</txp:smd_if>
</txp:article_custom>
</txp:if_individual_article>
there are 2 different article forms to use but those line of tags only pick the big one and ignoring the small
because some articles I have has custom_field valued small while other is big
do you have any idea Sir ?
thank you
Last edited by sereal (2010-04-17 20:29:21)
$(mydocument).notyetready(function() {});
dowebsitesneedtolookexactlythesameineverybrowser ?
Offline
#311 2010-04-18 05:44:32
Re: smd_if: Generic multiple if condition tests
Hi sereal.
Calling a txp:article
inside a txp:article_custom
is technically wrong.
You could try something like this:
<txp:if_individual_article>
<!--
As you are already at the individual-article context,
the value of any article field, like the custom field 'smallORbig',
is already available for being used, or tested, like you're doing
with smd_if. Also, class attribute isn't needed, as you aren't using
the wraptag attribute (class attribute value applies to wraptag).
-->
<txp:smd_if field="smallORbig" operator="contains" value="big">
<txp:article form="article_full_big" />
<txp:else />
<txp:article form="article_full" />
</txp:smd_if>
<txp:else />
<!--
But here, you are at an article-list context, so the value for any
article field will be available inside each iteration, but not outside
it. So the following is *wrong*.
-->
<txp:hide>
<!-- wrong, so we txp:hide it -->
<txp:smd_if field="smallORbig" operator="contains" value="big">
<txp:article form="article_short_big" limit="2" />
<txp:else />
<txp:article form="article_short" limit="2" />
</txp:smd_if>
</txp:hide>
<!--
Instead, it would have been more correct to do it this way:
-->
<txp:article class="sec1" limit="2">
<txp:smd_if field="smallORbig" operator="contains" value="big">
... some html and txp tags here, like txp:title, txp:body, etc...
<txp:else />
... some html and txp tags here, like txp:title, txp:body, etc...
</txp:smd_if>
</txp:article>
</txp:if_individual_article>
A better, shorter way to have the same functionality could have been this:
<txp:if_individual_article>
<!--
Here, you avoid using an smd_if to test 'smallORbig' custom_field value
(btw, I would call this cf just 'size').
Of course, you should have to (re)name your forms to 'article_full_big'
and 'article_full_small'.
Also, I've set a default value for the custom_field, in case it has
been left empty. So, for any article you forgot (or decided) to leave
custom_field value empty, that article will be rendered using 'article_full_small'
form.
-->
<txp:article form='article_full_<txp:custom_field name="smallORbig" default="small" />' />
<txp:else />
<!--
OK, now you are at article-list context. Let's do a similar trick.
txp:article will fetch the list of articles, and on each iteration
each article will be rendered using a particular form, according
to the custom_field value. Of course, you will need to rename
form 'article_short' to 'article_short_small'.
-->
<txp:article limit="2">
<txp:output_form form='article_short_<txp:custom_field name="smallORbig" default="small" />' />
</txp:article>
</txp:if_individual_article>
Shorter and easier, right? And, in this version, no need for the great, invaluable smd_if swiss knife.
Note that when you use txp:tags inside other txp:tags (as attribute values), you will need to use single quote (‘) instead of double quote.
Offline
#312 2010-04-18 10:49:56
- sereal
- Member
- From: Indonesia
- Registered: 2010-02-18
- Posts: 55
Re: smd_if: Generic multiple if condition tests
Hi maniqui
it’s been a week trying to solve this :(
you’re right I think I have to be more carefull on renaming my custom forms
your 1st example is not what I am trying to achieve because the first calls
<txp:if_individual_article>
<!--
As you are already at the individual-article context,
the value of any article field, like the custom field 'smallORbig',
is already available for being used, or tested, like you're doing
with smd_if. Also, class attribute isn't needed, as you aren't using
the wraptag attribute (class attribute value applies to wraptag).
-->
<txp:smd_if field="smallORbig" operator="contains" value="big">
<txp:article form="article_full_big" />
<txp:else />
<txp:article form="article_full" />
</txp:smd_if>
<txp:else />
this renders a blank page on my test :(
but your 2nd example really works !!!!!!! exactly what I need
thank you thank you
$(mydocument).notyetready(function() {});
dowebsitesneedtolookexactlythesameineverybrowser ?
Offline