Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#16 2009-03-18 23:20:11
- jeremywood
- Member
- Registered: 2007-12-12
- Posts: 26
Re: smd_each: iterate over stuff
I’ve run into another hiccup in using smd_each.
Setup:
Case 1: value = “1”
Case 2: value = “1, 2, 3, 4”
<txp:smd_each type=“txpvar” include=“related” subset=“1” >
- {var_value}
</txp:smd_each>
Here’s the problem: What if I don’t know if I’m going to have 1 item, or a whole series of items in the source field? Which subset setting should I use?
Well, it doesn’t seem like any of the choices makes sense.
Not setting a subset works with one item, and not at all with more than one.
Case 1:
- 1
Case 2:
- 1, 2, 3, 4
If I use subset=“1” then having 1 item works, but I get a garbage result at the beginning of the output of a list.
Case 1:
- 1
Case 2:
- 1,2,3,4
- 1
- 2
- 3
- 4
If I use subset=“2” then having 1 item makes no output, but the list looks good.
Case 1:
Case 2:
- 1
- 2
- 3
- 4
I’m not sure what the use of having subset=“1” work the way it does is. Is there something I’m missing?
Offline
Re: smd_each: iterate over stuff
jeremywood wrote:
What if I don’t know if I’m going to have 1 item, or a whole series of items in the source field?
That’s a very good question, and one I didn’t think about when I wrote the plugin. As you found out, the subset values can give various degrees of output; none of which fit in this case. It looks like subset="1" is the closest, but…
I get a garbage result at the beginning of the output of a list.
That ‘garbage’ is actually the entire contents of the given field. I can’t for the life of me think why I left it in there, but there was definitely a reason because I do it on purpose. Clearly there should be a toggle switch to turn this ‘feature’ off, which I’ll build into the next release.
In the meantime, skip down to line 243 of the plugin where it says:
if ($sub == 1) {
and change it to:
if ($sub == 1 && 0) {
That’ll get rid of that extra combined list.
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
#18 2009-03-19 21:00:57
- jeremywood
- Member
- Registered: 2007-12-12
- Posts: 26
Re: smd_each: iterate over stuff
Bloke wrote:
That ‘garbage’ is actually the entire contents of the given field. I can’t for the life of me think why I left it in there, but there was definitely a reason because I do it on purpose. Clearly there should be a toggle switch to turn this ‘feature’ off, which I’ll build into the next release.
Sorry, I should have been more clear to say it wasn’t a useful result, not a non-sensical one.
Thanks for the note of how to tweak the plug-in… I tried to figure it out myself, but my plug-in-fu is weak.
A thought as how to fix the problem: You could just make another setting for subset, which would give the result I’m interested in. Or maybe I don’t understand what you mean by “subset” exactly.
Offline
Re: smd_each: iterate over stuff
jeremywood wrote:
You could just make another setting for subset, which would give the result I’m interested in.
Yeah I could do that. Might actually do that because it’s easiest. Or I might add another plugin attribute that allows you to turn on/off that combined first entry. I’ll see what is most useful (probably subset="3"!)
I’m sort of wishing I’d implemented subset differently now; with all the stuff I’ve learnt recently I can see much better ways to do it, but that would sacrifice plugin backwards compatibility in a big way. Mind you, might not be a bad thing…
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
#20 2009-03-19 22:19:57
- jeremywood
- Member
- Registered: 2007-12-12
- Posts: 26
Re: smd_each: iterate over stuff
Bloke wrote:
…with all the stuff I’ve learnt recently I can see much better ways to do it…
That’s the story of my life! It’s the depressing/awesome1 side of learning and progress.
1 Whether that’s depressing or awesome depends on mood.
Last edited by jeremywood (2009-03-19 22:20:26)
Offline
Re: smd_each: iterate over stuff
Updated plugin to v0.12 | compressed
- Fixed stupid subset bug so
subset="2"now works with one or more items (thanks jeremywood) - Added two new replacement variables
{var_counter}and{var_total}which can now be tested via smd_if to do stuff depending on where you are in the list (thanks mrdale)
Share and Enjoy TM
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
Re: smd_each: iterate over stuff
Updated plugin to v0.2
New attribute: var_prefix. Whether this is considered a bug fix or a feature is open to interpretation, but if you used nested smd_each tags, the {var_value} of the inner one would take on the value of the outer tag; undesirable.
This has now been rectfiied but it introduced a backwards-incompatible change. The var_prefix attribute now defaults to smd_. Thus, everywhere you once had {var_value}, {var_name}, {var_total}, etc will now have to become {smd_var_value}, {smd_var_name}, {smd_var_total}…
Alternatively you can set var_prefix="" in your (non-nested) tags to restore the existing functionality but be warned that if you subsequently nest an smd_each tag inside that, you’ll probably be issued with a plugin error. Especially if using collate mode.
Sorry to have to do this but I’ve just found a fab implementation that requires nested smd_each tags and just had to fix the plugin ;-)
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
#23 2010-02-22 06:10:16
- roelof
- Member
- Registered: 2005-03-27
- Posts: 647
Re: smd_each: iterate over stuff
Hello Bloke,
Mayby you can help me.
I have in a cusntom field named pagenr the pagenr where a article must be placed.
So every pagenr can be used more then one time.
What I need is that this plugin checks every custom field of a particular month and also check if that value is used before by the article before.
Can this be done by this plugin.
Roelof
P.S. I could use a <if-diifferent > and <article custom tag> but then I don’t know how to fill the smd-each loop
Last edited by roelof (2010-02-22 13:22:22)
Offline
#24 2010-02-23 07:19:04
- roelof
- Member
- Registered: 2005-03-27
- Posts: 647
Re: smd_each: iterate over stuff
Hello Speeke,
I think it can work with a double smd-each.
There are two things I can’t do : read the months and years from database and make the months bold.
This is a script which is not tested.
<txp:smd_each var-prefix="year_" list="2001,2005"
<txp:smd_each var-prefix="month_" list="1,2,3,4,5,6,7,8,9,10,11,12"
<txp:variable name="monthpage value='<txp:article_custom month="{year_smd_var_value - month_smd_var_value}" limit="99">' /> Look if there is a article of that month
<txp:if_variable name="monthpage" = "" >
put the month in normal text.
<txp:else />
put the text in bold.
</txp:if_variable>
</txp:smd_each>
</txp:smd_each>
p.Roelof
Last edited by roelof (2010-02-23 18:25:33)
Offline
Re: smd_each: iterate over stuff
OK finally got a challenge for this bad boy.
I have a custom field named “related_programs_groups” which returns stuff like “item1|item2|item3”
What I want to do is make links out of those suckers.
like: <a href="/events?c=item2">item2</a> etc…
so far this don’t do the trick….
<ul>
<txp:smd_each include="custom_4" subset="2" delim="|">
<li><a href="/events?c={smd_var_value}">{smd_var_value}</a></li>
</txp:smd_each>
</ul>
Any ideas, you geeky freakin’ geniuses? :)
[edit] OK, I’m a geeky Freakin’ genius…
<txp:smd_each type="field" include="related_programs_groups" subset="2" delim="|">
<li><a href="/events?c={smd_var_value}">{smd_var_value}</a></li>
</txp:smd_each>
Last edited by mrdale (2010-05-28 01:24:12)
Offline
Re: smd_each: iterate over stuff
I think I’ve spotted an obscure bug:
This dummy code:
<txp:smd_each type="field,fixed" include="title, who:yo:vos:el, aaa:1:2:3:4, bbb:5, ccc:6" subset="2">
{smd_var_name} = {smd_var_value}<br />
</txp:smd_each>
will correctly output this:
title_1 = The Article Title
who_1 = yo
who_2 = vos
who_3 = el
aaa_1 = 1
aaa_2 = 2
aaa_3 = 3
aaa_4 = 4
bbb_1 = 5
ccc_1 = 6
Now, if we just rename those aaa, bbb and ccc variables to just a, b, and c:
<txp:smd_each type="field,fixed" include="title, who:yo:vos:el, a:1:2:3:4, b:5, c:6" subset="2">
{smd_var_name} = {smd_var_value}<br />
</txp:smd_each>
this will incorrectly output:
title_1 = The Article Title
who_1 = yo
who_2 = vos
who_3 = el
a =
a_1 = 1
a_2 = 2
a_3 = 3
a_4 = 4
b =
b_1 = 5
c_1 = 6
c =
The incorrect thing there are the a =, b = and c = lines (also it looks somewhat incorrect that c = comes after c_1 = 6, doesnt?).
Those shouldn’t be in the output, as we are using subset="2", and, btw, remember: the only change we made was renaming the variables!
Now, believe or not, this bug seems to be triggered by this: type="field,fixed".
If we remove the field value, so we code looks like this:
<txp:smd_each type="fixed" include="title, who:yo:vos:el, a:1:2:3:4, b:5, c:6" subset="2">
{smd_var_name} = {smd_var_value}<br />
</txp:smd_each>
we again get the correct output, which for this case, it will be:
who_1 = yo
who_2 = vos
who_3 = el
a_1 = 1
a_2 = 2
a_3 = 3
a_4 = 4
b_1 = 5
c_1 = 6
So, bottom summary: a combination of type="field,fixed" and one-character variable names (a:, b:, c:) leads to incorrect output. Removing field or adding more chars to variable names (aaa, bbb, ccc) fixes it.
Offline
Re: smd_each: iterate over stuff
Hi Stef.
Do you think this plugin could have some break/continue capabilities?
I’m thinking of a way to use it to iterate over a list of fields (example: custom_1, custom_2, custom_3), and break the loop on certain condition. For example, once the iteration reaches a field with some content (i.e. not empty), it does whatever it has to do and then breaks the loop.
Probably doable with just smd_if (to test the value of the field) and also setting some boolean flag (a txp:variable, for example), both conditions needing to be true on each loop. This method may be enough to simulate break/continue capabilities on the iterator, although it has the disadvantage that it will have to iterate over every field (i.e. the loop doesn’t really get “break”).
Am I making sense?
Offline
#28 2012-01-31 06:43:41
- mmelon
- Member
- Registered: 2006-03-02
- Posts: 95
Re: smd_each: iterate over stuff
Hi guys,
Could someone with more experience with this plugin please advise me if it can do the following:
I am using
<txp:variable name=“hasnav”><txp:article_custom/></txp:variable>
to assign a list of articles to a variable and then testing against that variable.
In essence I have already queried the database and retrieved the articles so I want to avoid calling txp:article_custom again lower down.
Can I use smd_each, maybe with the correct delimiter, to loop over articles stored in a variable like above?
If so, how would I access the normal article fields, so instead if
<txp:article_custom section=“blah”>
<txp:permlink/>
</txp:article_custom>
Could I use the previously grabbed articles and do something like
<txp:smd_each type=“txpvar:hasnav”>
{smd_var_value}
</txp:smd_each>
I suspect this isn’t possible unless I originally grab the articles using smd_query. It’s just that I am comfortable with the syntax of article_custom.
Kind regards,
Mike
Offline
#29 2012-02-07 08:50:27
- mmelon
- Member
- Registered: 2006-03-02
- Posts: 95
Re: smd_each: iterate over stuff
I guess nobody has tried this. Can anybody recommend any other threads or resources dealing with page. Speed and minimizing database calls?
Offline
Re: smd_each: iterate over stuff
mmelon wrote:
assign a list of articles to a variable and then testing against that variable.
Sorry I missed this one. Yes it’s possible:
<txp:variable name="hasnav"><txp:article_custom section="blah" form="article_list" break="@SMD@" /></txp:variable>
<txp:smd_each type="txpvar" include="hasnav" subset="2" delim="@SMD@">
{smd_var_value}
</txp:smd_each>
That will present each ‘row’ from article_custom as a separate entity. It’s up to your article_custom form and attributes to make sure you don’t have any extra markup in there that you don’t want. You can apply the markup in the smd_each body once you’ve done your jiggery pokery.
The key to it is choosing a break that’s unlikely to occur in the article_custom output.
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