Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active June 11, 2024 17:57
Show Gist options
  • Save stevewithington/6214658 to your computer and use it in GitHub Desktop.
Save stevewithington/6214658 to your computer and use it in GitHub Desktop.
Mura CMS : Display nested categories with links (only if the category has been set to allow content assignments)
<cfscript>
/**
* @parentID Category ParentID
*/
public any function dspNestedCategories(
string siteid='#variables.$.event('siteid')#'
, string parentID=''
, string keywords=''
, boolean activeOnly=false
, boolean InterestsOnly=false
, boolean showCategoryKids=true
) {
var local = {};
local.str = '';
local.gateway = variables.$.getBean('categoryGateway');
local.rs = local.gateway.getCategories(argumentCollection=arguments);
local.it = variables.$.getBean('categoryIterator').setQuery(local.rs);
if ( local.it.hasNext() ) {
savecontent variable='local.str' {
WriteOutput('<ul>');
While ( local.it.hasNext() ) {
local.item = local.it.next();
// If allows content assignments, then create a link
if ( local.item.getIsOpen() ) {
local.itemCategoryURL = variables.$.createHREF(
filename = variables.$.event('currentFilenameAdjusted')
& '/category/'
& local.item.getFilename()
);
WriteOutput('<li>
<a href="#local.itemCategoryURL#">
#HTMLEditFormat(local.item.getName())#
</a>
');
} else {
WriteOutput('<li>#HTMLEditFormat(local.item.getName())#');
}
// Show Child Categories?
if ( arguments.showCategoryKids ) {
local.rsKids = local.gateway.getCategories(
siteID=local.item.getSiteID()
, parentID=local.item.getCategoryID()
);
if ( local.rsKids.recordcount ) {
local.itKids = variables.$.getBean('categoryIterator').setQuery(local.rsKids);
While ( local.itKids.hasNext() ) {
local.kid = local.itKids.next();
WriteOutput('<ul>
<li>');
// If allows content assignments, then create a link
if ( local.kid.getIsOpen() ) {
local.kidCategoryURL = variables.$.createHREF(
filename = variables.$.event('currentFilenameAdjusted')
& '/category/'
& local.kid.getFilename()
);
WriteOutput('
<a href="#local.kidCategoryURL#">
#HTMLEditFormat(local.kid.getName())#
</a>
');
} else {
WriteOutput('#HTMLEditFormat(local.kid.getName())#');
}
WriteOutput('#dspNestedCategories(parentID=local.kid.getCategoryID())#
</li>
</ul>');
}
}
} // @end showCategoryKids
WriteOutput('</li>');
}
WriteOutput('</ul>');
}
} // @end local.it.hasNext()
return local.str;
}
</cfscript>
@bradmarshall
Copy link

Excellent function! I modified the href attributes of the anchor elements to suit the URL structure of my project, which looks like /mynewsfeed/?categoryid=[categoryid]. These two lines got me there:

local.itemCategoryURL = variables.$.event('scriptName') & "?categoryid=" & local.item.getCategoryId();
local.kidCategoryURL = variables.$.event('scriptName') & "?categoryid=" & local.kid.getCategoryId();

... though I do prefer your URL structure and it's making me re-think my URLs!

This function (or something simliar) should be included in Mura core!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment