Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Skipping articles with an empty custom field?
I have found a solution, so I am just checking if this is a good option, or whether there are other, better options?
Situation:
I have an article_custom tag that outputs JSON for points of interest (POI) on a map. The longitude and latitude are stored in two custom fields.
My client has added new POI-articles but not yet entered the longitude and latitude, causing incomplete JSON entries to be output. The js-script showing the map POIs then throws an error and no points are shown at all on the map.
Intention:
To guard against this, I want article_custom to not output POI entries those with incomplete data.
Attempt 1:
Skip the code output by checking for the empty custom fields:
<txp:if_custom_field name="poi_lat" value="" not><txp:if_custom_field name="poi_long" value="" not>
…
</txp:if_custom_field></txp:if_custom_field>
This skips those entries, but the break=","
in the article_custom tag does get output, causing the JSON to be invalid.
Attempt 2:
Put the comma in the output and use the following to output separating commas except for the last article
<txp:if_last_article not>,</txp:if_last_article>
But here too, the last article in the set of matches that article_custom finds is one with an empty custom field. The last complete POI entry is not the last article so the final comma is output … and the JSON is again invalid.
Attempt 3:
Try to exclude articles with empty custom fields from the search matches, by excluding the custom fields that have an empty value
<txp:article_custom section="locations" wraptag="" break="," custom_11="" custom_12="" exclude limit="999" sort="ID asc"> …
where custom_11 and custom_12 hold the lat and long values. Here I get no output at all. If I drop the exclude and just search for the empty items, I get only the POIs with empty details, so the filter does work but not in reverse.
Attempt 4:
Match against the relevant custom fields (but do not specify any value, neither something or nothing)
<txp:article_custom section="locations" wraptag="" break="," custom_11 custom_12 match limit="999" sort="ID asc"> …
THIS WORKS!
Is there a better way? And if not, maybe this helps someone else?
TXP Builders – finely-crafted code, design and txp
Offline
Re: Skipping articles with an empty custom field?
Would using <txp:evaluate><txp:custom_field name="poi_lat" />blah</txp:evaluate>
do the trick?
Offline
Re: Skipping articles with an empty custom field?
Did you try simplifying it?
<txp:article_custom section="locations" wraptag="" break=",">
<txp:if_custom_field name="poi_lat" value=""><txp:else />...</txp:if_custom_field >
</txp:article_custom>
or
<txp:article_custom section="locations" wraptag="" break=",">
<txp:if_custom_field name="poi_lat">...<txp:else /></txp:if_custom_field >
</txp:article_custom>
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: Skipping articles with an empty custom field?
giz wrote #340043:
Would using
<txp:evaluate><txp:custom_field name="poi_lat" />blah</txp:evaluate>
do the trick?
Thanks. It might, except that the actual code is a larger block – including POI title, link, image, type (category) – so some of that code will succeed. It could work with txp:evaluate test="custom_field"
if the longitude and latitude are the only custom fields in that code block.
That said, I’m not sure if it resolves the trailing comma on the last article. That one comma throws the JSON out.
colak wrote #340045:
Did you try simplifying it?
<txp:article_custom section="locations" wraptag="" break=",">...
…
Thank you too. I think that is more or less what I have with attempt 1, except that I’m using not
instead of txp:else
.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Skipping articles with an empty custom field?
I think your n°4 is the most straightforward one, though match
should not be necessary here.
Probably, n°3 with exclude="custom_11, custom_12"
would work too, but I’m not sure about AND/OR
logic there.
Offline