Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Re: [solved x 2] tricky problem: format article custom fields into a table
Uli, he adds the variable to itself, at the same time it adds the value of the custom field, making a list of values.
If the first value of the variable is “dog”, and then the custom field value is “cat”, the variable will end up being “cat, dog”. On each iteration (over textpattern articles), the variable gets a new value.
Offline
#14 2012-07-04 14:07:04
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,306
Re: [solved x 2] tricky problem: format article custom fields into a table
Ah, thanks, Julián, didn’t know that you can do that with a variable, thought it’d lead to a short circuit.
And b) had forgotten we’re in an article_custom loop, thus it isn’t the only call of this variable.
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: [solved x 2] tricky problem: format article custom fields into a table
A glass of 2007 Malbec would make it all seem better.
Offline
Re: [solved x 2] tricky problem: format article custom fields into a table
colak wrote: I was wondering if
<txp:chh_if_data>
could be replaced with<txp:if_custom_field name="wine_cellaring">
good call you crazy Cypriot. Updated the solution to add classes for odd/even rows and other stuff.
Offline
Re: [solved x 2] tricky problem: format article custom fields into a table
<txp:if_custom_field name="wine_cellaring" value="">—<txp:else/><txp:custom_field name="wine_cellaring" /></txp:if_custom_field>
Want to shorten that even more?
You could try:
<txp:custom_field name="wine_cellaring" default="-" />
(yeah, txp:custom_field
accepts a default
attribute, crazy, isn’t it?)
Offline
#18 2012-07-04 19:35:49
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: [solved x 2] tricky problem: format article custom fields into a table
I think we need a Wall of Fame for code. This specimen definitely deserves a place there!
Offline
Re: [solved x 2] tricky problem: format article custom fields into a table
That code can become bit of nightmare when it comes to performance and the amount of resource it uses. For starters, it generates 4 MD5 hashes for each article it displays. While calculating a single md5 hash doesn’t take much, on a site that has full set of 999 articles published in section releases it will generate 3996 MD5 hashes in total on each page load. On a as large site it can also use up to 1002 SQL queries, and there is quite a bit of copying and processing going on too.
uli wrote:
Ah, thanks, Julián, didn’t know that you can do that with a variable, thought it’d lead to a short circuit.
No, because variables store just results, not code or references. If it worked like a Form template or an article field, and stored and evaluated code, then in that situation you would have a circular reference since the instance of variable tag would call the variable tag again. But since it stores just the value the variable returned on the last call, fortunately no.
Last edited by Gocom (2012-07-04 20:44:47)
Offline
Re: [solved x 2] tricky problem: format article custom fields into a table
Gocom wrote: That code can become bit of nightmare
atta boy… feel free to refactor that for me ;)
Manequi wrote: Want to shorten that even more?
way cool. great to know That’ll save me 5% on my keyboard’s life. I’ll try it. BUT interestingly enough the else part of that doesn’t actually work in the code. no idea why.
Last edited by mrdale (2012-07-04 22:47:23)
Offline
Re: [solved x 2] tricky problem: format article custom fields into a table
mrdale wrote:
BUT interestingly enough the else part of that doesn’t actually work in the code. no idea why.
I would change it with the code Julián suggested or with:
<txp:if_custom_field name="wine_cellaring">
<txp:custom_field name="wine_cellaring" />
<txp:else/>—
</txp:if_custom_field>
Last edited by colak (2012-07-05 12:27:12)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: [solved x 2] tricky problem: format article custom fields into a table
Nothing would beat <txp:php />
, but it is forbidden, I suppose? Then go for etc_query, it should be faster than recursive <txp:article_custom />
anyway. The idea is to construct an initial table (one row per release), find the vintage range, then fill the “gaps”:
<txp:variable name="releases"><!-- Stores the initial table rows -->
<txp:article_custom sort="custom_34,custom_5 asc" limit="999">
<txp:variable name="release" value='<txp:if_different><txp:custom_field name="wine_release_name" /></txp:if_different>' />
<txp:if_first_article>
<tr data-release='<txp:custom_field name="wine_release_name" />'>
<txp:else />
<txp:if_variable name="release" value=""><txp:else />
</tr><tr data-release='<txp:custom_field name="wine_release_name" />'>
</txp:if_variable>
</txp:if_first_article>
<td data-vintage='<txp:custom_field name="wine_vintage" />'><txp:custom_field name="wine_cellaring" /></td>
<txp:if_last_article></tr></txp:if_last_article>
<txp:etc_query functions="min,max"><!-- Vintage range -->
<txp:variable name="min" value="{min({?min|9999},{?wine_vintage})}" />
<txp:variable name="max" value="{max({?max|0},{?wine_vintage})}" />
</txp:etc_query>
</txp:article_custom>
</txp:variable>
<table>
<thead><!-- Head first -->
<tr><th>Releases</th><txp:etc_query data="[{?min}..{?max}]" markup="json" break="th" /></tr>
</thead>
<!-- Body now, with gaps filled -->
<txp:etc_query data='<txp:variable name="releases" />' query="tr" break="tr" wraptag="tbody">
<td><b>{@data-release?}</b></td>
<txp:variable name="row">{*}</txp:variable>
<txp:variable name="cmin" value="{?min|0|-1}" />
<txp:etc_query data="{{?row}}" query="td">
<txp:etc_query data='[{{{?cmin|0|+1}}}..{{@data-vintage?}}]' markup="json" break="td">
{{{$=({{{?}}}|{{@data-vintage?}}).?({{?}}|-)}}}
</txp:etc_query>
<txp:variable name="cmin" value="{{@data-vintage?}}" />
</txp:etc_query>
<txp:etc_query data='[{{?cmin}}..{{?max}}]' markup="json">
{{$=({{?}}|{{?max}}).?(|<td>-</td>)}}
</txp:etc_query>
</txp:etc_query>
</table>
I have tested it here, with custom fields replaced by Section
, Posted
and so on.
Last edited by etc (2012-07-06 12:43:58)
Offline
Re: [solved x 2] tricky problem: format article custom fields into a table
OK, now you made me go and look into xPath.
Offline
Re: [solved x 2] tricky problem: format article custom fields into a table
Good intention, but this is not the best example, sorry, there is very little xpath therein. And actually, since etc_query
can import JSON too, the following code should run about twice as fast and without any dom manipulation. You’ll have to (re)download the latest version of etc_query
though (and I’ll have to update its help).
<txp:variable name="vintage" value="" />
<txp:variable name="releases"><!-- json array of data -->
<txp:article_custom sort="custom_34,custom_5 ASC" limit="999">
<txp:if_first_article><txp:else />,</txp:if_first_article>
{"release":"<txp:custom_field name='wine_release_name' />", "vintage":"<txp:custom_field name='wine_vintage' />", "cellaring":"<txp:custom_field name='wine_cellaring' />"}
<txp:variable name="vintage" value='<txp:variable name="vintage" />,<txp:custom_field name="wine_vintage" />' />
</txp:article_custom>
</txp:variable>
<-- find the min and max vintages -->
<txp:etc_query data="{?vintage||ltrim($|,)}" markup="raw">
<txp:variable name="min" value="{$do_list.min}" />
<txp:variable name="max" value="{$do_list.max}" />
</txp:etc_query>
<table><thead><tr>
<th>Releases</th><txp:etc_query data="[{?min}..{?max}]" break="th" /></tr></thead>
<tbody>
<txp:etc_query data='[<txp:variable name="releases" />]' markup="json">
<txp:if_variable name="release" value="{release?}"><txp:else />
<!-- complete and close the previous row -->
<txp:etc_query data="[{{?cmin,max}}<.{?max}]" markup="json" break="td">-</txp:etc_query>
<txp:if_variable name="cmin"></tr></txp:if_variable>
<!-- start the next one -->
<tr><td><b>{release?}</b></td>
<txp:variable name="cmin" value="{?min|1|-1}" />
</txp:if_variable>
<!-- fill the gaps between vintages -->
<txp:etc_query data="[{{?cmin}}<<{vintage?}]" markup="json" break="td">-</txp:etc_query>
<td>{cellaring?}</td>
<txp:variable name="cmin" value="{vintage?}" />
<txp:variable name="release" value="{release?}" />
</txp:etc_query>
<txp:etc_query data="[{?cmin}<.{?max}]" markup="json" break="td">-</txp:etc_query>
</tr></tbody>
</table>
Offline