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: 10,568
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: 10,568
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: 10,568
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: 10,568
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: 10,568
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: 10,568
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-01-21 02:49:34

speeke
Member
From: Bruny Island, Australia
Registered: 2009-03-29
Posts: 161
Website

Re: smd_each: iterate over stuff

I am trying to produce the following – and driving myself a little bit bananas in the process :(

2010: 1 2 3 4 5 6 7 8 9 10 11 12
2009: 1 2 3 4 5 6 7 8 9 10 11 12
2005: 1 2 3 4 5 6 7 8 9 10 11 12
2001: 1 2 3 4 5 6 7 8 9 10 11 12

The idea is to create a link to any month (shown in bold above) within which one or more articles exists. Where no articles exist for a given month, the number referring to that month would be unlinked. I’m using for ($i = 1; $i < 13; $i++) {, where $i refers to the number of each month, to produce the output.

I have managed to nest the smd_query tags, using the following code:

<h4>Entries by Month</h4>
  <ul><txp:smd_query query="SELECT DISTINCT SUBSTR(Posted,1,4) AS theyear FROM textpattern WHERE custom_2 = '?s' AND Status = 4 GROUP BY Posted DESC">
    <li>{theyear}:
<txp:smd_query query="SELECT DISTINCT SUBSTR(Posted,1,7) AS themonth FROM textpattern WHERE custom_2 = '?s' AND Status = 4 AND SUBSTR(Posted,1,4) = {theyear} GROUP BY Posted ASC">
<txp:php>
$month_txt = substr("{themonth}",5,2);
$month_txt = ltrim($month_txt, '0');
for ($i = 1; $i < 13; $i++) {
  if ($month_txt == $i) {
    echo " <a href=\"<txp:site_url /><txp:section />/date/{themonth}/\">$i</a>";
  }
  else {
    echo " " . $i;
  }
}
echo "</li>";</txp:php></txp:smd_query></txp:smd_query>
  </ul>

But, obviously, the for loop is in the wrong location. Hence, I am trying to get my head around smd_each, hoping that this plugin will allow me to iterate over each month that has an article, outside of the smd_query.

I’m currently getting the following output from the code above:

2010: 1 2 3 4 5 6 7 8 9 10 11 12
2009: 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
2005: 1 2 3 4 5 6 7 8 9 10 11 12
2001: 1 2 3 4 5 6 7 8 9 10 11 12

Can smd_each help me here? I’ve been pouring over your examples of how to use it, but can’t get my head around how to modify it for this purpose.

Last edited by speeke (2010-01-21 21:25:13)


“People who value their privileges above their principles soon lose both.”
Dwight D. Eisenhower

Offline

#24 2010-01-22 06:56:43

speeke
Member
From: Bruny Island, Australia
Registered: 2009-03-29
Posts: 161
Website

Re: smd_each: iterate over stuff

Never mind. I couldn’t see how to use smd_each so I’ve concocted a very ugly solution using <txp:php />.

The problem was that I needed to store an array of variables (in this case, dates that contained articles), and then loop through them outside the second smd_query tag. Anyway, I’ve included my awful code below, for some light entertainment ;)

<h4>Entries by Month</h4>
<ul>
  <txp:smd_query query="SELECT DISTINCT SUBSTR(Posted,1,4) AS theyear FROM textpattern WHERE custom_2 = '?s' AND Status = 4 GROUP BY Posted DESC">
  <li>{theyear}:
  <txp:smd_query query="SELECT DISTINCT SUBSTR(Posted,1,7) AS themonth FROM textpattern WHERE custom_2 = '?s' AND Status = 4 AND SUBSTR(Posted,1,4) = {theyear} GROUP BY Posted ASC">
  <txp:php>
    variable( array('name' => 'sdate{themonth}', 'value' => '{themonth}') );
  </txp:php>
  </txp:smd_query>
  <txp:php>
    for ($i = 1; $i < 13; $i++) {
      if (substr(variable(array('name' => 'sdate{theyear}-'.$i)),5,2) == $i) {
        $ddigit = variable(array('name' => 'sdate{theyear}-'.$i));
        echo " <a href=\"<txp:site_url /><txp:section />/date/$ddigit/\">$i</a>";
      }
      elseif (substr(variable(array('name' => 'sdate{theyear}-0'.$i)),6,1) == $i) {
        $sdigit = variable(array('name' => 'sdate{theyear}-0'.$i));
        echo " <a href=\"<txp:site_url /><txp:section />/date/$sdigit/\">$i</a>";
      }
      else {
        echo " " . $i;
      }
    }
  </txp:php>
  </li>
  </txp:smd_query>
</ul>

I did say “ugly” didn’t I?

So, just out of interest, could smd_each be used in this for loop situation?

Last edited by speeke (2010-01-22 07:08:16)


“People who value their privileges above their principles soon lose both.”
Dwight D. Eisenhower

Offline

Board footer

Powered by FluxBB