Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
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
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
Re: smd_each: iterate over stuff
Thanks I will take a look at smd_wrap :)
Offline
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
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
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
Re: smd_each: iterate over stuff
johnstephens wrote:
Setting
delim,outdelim, orparadelimto|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
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!
Offline
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