Textpattern Forum

You are not logged in. Register | Login | Help

#21 2012-07-05 05:50:21

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 4,926
Website

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.org | neme-imca.org | hblack.net | LABS

Offline

#22 2012-07-05 20:24:13

etc
Plugin Author
Registered: 2010-11-11
Posts: 1,147
Website

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

#23 2012-07-08 06:05:17

mrdale
Moderator
From: Walla Walla
Registered: 2004-11-19
Posts: 2,051
Website

Re: [solved x 2] tricky problem: format article custom fields into a table

OK, now you made me go and look into xPath.

Offline

#24 2012-07-08 21:50:03

etc
Plugin Author
Registered: 2010-11-11
Posts: 1,147
Website

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

Board footer

Powered by FluxBB