Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-06-12 17:11:59

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 7,755
Website

smd_each: iterate over stuff

Another plugin that’s sort of a programmer’s companion (like smd_if). This one allows you to pick a set of variables available within TXP — including from the URL line and the new <txp:variable /> tag — filter them to your liking by variable name or match-by-name/value and then chuck the values at a form so you can, well, do stuff with those names/values.

If your article fields contain further lists of things you can ask the plugin to dive in and treat those as pseudo-variables too.

Exceptionally useful when you don’t know how many variables are in somethnig (e.g. a browser address bar request) because it just keeps going until it runs out of variables. And if the filtering isn’t enough you can always put smd_if inside the output form and filter it further :-)

At the moment it will not be able to grab smd_vars because the multi-get functionality isn’t in the smd_vars plugin yet. It’s coming…

In the meantime, here’s smd_each

P.S. mrdale, check Example 4 in the help. Should do what you asked yesterday

Revision history
————————

All available versions and changes are listed here. Each entry indexes the relevant post(s) in the thread to learn about the features.

Last edited by Bloke (2009-08-30 12:03:51)


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

#2 2008-06-12 17:25:17

mrdale
Moderator
From: Walla Walla
Registered: 2004-11-19
Posts: 2,198
Website

Re: smd_each: iterate over stuff

nyuk, nyuk, nyuk. “ask and you shall receive” I guess I’m going to have to get religion…

Offline

#3 2008-06-22 05:45:19

mrdale
Moderator
From: Walla Walla
Registered: 2004-11-19
Posts: 2,198
Website

Re: smd_each: iterate over stuff

OK, I have a complicated scenario, which I can currently only solve in the following manner…

I make numerous article calls….

<txp:glz_article_custom retail_region="northwest" form="article-retail-map-nw" />
<txp:glz_article_custom retail_region="southwest" form="article-retail-map-sw" />
<txp:glz_article_custom retail_region="mountain" form="article-retail-map-mtn" />
many, many more...

the form “article-retail-map-xxx” differs only by two variables which we’ll call {value_name} and {value_title} here’s how it looks…

<txp:if_first_article>
<dt><a href="#" class="location" id="{value_name}">{value_title}</a></dt>
<dd><txp:zem_ir group="subhead">{value_title}</txp:zem_ir></txp:if_first_article>
<txp:output_form form="unrelated-markup" />
<txp:if_last_article>
<a href="#" class="region" id="{value_name}">{value_title}</a>
</dd></txp:if_last_article>

I have this working with many, many different forms that vary only slightly, but I feel so dirty. I’d prefer to dynamically construct the differences in forms with variables and get my form page back from a scrollathon.

  1. <txp:variable name="value_name" value="northwest,southwest,mountain"/>
  2. <txp:variable name="value_title" value="North West,South West, Mountain States"/>

I’m so almost there, with this plugin, I just need to see how it would tackle this job, and I’ll be able to kick off the training wheels…

Blokeroo, chumpus?

Offline

#4 2008-06-22 08:48:08

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 7,755
Website

Re: smd_each: iterate over stuff

mrdale wrote:

I’d prefer to dynamically construct the differences in forms with variables and get my form page back from a scrollathon.

There are a couple of issues here. Firstly, it sounds like you want to consolidate the glz_article_custom calls; secondly you want to use a generic form to display each article. The first is simple as you have seen with <txp:variable />. You could either do it like this:

<txp:variable name="region" value="northwest, southwest, mountain"/>

<txp:smd_each type="txpvar" include="region" subset="2">
 <txp:glz_article_custom retail_region="{var_value}" form="article-retail-map" />
</txp:smd_each>

OR, use a feature I’ve just added to v0.11 of smd_each | compressed which allows you to skip the txp:variable step and define your own variables to iterate over directly, using a new type called fixed viz:

<txp:smd_each type="fixed" include="region:northwest:southwest:mountain" subset="2">
 <txp:glz_article_custom retail_region="{var_value}" form="article-retail-map" />
</txp:smd_each>

Both of those will do the same thing and it’s just up to you how much typing you prefer :-)

Now, the 2nd part is more tricky and I need to make an assumption here. Does your article contain a custom field called “retail_region” that holds the value, ‘northwest’ or ‘southwest’ or something? I assume so because it looks to me like the glz_article_custom only returns articles that have that value set. But what I don’t get is where you could grab the corresponding ‘title’.

You could define it in a 2nd custom field but that’s plain awkward. Besides, smd_each cannot iterate over pairs of values (a shame but I couldn’t figure it out since I don’t think PHP can even do it cleanly, but I may have to look closer).

You might be better off in your form, listing all possible regions as a series of name:value pairs using the new type="fixed" notation, iterate over every one and use smd_if to only display the one that matches the current custom field. This is untested, but for example in your article-retail-map form:

<txp:smd_each include="northwest:North West, southwest:South West, mountain:Mountain States">
 <txp:smd_if field="region_list" operator="eq" value="{var_name}">
  <txp:if_first_article>
   <dt><a href="#" class="location" id="{var_name}">{var_value}</a></dt>
   <dd><txp:zem_ir group="subhead">{var_value}</txp:zem_ir>
  </txp:if_first_article>
  <txp:output_form form="unrelated-markup" />
  <txp:if_last_article>
   <a href="#" class="region" id="{var_name}">{var_value}</a>
   </dd>
  </txp:if_last_article>
 </txp:smd_if>
</txp:smd_each>

Does that at least get you going down the bumpy track to enlightenment? I can’t think of another way round it right now but if I figure something out I’ll holler.


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

#5 2008-06-22 16:39:23

mrdale
Moderator
From: Walla Walla
Registered: 2004-11-19
Posts: 2,198
Website

Re: smd_each: iterate over stuff

I should fill in some details…

I’m producing a definition list, where:

  • dt=“a region (ie North West)”
  • dd=“All of the stores where you can buy a product in that region”

The “dd” part may contain one or more retail stores. Each retail store is an individual article and uses a custom field “retail_region” (multi-select) to determine which regions it goes into…

So “Safeway” might be an article that may appear in ALL regions, while “Trader Joes” appears only in the Northwest and Southwest, etc…

I’m then using a javascript form ALAP to present these regions visually on a geographic map.

I THINK I DO need to have the individual article calls for each region, because, we’re talking about redundant data… but it would be nice to put the kybosh on the form convention.

BTW it looks pretty cool… I need to upload it from my local install, so you can see.
prod it a little if you like

[edit:fixed link]

Last edited by mrdale (2008-06-23 04:46:34)

Offline

#6 2008-09-22 09:45:11

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

Re: smd_each: iterate over stuff

Bloke thank you, this is another very useful plugin and will be a real time saver!
(Actually I spent a lot of time with it because I downloaded v1.0 trying to make it work with the type=“fixed” attribute you introduced in v0.11 – please update your first post and your txp-resources link!!)

Last edited by redbot (2008-09-22 10:01:08)

Offline

#7 2008-09-22 10:29:58

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 7,755
Website

Re: smd_each: iterate over stuff

redbot wrote:

please update your first post and your txp-resources link!!

Glad the plugin was (eventually) useful for you. Really sorry about the old links, I’m such a slacker at keeping things in sync sometimes. I’ve updated the entries now.


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

#8 2008-09-22 10:49:41

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

Re: smd_each: iterate over stuff

Thank you, and yes… the plugin was eventually really useful ;) – as all your other plugins.

Offline

#9 2009-01-31 00:03:57

jeremywood
Member
Registered: 2007-12-12
Posts: 26

Re: smd_each: iterate over stuff

I can’t figure out how to get this to iterate over a comma-delimited list from a custom field.

I’ve tried:

<txp:smd_each type="field" include="RelatedProducts" subset="2">
 * {var_value}
</txp:smd_each>

Where the contents of the custom field “RelatedProducts” are: “7900, be90, 7600”.

I’ve been able to make the following workaround by using a variable, but it doesn’t seem like that should be necessary.

<txp:variable name='related'>
    <txp:custom_field name="RelatedProducts" />
</txp:variable>
<txp:smd_each type="txpvar" include="related" subset="2">
* {var_value}
</txp:smd_each>

Last edited by jeremywood (2009-01-31 00:04:22)

Offline

#10 2009-01-31 09:15:37

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 7,755
Website

Re: smd_each: iterate over stuff

jeremywood wrote:

<txp:smd_each type=“field” include=“RelatedProducts” subset=“2”>

Hmmm, it should work. Try using lower case for your custom field name. TXP always converts them to lower case “internally” and this plugin uses the internal name, not the name you have specified. That doesn’t explain why the <txp:custom_field /> method works, though. Perhaps that tag automatically converts them to lower case for you; something I may be able to make the plugin so in future. I’m not sure if that would have any impact on the URL variable and TXP variable support; I’ll have to think about it.

I’ll look into it for the next release, or at the very least add the lower case thing to the documentation as a warning.


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

Board footer

Powered by FluxBB