tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/13312-creating-a-news-page-only-want-to-show-categories-with-postsCascade CMS: Discussion 2015-12-22T20:35:32Ztag:help-archives.hannonhill.com,2010-02-09:Comment/386836622015-12-14T14:06:35Z2015-12-14T14:06:35Zcreating a news page, only want to show categories with posts<div><p>Hi,</p>
<p>This should be possible using XPath to check if there's at least
one category present. When you have a moment, please attach a
sample of your Index Block so I can provide you with an
example.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/386836622015-12-15T02:33:38Z2015-12-15T02:34:40Zcreating a news page, only want to show categories with posts<div><p>I actually hard coded all the predefined categories/tags per
requester's request... BUT i'd still like to know it would be
done...</p>
<p>hard coded categories/tags</p>
<pre>
<code> <div class="row categoryfilters" id="sortControls">
<div class="col-md-12">
<!-- toggles for collapsible panels // -->
<a class="filter" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" data-filter=".collegecenter">College/Center</a> <a class="filter" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" data-filter=".students">Students</a> <a class="filter" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" data-filter=".faculty">Faculty</a> <a class="filter" data-toggle="collapse" data-parent="#accordion" href="#collapseFour" data-filter=".stcenturyeducation">21st Century Education</a> <a class="filter" data-toggle="collapse" data-parent="#accordion" href="#collapseFive" data-filter=".alumni">Alumni</a> <a class="filter" data-toggle="collapse" data-parent="#accordion" href="#collapseSix" data-filter=".donors">Donors</a>
<!-- // toggles for collapsible panels -->
<a class="clear pull right" id="filterClearButton" >Clear <span class="glyphicon glyphicon-refresh"></span></a> </div>
</div>
<div class="row detailfilters">
<!-- collapsible panels - contain subcategories// -->
<!-- collapsible one -->
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body"> <a class="filter btn btn-default" data-filter=".collegecenter">All</a> <a class="filter btn btn-default" data-filter=".collegeofallopathicmedicine">College of Allopathic Medicine</a> <a class="filter btn btn-default" data-filter=".collegeofartshumanitiesandsocialsciences">College of Arts, Humanities, and Social Sciences</a> <a class="filter btn btn-default" data-filter=".collegeofdentalmedicine">College of Dental Medicine</a> <a class="filter btn btn-default" data-filter=".collegeofengineeringandcomputing">College of Engineering and Computing</a> <a class="filter btn btn-default" data-filter=".collegeofhealthcaresciences">College of Health Care Sciences</a> <a class="filter btn btn-default" data-filter=".collegeofmedicalsciences">College of Medical Sciences</a> <a class="filter btn btn-default" data-filter=".collegeofnursing">College of Nursing</a> <a class="filter btn btn-default" data-filter=".collegeofoptometry">College of Optometry</a> <a class="filter btn btn-default" data-filter=".collegeofosteopathicmedicine">College of Osteopathic Medicine</a> <a class="filter btn btn-default" data-filter=".collegeofpharmacy">College of Pharmacy</a> <a class="filter btn btn-default" data-filter=".collegeofpsychology">College of Psychology</a> <a class="filter btn btn-default" data-filter=".farquharhonorscollege">Farquhar Honors College</a> <a class="filter btn btn-default" data-filter=".abrahamsfischlercollegeofeducation">Abraham S. Fischler College of Education</a> <a class="filter btn btn-default" data-filter=".hwaynehuizengacollegeofbusinessandentrepreneurship">H. Wayne Huizenga College of Business and Entrepreneurship</a> <a class="filter btn btn-default" data-filter=".halmoscollegeofnaturalsciencesandoceanography">Halmos College of Natural Sciences and Oceanography</a> <a class="filter btn btn-default" data-filter=".mailmansegalcenterforhumandevelopment">Mailman Segal Center for Human Development</a> <a class="filter btn btn-default" data-filter=".shepardbroadcollegeoflaw">Shepard Broad College of Law</a> <a class="filter btn btn-default" data-filter=".universityschool">University School</a> </div>
</div>
<!-- collapsible two -->
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body"> <a class="filter btn btn-default" data-filter=".students">All</a> <!--ADD TAGS HERE--> </div>
</div>
<!-- collapsible three -->
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body"> <a class="filter btn btn-default" data-filter=".faculty">All</a> <!--ADD TAGS HERE--> </div>
</div>
<!-- collapsible four -->
<div id="collapseFour" class="panel-collapse collapse">
<div class="panel-body"> <a class="filter btn btn-default" data-filter=".alumni">All</a> <!--ADD TAGS HERE--> </div>
</div>
<!-- collapsible five -->
<div id="collapseFive" class="panel-collapse collapse">
<div class="panel-body"> <a class="filter btn btn-default" data-filter=".alumni">All</a> <!--ADD TAGS HERE--> </div>
</div>
<!-- collapsible six -->
<div id="collapseSix" class="panel-collapse collapse">
<div class="panel-body"> <a class="filter btn btn-default" data-filter=".donors">All</a> <!--ADD TAGS HERE--> </div>
</div>
<!-- //collapsible panels - contain subcategories -->
</div>
<div class="row filteredby">
<div class="col-md-10"><span class="h2 text-darkblue"><strong>NEWS</strong></span> <span id="filteredBy"></span></div>
<div class="col-md-2 text-right">Sort By: <a class="sort" data-sort="age:asc"><span class="glyphicon glyphicon-sort-by-attributes"></span></a> <a class="sort" data-sort="age:desc"><span class=" glyphicon glyphicon-sort-by-attributes-alt"></span></a></div>
</div>
<div class="row filteredselections text-center"><span class="glyphicon glyphicon-triangle-bottom"></span></div>
</div>
</div>
<!-- accordion end -->
<!-- <div class="btn-group" id="filterDisplayType" role="group" aria-label="...">
<a id="setGrid" class="btn btn-default">grid</a>
<a id="setList" class="btn btn-default">list</a>
</div>-->
</div></code>
</pre>
<p>format:<br></p>
<pre>
<code><section id="filteredItems">
<div class="fail-message"><span>No items were found matching the current filters.</span></div>
#set ($news = $_XPathTool.selectNodes($contentRoot, "//system-page[not(contains(path, 'index'))]"))##
#if ($news.size() > 0)
#foreach($story in $news)##
#set ($storydate = $story.getChild("start-date").value)##
#set ($storylink = $story.getChild("path").value)##
#set ($fstory = $story.getChild("system-data-structure").getChild("story"))
#set ($title = $fstory.getChild("title").value)
#set ($content = $fstory.getChild("content").value)
#set ($image = $fstory.getChild("image").getChild("path").value)
#set ($headerimage = $fstory.getChild("headerimage").getChild("path").value)
#set ($categories = $_XPathTool.selectNodes($fstory, 'category/value'))
#set ($collegecentertags = $_XPathTool.selectNodes($fstory, 'college-center-tags/value'))
#set ($studentstags = $_XPathTool.selectNodes($fstory, 'students-tags/value'))
#set ($facultytags = $_XPathTool.selectNodes($fstory, 'faculty-tags/value'))
#set ($centurytags = $_XPathTool.selectNodes($fstory, 'century-tags/value'))
#set ($alumnitags = $_XPathTool.selectNodes($fstory, 'alumni-tags/value'))
#set ($donorstags = $_XPathTool.selectNodes($fstory, 'donors-tags/value'))
#set ($contact = $_XPathTool.selectSingleNode($fstory, "contact").value)
#set ($name = $_XPathTool.selectSingleNode($fstory, "name").value)
#set ($email = $_XPathTool.selectSingleNode($fstory, "email").value)
#set ($phone = $_XPathTool.selectSingleNode($fstory, "phone").value)
<div class="row mix #foreach($category in $categories)$category.value.replaceAll("[^\dA-Za-z ]","").replaceAll(" ","").toLowerCase() #end #if($collegecentertags.value !="")#foreach($collegecentertag in $collegecentertags)$collegecentertag.value.replaceAll("[^\dA-Za-z ]","").replaceAll(" ","").toLowerCase() #end#end #if($studentstags.value !="")#foreach($studentstag in $studentstags)$studentstag.value.replaceAll("[^\dA-Za-z ]","").replaceAll(" ","").toLowerCase() #end#end #if($facultytags.value !="")#foreach($facultytag in $facultytags)$facultytag.value.replaceAll("[^\dA-Za-z ]","").replaceAll(" ","").toLowerCase() #end#end #if($centurytags.value !="")#foreach($centurytag in $centurytags)$centurytag.value.replaceAll("[^\dA-Za-z ]","").replaceAll(" ","").toLowerCase() #end#end #if($alumnitags.value !="")#foreach($alumnitag in $alumnitags)$alumnitag.value.replaceAll("[^\dA-Za-z ]","").replaceAll(" ","").toLowerCase() #end#end #if($donorstags.value !="")#foreach($donorstag in $donorstags)$donorstag.value.replaceAll("[^\dA-Za-z ]","").replaceAll(" ","").toLowerCase() #end#end" data-age='${storydate}'>
<div class="col-sm-6">
<div class="inner"><img src="${image}" class="img-responsive" /></div>
</div>
<div class="col-sm-6">
<div class="inner exerpt">
<p class="title h3">${title}</p>
<div class="newsitemdetails">${_DisplayTool.truncate($content,650,"...",true)}
</div>
<p> <a href="http://newhome.nova.edu${storylink}.html#story" class="more"><br />
More <span>»</span></a></p>
</div>
</div>
</div>
#end
#end </section></code>
</pre>
<p>everything pulls/displays great, but if you can point out how to
only show categories with posts that'd be awesome! any other
recommendations are also welcomed.</p>
<p>thanks!</p></div>Nandotag:help-archives.hannonhill.com,2010-02-09:Comment/386836622015-12-15T12:48:58Z2015-12-15T12:48:58Zcreating a news page, only want to show categories with posts<div><p>Hi,</p>
<p>The best way that I know of would be to use XPath to obtain all
categories and loop over them to see if there are any pages that
have that category present; so it's sort of the opposite of the
Format you provided.</p>
<p>Here is an example setup that I have used in the past:</p>
<pre>
<code>#set ($categoryArray = [])
#set ($categories = $_XPathTool.selectNodes($contentRoot, "//system-page[not(contains(path, 'index'))]/system-data-structure/story/category/value"))
#if ($categories.size() > 0)
#foreach ($category in $categories)
$!categoryArray.add({
"name": $category.value,
"pages": $_XPathTool.selectNodes($contentRoot, //system-page[not(contains(path, 'index')) and system-data-structure/story/category/value[. = '$category.value']])
})
#end
## Sort and loop over the categories containing pages
#if ($categoryArray.size() > 0)
#foreach ($category in $_SortTool.sort($categoryArray, ["name:asc"]))
$category.get("name")
#end
#end
#end</code>
</pre>
<p>Note: if you don't need the <code>pages</code> key/value, you
would probably just use <code>selectSingleNode</code> and check to
see if the returned value is null using the Property Tool.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/386836622015-12-15T14:12:13Z2015-12-15T14:12:13Zcreating a news page, only want to show categories with posts<div><p>hmm.. I wanted to see what your code produced with my content so
I put it in an empty format indexing the stories directory and I
get the following error:</p>
<p>An error occurred while rendering asset preview: Lexical error,
Encountered: "/" (47), after : "" at <em>unset</em>[line 7, column
54]</p></div>Nandotag:help-archives.hannonhill.com,2010-02-09:Comment/386836622015-12-15T17:11:18Z2015-12-15T17:11:18Zcreating a news page, only want to show categories with posts<div><p>Whoops, typo on my end. When you have a moment, try adjusting
this line:</p>
<pre>
<code>"pages": $_XPathTool.selectNodes($contentRoot, //system-page[not(contains(path, 'index')) and system-data-structure/story/category/value[. = '$category.value']])</code>
</pre>
<p>To the following:</p>
<pre>
<code>"pages": $_XPathTool.selectNodes($contentRoot, "//system-page[not(contains(path, 'index')) and system-data-structure/story/category/value[. = '${category.value}']]")</code>
</pre>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffith