Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2009-03-04 22:56:15

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

Re: smd_each: iterate over stuff

mrdale wrote:

I’m looking for a file equivalent of @<txp:if_first/last_article>@

First/last doesn’t have much of a concept with files since the timestamp is not taken into account (kind of related). I believe — may be wrong — that the “last” file is always the one you uploaded most recently. So, no, this plugin won’t help in that regard, though you should be able to iterate over them and if the upload order is actually the order you want then yeah it can help(ish).

You could combine the container of smd_each with smd_if — assuming you can get the files in the order you want in the first place. Or use smd_query to grab all the files in the order you want them and then use that plugin’s container to do the dirty work (perhaps also with smd_if).

Last edited by Bloke (2009-03-04 22:56:57)


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

#14 2009-03-04 23:03:27

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: smd_each: iterate over stuff

Yeah, I have no problems getting the files in the right order, but when I offload the display work to the form I lack the flexibility I need to format the first and list of those items…

hmm… thinking….

Offline

#15 2009-03-04 23:26:36

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

Re: smd_each: iterate over stuff

mrdale wrote:

when I offload the display work to the form I lack the flexibility I need to format the first and list of those items…

Uhh, yeah, damn. Cos smd_each has no concept of ‘start’ and ‘end’ of a list.

Perhaps it should. After all, I retrofitted other plugins with that ability (e.g. smd_query and smd_gallery give out {} vars that can be tested with smd_if to take action on the first and last elements). Maybe I’ll see what I can do in the next version. For now you’re probably stuck with smd_query, sorry.


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

#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

#17 2009-03-19 19:40:20

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

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.

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

#19 2009-03-19 21:28:05

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

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.

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

#21 2009-04-04 20:38:15

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

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.

Txp Builders – finely-crafted code, design and Txp

Offline

#22 2009-08-30 12:02:08

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

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.

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

Board footer

Powered by FluxBB