Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Evaluating whether to show a Category or Section in a Navigation Bar
I culled some ideas from here on the forum (both from Oleg) and came up with this.
<ul class="header__nav">
<li class="current"><a href="<txp:link_to_home />" title="">Home</a></li>
<li class="has-children">
<a href="#0" title="">Categories</a>
<txp:category_list wraptag="ul" break="li" class="sub-menu">
<txp:variable name="articles_exist">
<txp:article_custom category='<txp:category />' pgonly pageby="1" />
</txp:variable>
<txp:evaluate query='<txp:variable name="articles_exist" /> > 0'>
<txp:category link title />
</txp:evaluate>
</txp:category_list>
</li>
<txp:evaluate test="section">
<txp:section wraptag="li" name="about" link title />
</txp:evaluate>
<txp:evaluate test="section">
<txp:section wraptag="li" name="contact" link title />
</txp:evaluate>
</ul> <!-- end header__nav -->
I wanted to include this in a template conversion I am working on. The articlecustom returns the actual number of articles in a category or 0 if there are none. If there is at least 1 aka greater than 0 the category gets displayed. The section does the same thing only simpler.
evaluate was such a powerful addition to Textpattern.
Addendum: Colak points out below that with the proper use of single quotes (note the ‘’<txp:category />’‘) you can also do this:
<ul class="header__nav">
<li class="current"><a href="<txp:link_to_home />" title="">Home</a></li>
<li class="has-children">
<a href="#0" title="">Categories</a>
<txp:category_list wraptag="ul" break="li" class="sub-menu">
<txp:evaluate query='<txp:article_custom category=''<txp:category />'' pgonly pageby="1" /> > 0'>
<txp:category link title />
</txp:evaluate>
</txp:category_list>
</li>
<txp:evaluate test="section">
<txp:section wraptag="li" name="about" link title />
</txp:evaluate>
<txp:evaluate test="section">
<txp:section wraptag="li" name="contact" link title />
</txp:evaluate>
</ul> <!-- end header__nav -->
So whichever one you prefer for readability.
Addendum 2:
I was running into the non-static category url issue and I found that fix but also only displaying articles with categories was first solved by Colak back in 2009: static category urls
So this is the final version. Notice the addition of section=“default” to category_link. This stops you from getting those weird links to non-existent categories on section pages where you don’t need them.
<ul class="header__nav">
<li class="current"><a href="<txp:link_to_home />" title="">Home</a></li>
<li class="has-children">
<a href="#0" title="">Categories</a>
<txp:category_list wraptag="ul" break="li" class="sub-menu">
<txp:variable name="articles_exist">
<txp:article_custom category='<txp:category />' pgonly pageby="1" />
</txp:variable>
<txp:evaluate query='<txp:article_custom category=''<txp:category />'' pgonly pageby="1" /> > 0'>
<txp:category link title section="default" />
</txp:evaluate>
</txp:category_list>
</li>
<txp:evaluate test="section">
<txp:section wraptag="li" name="about" link title />
</txp:evaluate>
<txp:evaluate test="section">
<txp:section wraptag="li" name="contact" link title />
</txp:evaluate>
</ul> <!-- end header__nav -->
Last edited by michaelkpate (2020-07-01 12:09:55)
Offline
Re: Evaluating whether to show a Category or Section in a Navigation Bar
nice!
I’m wondering if this
<txp:variable name="articles_exist">
<txp:article_custom category='<txp:category />' pgonly pageby="1" />
</txp:variable>
<txp:evaluate query='<txp:variable name="articles_exist" /> > 0'>
<txp:category link title />
</txp:evaluate>
Could be changed to this
<txp:evaluate query='<txp:article_custom category=''<txp:category />'' pgonly pageby="1" /> > 0'>
<txp:category link title />
</txp:evaluate>
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: Evaluating whether to show a Category or Section in a Navigation Bar
colak wrote #324145:
Could be changed to this
That does indeed work. I didn’t know the double single quote trick.
Thanks.
Offline
Re: Evaluating whether to show a Category or Section in a Navigation Bar
michaelkpate wrote #324147:
That does indeed work. I didn’t know the double single quote trick.
Thanks.
np. Check docs.textpattern.com/tags/learning/#nesting-quotes-in-quotes-in re this feature.
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: Evaluating whether to show a Category or Section in a Navigation Bar
colak wrote #324148:
np. Check docs.textpattern.com/tags/learning/#nesting-quotes-in-quotes-in re this feature.
So the code in the example is:
<txp:variable name="file_count" value='<txp:file_download_list form="file_cat" category=''<txp:l10n_get_lang type="short" />'' />' />
<txp:if_variable name="file-count">
<h3>Some Header</h3>
<txp:file_download_list category='<txp:l10n_get_lang type="short" />' wraptag="ul" break="li" />
<txp:else />
<p>No files for this language.</p>
</txp:if_variable>
which could also be:
<txp:evaluate query='<txp:file_download_list form="file_cat" category=''<txp:l10n_get_lang type="short" />'' />' >
<h3>Some Header</h3>
<txp:file_download_list category='<txp:l10n_get_lang type="short" />' wraptag="ul" break="li" />
<txp:else />
<p>No files for this language.</p>
</txp:evaluate>
Ummm… I think.
Offline
Re: Evaluating whether to show a Category or Section in a Navigation Bar
michaelkpate wrote #324149:
which could also be:
That is indeed correct. I think that the example was posted before the evaluate
tag was introduced.
Admittedly I am still mostly using variables
in my sites as I feel more conceptually comfortable with them. One of the handful times I use evaluate
, is just appropriating your code example.
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: Evaluating whether to show a Category or Section in a Navigation Bar
colak wrote #324150:
One of the handful times I use
evaluate
, is just appropriating your code example.
Glad it was useful.
It took me a while to understand just what evaluate does and I still have to refer back to examples from time to time – one of the reasons for posting this thread, so I can come back here in a couple of years if I forget again.
Offline
Re: Evaluating whether to show a Category or Section in a Navigation Bar
Though this illustrates certain features of txp:evaluate />
, one can get to the same result with
<txp:if_variable name="articles_exist" not value="0" />
or simply
<txp:if_variable name="articles_exist" value />
Offline
Re: Evaluating whether to show a Category or Section in a Navigation Bar
etc wrote #324168:
one can get to the same result with
I just tried both and they both failed. I thought the second one might (0 is, after all, a value) but thought the first would succeed but it didn’t.
<txp:if_variable name="articles_exist" not value="0" />
<txp:category link title />
</txp:if_variable>
27.21 | 3.29 | <txp:category_list wraptag="ul" break="li" class="sub-menu">
27.27 | 0.36 | [SQL: SELECT name, parent, title, description FROM typerite_txp_category as txp_category WHERE name !='root' and type = 'article' order by name ASC ]
27.63 | | [Rows: 3]
27.65 | | [true]
27.66 | 1.29 | <txp:variable name="articles_exist">
27.67 | | [true]
27.68 | 1.26 | <txp:article_custom category='<txp:category />' pgonly pageby="1" />
27.69 | 0.32 | [attribute 'category']
27.69 | | [true]
27.71 | 0.30 | <txp:category />
27.73 | 0.25 | [SQL: SELECT id, name, parent, title, description FROM typerite_txp_category as txp_category WHERE type = 'article' ]
27.99 | | [Rows: 4]
28.01 | | [/attribute]
28.17 | 0.74 | [SQL: SELECT COUNT(*) FROM typerite_textpattern as textpattern WHERE 1 AND `Posted` <= from_unixtime(2147483647) AND (Expires IS NULL OR from_unixtime(2147483647) <= Expires) AND ((Category1 IN ('installation')) OR (Category2 IN ('installation'))) AND Section IN('default','articles','about','contact','typerite','smd_tag') AND Status IN (4) ]
28.91 | | [Rows: 1]
28.94 | | </txp:variable>
28.95 | 0.09 | <txp:if_variable name="articles_exist" not value="0">
28.98 | | [true]
28.99 | 0.04 | <txp:category link title />
29.04 | | </txp:if_variable>
29.04 | | [true]
29.05 | 0.65 | <txp:variable name="articles_exist">
29.06 | | [true]
29.06 | 0.63 | <txp:article_custom category='<txp:category />' pgonly pageby="1" />
29.07 | 0.02 | [attribute 'category']
29.07 | | [true]
29.07 | 0.02 | <txp:category />
29.09 | | [/attribute]
29.16 | 0.51 | [SQL: SELECT COUNT(*) FROM typerite_textpattern as textpattern WHERE 1 AND `Posted` <= from_unixtime(2147483647) AND (Expires IS NULL OR from_unixtime(2147483647) <= Expires) AND ((Category1 IN ('post-types')) OR (Category2 IN ('post-types'))) AND Section IN('default','articles','about','contact','typerite','smd_tag') AND Status IN (4) ]
29.68 | | [Rows: 1]
29.69 | | </txp:variable>
29.70 | 0.05 | <txp:if_variable name="articles_exist" not value="0">
29.71 | | [true]
29.72 | 0.03 | <txp:category link title />
29.75 | | </txp:if_variable>
29.75 | | [true]
29.76 | 0.65 | <txp:variable name="articles_exist">
29.77 | | [true]
29.77 | 0.64 | <txp:article_custom category='<txp:category />' pgonly pageby="1" />
29.77 | 0.02 | [attribute 'category']
29.77 | | [true]
29.78 | 0.02 | <txp:category />
29.80 | | [/attribute]
29.86 | 0.53 | [SQL: SELECT COUNT(*) FROM typerite_textpattern as textpattern WHERE 1 AND `Posted` <= from_unixtime(2147483647) AND (Expires IS NULL OR from_unixtime(2147483647) <= Expires) AND ((Category1 IN ('pre-installation')) OR (Category2 IN ('pre-installation'))) AND Section IN('default','articles','about','contact','typerite','smd_tag') AND Status IN (4) ]
30.39 | | [Rows: 1]
30.41 | | </txp:variable>
30.42 | 0.05 | <txp:if_variable name="articles_exist" not value="0">
30.43 | | [true]
30.44 | 0.03 | <txp:category link title />
30.47 | | </txp:if_variable>
30.50 | | </txp:category_list>
I have 3 categories so far – two of them are empty, the other one has 4 articles.
Offline
Re: Evaluating whether to show a Category or Section in a Navigation Bar
michaelkpate wrote #324197:
I just tried both and they both failed. I thought the second one might (0 is, after all, a value) but thought the first would succeed but it didn’t.
<txp:if_variable name="articles_exist" not value="0" />...
Could it be the self-closed variable?
<txp:if_variable name="articles_exist" not value="0" />
<txp:category link title />
</txp:if_variable>
Offline