Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Problems with match in <txp:if_variable>
Hello, I am working on a redesign of an old txp-website for a hiking club. This problem showed up when doing an article count.
I am setting a variables value to the article count generated by <txp:search_result_count escape=‘trim’ text=’‘/>
Afterwards I want to check if its “1” to append “ hike” instead of “ hikes”.
Unfortunately I only get the “else” result. I have tried several espace-methods (e.g. “trim, number”) but this changed the variable-value to “0”
<txp:variable name="resultCount" value="<txp:search_result_count escape='trim' text=''/>" />
<li><span id="js-hikecount"><txp:variable name="resultCount"/></span>
<txp:if_variable name="resultCount" value='1'> hike<txp:else/> hikes</txp:if_variable>
</li>
Any ideas what I am doing wrong?
Best regards,
Peter
Last edited by Pemischl (2022-10-22 12:12:58)
Offline
Re: Problems with match in <txp:if_variable>
You will have seen that txp:search_result_count will append “article” or “articles” automatically but in your case you want “hike” or “hikes”. You can achieve what you want with txp:if_search_results and the attribute min="2"
, e.g.
<li>
<txp:search_result_count text="" html_id="js-hikecount" wraptag="span" />
hike<txp:if_search_results min="2">s</txp:if_search_results>
</li>
Your if_variable
approach is also good but you have your single and double quotes mixed up. Single quotes for an attribute mean ‘process this and put the result here’ while double quotes mean “the value I’ve written here”.
So you need to slightly amend your code as follows:
<txp:variable name="resultCount" value='<txp:search_result_count escape="trim" text="" />' />
<li>
<span id="js-hikecount"><txp:variable name="resultCount" /></span>
<txp:if_variable name="resultCount" value="1"> hike<txp:else/> hikes</txp:if_variable>
</li>
That should achieve the same result.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Problems with match in <txp:if_variable>
Thank your for the quick reply and help!
Even with copy/pasting your if-variable fix, it is still not working in my setup.
I have already spent too much time on this single “s” and fixed it with three lines of javascript
<li id="hikecount">
<span id="js-hikecount"><txp:variable name="resultCount" /></span>
<span id="js-hikelabel"><txp:if_variable name="resultCount" value="1"> hike<txp:else/> hikes</txp:if_variable></span>
</li>
<script>
window.addEventListener('DOMContentLoaded',()=>{
let count = Number(document.getElementById('js-hikecount').innerText);
if (count == 1) document.getElementById('js-hikelabel').innerText = "hike";
})
</script>
Last edited by Pemischl (2022-10-23 08:40:35)
Offline
Re: Problems with match in <txp:if_variable>
That works too, of course (though you no longer need txp:variable there).
Strange, though: I did test both variants and they should work – here screenshots of hike and hikes.
Did you perhaps miss out a line while copying? The most crucial line is the txp:variable tag at the beginning that sets the variable “resultCount”. (I don’t meant to be discourteous, just trying to understand why – you don’t show that line in your code snippet).
TXP Builders – finely-crafted code, design and txp
Offline
Re: Problems with match in <txp:if_variable>
I did some further testing. The if_variable variant works correctly when I use it in an article-form.
But: when I use it within an output_form it does not work anymore. It puts out the correct hikeCount, but the if/else match in if_variable does not work.
Last edited by Pemischl (2022-10-23 19:16:36)
Offline
Re: Problems with match in <txp:if_variable>
Interesting. Maybe / probably that is due to what’s described in Example 2 of the if_search_results docs.
I’m wondering if you might be able to use a process-order trick to populate your variable in the article form first and then output the result of that further up the page. Take a look at this topic/thread for some background, but the basic principle is:
<!-- your header area -->
<txp:hide process="2">
<txp:variable name="hikes_count" html_id="js-hikecount" wraptag="span" />
</txp:hide>
…
<txp:article form="article_form" listform="article_listform" />
and in your article article_form
, you then define the variable while you’re outputting it:
<txp:variable name="hikes_count" output trim>
<txp:search_result_count text="" /> hike<txp:if_search_results min="2">s</txp:if_search_results>
</txp:variable>
Might that work?
TXP Builders – finely-crafted code, design and txp
Offline
Re: Problems with match in <txp:if_variable>
Thanks again for your help! I could not make it work this way.
I solved it by putting the header-form, enclosed by <txp:if_first_article> into the article-tag
Offline