Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#25 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

#26 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

#27 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

#28 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

#29 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

#30 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

#31 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

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

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,243
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

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

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 619
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

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

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,243
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

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

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

Re: smd_each: iterate over stuff

Thanks I will take a look at smd_wrap :)

Offline

#36 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

Board footer

Powered by FluxBB