Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 638
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: 12,452
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.

Hire 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: 638
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: 1,000
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: 1,000
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: 5,202
Website GitHub

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

#37 2012-03-26 21:31:19

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,452
Website GitHub

Re: smd_each: iterate over stuff

johnstephens wrote:

Setting delim, outdelim, or paradelim to | doesn’t help

Unfortunately no, cf. the comment above to the blue dragon. The plugin is pretty stupid and it really needs a make-over. If you solve the order thing and need the plugin for your site, let me know and I’ll divert some time to it.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#38 2012-03-26 23:31:27

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

Re: smd_each: iterate over stuff

johnstephens wrote

However, the required subset=“2” setting only takes commas as a delimiter. Is that right?

In my experience, subset="2" always required colon (:) as separator, when using type="fixed" (don’t recall trying with other types).
For example:

<txp:smd_each type="fixed" include="my_categories:music:film:games:arts:literature" subset="2">...</txp:smd_each>

where my_categories is just the variable name and music, film, games, etc, are each of the values smd_each iterates over.
It always looked a bit odd to me to use the same separator (the colon) between the variable name and the list of values, and between the values themselves. Not complaining, just an observation!


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#39 2013-04-16 15:27:38

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

Re: smd_each: iterate over stuff

Smd Each Challenge I can’t quite get sorted.

I have two variables which are each lists (prices and sizes).

  • prices=”$3,$8,$2,$6”
  • sizes=“glass,pitcher,glass(happy hour)|pitcher(happy hour)”

I want to output this:

<p>$3 bottle | $8 pitcher</p>
<p>$2 bottle(happy hour) | $6 pitcher(happy hour)</p>

What I really need to do is make a new variable which is an alternating list of prices and sizes. Any ideas?

Offline

Board footer

Powered by FluxBB