Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#25 2010-05-28 01:13:25

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

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

#26 2012-01-05 23:19:20

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

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.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#27 2012-01-19 19:52:17

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

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?


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

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

#30 2012-02-07 11:15:35

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

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.

Txp Builders – finely-crafted code, design and Txp

Offline

#31 2012-03-19 11:27:20

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 637
Website

Re: smd_each: iterate over stuff

Is this a smd_each case?
I got a custom-field as a textarea (glz_custom_fields)
and I’m looking to split each line by a line-break (\n) or (<br/>),
and then split that line again by a colon (:) and wrap the second part (the one that after the colon) with a span tag.

From:

<p>
property: value
property: value
property: value
</p>

To:

<p>
property: <span>value</span>
property: <span>value</span>
property: <span>value</span>
</p>

What I got for now it’s this code that output the custom-filed with <br/> for each line ( got it here ):

<p><txp:php>echo nl2br(trim(custom_field(array('name' => 'extra_properties','escape'=>0))));</txp:php></p>

And I added rah_replace to replace the regular line-break (\n) with nothing so the output will be a single line but with <br/> from the php code.

<p><txp:rah_replace from="
" to=""><txp:php>echo nl2br(trim(custom_field(array('name' => 'extra_properties','escape'=>0))));</txp:php></txp:rah_replace></p>

In the end the result I wish for is this:

<p>property: <span>value</span><br/>property: <span>value</span><br/>property: <span>value</span></p>

EDIT: In some cases lines may not include a colon so they only need to get a line-break with no span.

Last edited by THE BLUE DRAGON (2012-03-19 11:30:01)

Offline

#32 2012-03-19 11:37:05

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

Re: smd_each: iterate over stuff

THE BLUE DRAGON wrote:

Is this a smd_each case?

I don’t think so. I’m pretty sure smd_each assumes your stuff is split by a comma. It’s not very clever.

smd_wrap might be worth a look though, as it has split, combine and replace capabilities and you can wrap a given element that’s been split. Not sure how it’ll work with the newline scenario as I’ve never tried it, but in theory it should be able to do it.

Last edited by Bloke (2012-03-19 11:37:31)


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

#33 2012-03-19 13:34:48

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 637
Website

Re: smd_each: iterate over stuff

Thanks I will take a look at smd_wrap :)

Offline

#34 2012-03-26 19:06:29

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: smd_each: iterate over stuff

Okay, I’m trying to do something similar to what mrdale talked about here — glancing over the replies since then, I didn’t spot one that dealt with his issue, but please let me know if it’s covered elsewhere.

I use a custom field to assign multiple authors to a single article, as is common in academia. I’m using smd_each to iterate over those and list them for attribution in the article template, and I’m using smd_bio to give the authors firstname and lastname fields to flexibly format the names according to different citation conventions. Everything works as long as I separate the author IDs by commas in my custom field.

The problem creeps in when I use glz_custom_fields to make the field a multi-select instead of a plain text field. glz_cf automatically uses | as the delimiter— I could change it to a comma using rah_replace and txp:variable, but I’d rather just change the delimiter used by smd_each. However, the required subset="2" setting only takes commas as a delimiter. Is that right?

Here’s some code, if that helps:

With custom field author_ids input: author-1, author-2, this works:

<txp:smd_each
    type="field"
    include="author_ids"
    subset="2"><!-- My author info --></txp:smd_each>

But with multiselect enabled, the field output looks like this: author-1|author-2; the above code no longer recognizes the author IDs as separate variables. Setting delim, outdelim, or paradelim to | doesn’t help, as none of those seem to refer to the delimiter within the custom field content.

Is there a solution I’m overlooking, or should I force commas into the custom field output using rah_replace and assign that list to a new variable?

Thank you!

Offline

#35 2012-03-26 19:20:09

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: smd_each: iterate over stuff

After posting this, I realized that the multiselect field won’t meet my needs, since you can’t control the order in which authors will be listed— that’s kind of important for academic content. While I’m curious about a solution to this for future struggles, please don’t divert your attention from more important endeavors!

Offline

#36 2012-03-26 19:55:19

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,754
Website

Re: smd_each: iterate over stuff

I realized that the multiselect field won’t meet my needs, since you can’t control the order

John, bsmSelect may interest you then. Take a look at the sortable examples.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB