Saturday 15 January 2011

umbraco - Making one list from two calls in Razor -



umbraco - Making one list from two calls in Razor -

i'm creating simple site in umbraco, using razor.

i have page, may include several content "modules", should output on page. page might have subpages – these should of course of study not output on page, neither should content on these pages. create easier editor, modules can grouped in optional "modules" folder under page. construction thus:

– page ––– content 1 ––– module folder ––– ––– content 2 ––– subpage ––– ––– content 3

the page "my page" should list "content 1" , "content 2", while ignoring "subpage" , decendants.

this done creating 2 lists (currentpage.children , currentpage.modules.children) , concatenating them, such:

// retrieve modules in subfolder "modules" ... var modulesinfolder = currentpage.modules.first().children.where("nodetypealias != \"page\""); // retrieve modules found straight on page ... (i.e. kid nodes except pages) var modulesonpage = currentpage.children.where("nodetypealias != \"page\""); // concatenate 2 lists, can loop through them var modulelist = modulesinfolder.concat(modulesonpage).tolist();

this works great – however, "modules" folder optional. when doesn't exist, nil output on page @ all. presumably because "modulesinfolder" can not declared. if switch two, tells me "modulesonpage not conain definition 'concat'".

i have tried few different things, such counting on number of elements, , defining 'modulelist' differently epending on result, no avail. tried using 1 call, , listing descendants() lists content subpages – unless can pass descendants() method?

how can solve in elegant way elegantly?

there 1 page type, , modules folder has own document type, there several different document types content modules.

i came next illustration perhaps point in direction. not clear if collection of objects wanted or not. illustration below take parent node passed view , write values of properties page. tried few examples things ignore , cases may have different logic document type. hope gives guidance on question.

@using umbraco.nodefactory @model node @{ // sake of example, can state model page node have } @foreach (var item in model.getchildnodes()) { // lets go through items mentioned @rendersection(item) } @helper rendersection(node node) { if (node.nodetypealias != "page") { // pages don't rendered return; } if (node.nodetypealias == "module") { @html.raw(node.getproperty("titleofmodule")) } // output content @html.raw(node.getproperty("bodycontent")) // if phone call render section 1 time again on kid nodes can nest construction things if needed. not sure //– page //––– content 1 //––– module folder //––– ––– content 2 //––– ––– ––– module folder //––– ––– ––– ––– content 3 //––– subpage //––– ––– content 3 foreach (var subitem in node.getchildnodes()) { @rendersection(subitem) } }

razor umbraco umbraco7

No comments:

Post a Comment