how to display an array of arrays in Moodle Mustache

how to display an array of arrays in Moodle Mustache

by Frederik Evans -
Number of replies: 11

I have a list of courses, some of them is a list of associated courses (nested array), I tried using the script {{#core}} {{name}} {{/core}} where core is the name of the array of courses. I tried also intercalating the {{#.}} and {{/.}} but it still only displays the courses on level 1, but does not show those in the nested arrays.

the array been passed is like this 

Array ( [0] => Array ( [courseCode] => UBSA-C700 [courseName] => Undergraduate Biochemistry [routes] => Array ( [0] => Array ( [routeCode] => C700 [routeName] => Biochemistry [type] => Core [block] => 3 [year] => ) ) ) [1] => Array ( [courseCode] => ULFA-C1A2 [courseName] => Undergraduate Biochemistry (MBio) [routes] => Array ( [0] => Array ( [routeCode] => C1A2 [routeName] => Biochemistry [type] => Core [block] => 3 [year] => ) ) ) [2] => Array ( [courseCode] => UBSA-C701 [courseName] => Undergraduate Biochemistry (with Intercalated Year) [routes] => Array ( [0] => Array ( [routeCode] => C701 [routeName] => Biochemistry with Intercalated Year [type] => Core [block] => 4 [year] => ) ) ) [3] => Array ( [courseCode] => ULFA-C1A6 [courseName] => Undergraduate Biochemistry with Industrial Placement (MBio) [routes] => Array ( [0] => Array ( [routeCode] => C1A6 [routeName] => Biochemistry with Industrial Placement [type] => Core [block] => 3 [year] => ) ) ) [4] => Array ( [courseCode] => UBSA-3 [courseName] => Undergraduate Biological Sciences [routes] => Array ( [0] => Array ( [routeCode] => C100 [routeName] => Biological Sciences [type] => Core [block] => 3 [year] => ) [1] => Array ( [routeCode] => C102 [routeName] => Biological Sciences with Cell Biology [type] => Core [block] => 3 [year] => ) [2] => Array ( [routeCode] => C103 [routeName] => Biological Sciences with Environmental Resources [type] => Core [block] => 3 [year] => ) [3] => Array ( [routeCode] => C104 [routeName] => Biological Sciences with Microbiology [type] => Core [block] => 3 [year] => ) [4] => Array ( [routeCode] => C105 [routeName] => Biological Sciences with Molecular Genetics [type] => Core [block] => 3 [year] => ) [5] => Array ( [routeCode] => C107 [routeName] => Biological Sciences with Virology [type] => Core [block] => 3 [year] => ) ) ) [5] => Array ( [courseCode] => ULFA-C1A1 [courseName] => Undergraduate Biological Sciences (MBio) [routes] => Array ( [0] => Array ( [routeCode] => C1A1 [routeName] => Biological Sciences [type] => Core [block] => 3 [year] => ) ) ) [6] => Array ( [courseCode] => UBSA-4 [courseName] => Undergraduate Biological Sciences (with Intercalated Year) [routes] => Array ( [0] => Array ( [routeCode] => C111 [routeName] => Biological Sciences with Cell Biology (with Intercalated Year) [type] => Core [block] => 4 [year] => ) [1] => Array ( [routeCode] => C101 [routeName] => Biological Sciences with Intercalated Year [type] => Core [block] => 4 [year] => ) [2] => Array ( [routeCode] => C109 [routeName] => Biological Sciences with Microbiology (with Intercalated Year) [type] => Core [block] => 4 [year] => ) [3] => Array ( [routeCode] => C108 [routeName] => Biological Sciences with Molecular Genetics ( with Intercalated Year) [type] => Core [block] => 4 [year] => ) [4] => Array ( [routeCode] => C110 [routeName] => Biological Sciences with Virology (with Intercalated Year) [type] => Core [block] => 4 [year] => ) [5] => Array ( [routeCode] => C521 [routeName] => Microbiology and Virology with Intercalated Year [type] => Core [block] => 4 [year] => ) [6] => Array ( [routeCode] => C523 [routeName] => Virology (with intercalated year) [type] => Core [block] => 4 [year] => ) ) ) [7] => Array ( [courseCode] => ULFA-C113 [courseName] => Undergraduate Biological Sciences (with Placement Year) [routes] => Array ( [0] => Array ( [routeCode] => C113 [routeName] => Biological Sciences (with Placement Year) [type] => Core [block] => 4 [year] => ) ) ) [8] => Array ( [courseCode] => ULFA-C1A5 [courseName] => Undergraduate Biological Sciences with Industrial Placement (MBio) [routes] => Array ( [0] => Array ( [routeCode] => C1A5 [routeName] => Biological Sciences with Industrial Placement [type] => Core [block] => 3 [year] => ) ) ) [9] => Array ( [courseCode] => UBSA-C1B9 [courseName] => Undergraduate Biomedical Science [routes] => Array ( [0] => Array ( [routeCode] => C1B9 [routeName] => Biomedical Science [type] => Core [block] => 3 [year] => ) ) ) [10] => Array ( [courseCode] => ULFA-C1A3 [courseName] => Undergraduate Biomedical Science (MBio) [routes] => Array ( [0] => Array ( [routeCode] => C1A3 [routeName] => Biomedical Science [type] => Core [block] => 3 [year] => ) ) ) [11] => Array ( [courseCode] => ULFA-C1A7 [courseName] => Undergraduate Biomedical Science with Industrial Placement (MBio) [routes] => Array ( [0] => Array ( [routeCode] => C1A7 [routeName] => Biomedical Science with Industrial Placement [type] => Core [block] => 3 [year] => ) ) ) [12] => Array ( [courseCode] => UBSA-CB19 [courseName] => Undergraduate Biomedical Science with Intercalated Year [routes] => Array ( [0] => Array ( [routeCode] => CB19 [routeName] => Biomedical Science with Intercalated Year [type] => Core [block] => 4 [year] => ) ) ) )

i also tried flattening the array, but that does not work either

the mustache section looks like this

{{#Core}}

 <p class="paragraph-list bulletlist">  {{#.}} {{courseCode}} {{/.}} {{ courseName }}   </p>                 

  {{/Core}}

Average of ratings: -
In reply to Frederik Evans

Re: how to display an array of arrays in Moodle Mustache

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi Frederick,
It's not easy to interpret the array structure when its posted like that, try editing your post so its formatted using the "pre-formatted" style with line breaks and indentation, and it should make it easier for us to provide suggestions.
In reply to Mark Johnson

Re: how to display an array of arrays in Moodle Mustache

by Frederik Evans -
Here is an example, I have an array like the following :
[0] => Array ( [courseCode] => UBSA-3 [courseName] => Undergraduate Biological Sciences [routes]
this array, in turn contains anotyer array, :
Array ( [0] => Array ( [routeCode] => C100 [routeName] => Biological Sciences [type] => Core [block] => 3 [year] => ) [1] => Array ( [routeCode] => C102 [routeName] => Biological Sciences with Cell Biology [type] => Core [block] => 3 [year] => ) [2] => Array ( [routeCode] => C103 [routeName] => Biological Sciences with Environmental Resources [type] => Core [block] => 3 [year] => ) [3] => Array ( [routeCode] => C104 [routeName] => Biological Sciences with Microbiology [type] => Core [block] => 3 [year] => ) [4] => Array ( [routeCode] => C105 [routeName] => Biological Sciences with Molecular Genetics [type] => Core [block] => 3 [year] => )
I tried flattening the array into one, but that did not work, I am serializing the array of arrays, and then in my renderer.php i unserialize it, however, when displaying it, am only getting the upper level in other words [0] => Array ( [courseCode] => UBSA-3 [courseName] => Undergraduate Biological Sciences [routes] but not the sub array. I was hoping to be able to see the sub array too
In reply to Frederik Evans

Re: how to display an array of arrays in Moodle Mustache

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
You can't use named key arrays in mustache, they have to be numbered indexes. Use objects for key names.

[0] => stdClass ( 'courseCode' => UBSA-3, 'courseName' => Undergraduate Biological Sciences, 'routes' => Array ( [0] => 'one', [1] => 'two',  [2] => 'three'))
In reply to Mark Sharp

Re: how to display an array of arrays in Moodle Mustache

by Frederik Evans -
i was able to get something to work, however, one nagging thing, I can't seem to get rid on duplicates. for example in the first array, there is a single sub array,

in this case, the top level of the array shows course code as UBSA-C700 and the nested level is routeCode => C700, it would be nice if i could filter that value out
Array ( [0] => Array ( [courseCode] => UBSA-C700 [courseName] => Undergraduate Biochemistry [routes] => Array ( [0] => Array ( [routeCode] => C700 [routeName] => Biochemistry [type] => Core [block] => 3 [year] => ) ) )

i.e. combine the core of the nested array into the header of the array.
i guess id need to compare the second bit of the coursecode with that of the routecode. if they are identical, and the array id is 0 we remove it. 
so we get something like this
Year 3 of UBSA-C700 Undergraduate Biochemistry

as opposed to this
UBSA-C700 : Undergraduate Biochemistry
C700 - Biochemistry
In reply to Frederik Evans

Re: how to display an array of arrays in Moodle Mustache

by Frederik Evans -
if there's more than 1 subarray, then we display something like this...
UBSA-3 Undergraduate Biological Sciences
Year 3 of C100 Biological Sciences
Year 3 of C102 Biological Sciences with Cell Biology
Year 3 of C103 Biological Sciences with Environmental Resources
Year 3 of C104 Biological Sciences with Microbiology
Year 3 of C105 Biological Sciences with Molecular Genetics
Year 3 of C107 Biological Sciences with Virology
In reply to Frederik Evans

Re: how to display an array of arrays in Moodle Mustache

by Richard Jones -
Picture of Plugin developers Picture of Testers
Probably best to do this in the code that is preparing the data for the template not within Mustache, if I understand your question. You could pass a boolean to Mustache and use it to flag alternate display code.

{{#aboolean}}
  some html
{{/aboolean}}
{{^aboolean}}
 some other html
{{/aboolean}}

I don't know if it's considered the elegant thing to do but I do know it works like an if else.

In reply to Richard Jones

Re: how to display an array of arrays in Moodle Mustache

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

They key point here is that mustache is logic-less, it's not meant for you to pass a load of things you might want to display, then the template works out whether to display it or not. The renderable should perform any logic to construct the data to be displayed, then pass just that to the template.

In reply to Mark Johnson

Re: how to display an array of arrays in Moodle Mustache

by Richard Jones -
Picture of Plugin developers Picture of Testers

Hi Mark

This is getting a little off the thread but I'm curious about when to use {{$boolean}} ((^boolean}} in some cases where, for example, you want to display a set of buttons to one user role and a different set to another role.

Would you then have different templates (or sub-templates) called with different data generated by the renderable rather than using the method I outlined?  What is the key advantage?

NOTE: I'm not currently doing that, I'm using individual booleans to decide whether to show each button rather than using {{^boolean).

Eg

{{#edit}} html to show edit button {{/edit}}

Then edit is set to true in the renderer for one role and not the other.

Although the doco talks about logic-less I haven't seen any guidance on what the overall outcomes are in terms of code design, efficiency, maintenance, re-usability and so on.

Perhaps this belongs in a different thread.

Cheers

Richard

In reply to Richard Jones

Re: how to display an array of arrays in Moodle Mustache

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Richard, Basically, I think you're right for the case you describe. In the mustache documentation, the intention of {{#sections} and {{^inverted-sections}} is to say, "if this key is set, display something" or "if this key is not set, display something".

What I'm warning against is the idea of using this syntax to try and construct more complex if/else statements, and combining different bits of output under different conditions, which is what I think was being suggested in this thread. One of the key reasons for using templates was to make it easy for theme developers to override the output without having to worry about the logic of how the data is constructed.

Average of ratings:Useful (1)
In reply to Mark Johnson

Re: how to display an array of arrays in Moodle Mustache

by Frederik Evans -
I don't think changing mustache file will do the job, What i would like to do, is combine the value of the subarray into that of the main array. in this case, we are now returning

the array looks like this....
Array ( [0] => Array ( [courseCode] => UBSA-C701 [courseName] => Undergraduate Biochemistry (with Intercalated Year) [routes] => Array ( [0] => Array ( [routeCode] => C701 [routeName] => Biochemistry with Intercalated Year [type] => Core [block] => 4 [year] => ) ) ) [1] => Array ( [courseCode] => UBSA-3 [courseName] => Undergraduate Biological Sciences [routes] => Array ( [0] => Array ( [routeCode] => C102 [routeName] => Biological Sciences with Cell Biology [type] => Core [block] => 3 [year] => ) [1] => Array ( [routeCode] => C103 [routeName] => Biological Sciences with Environmental Resources [type] => Core [block] => 3 [year] => ) [2] => Array ( [routeCode] => C104 [routeName] => Biological Sciences with Microbiology [type] => Core [block] => 3 [year] => ) [3] => Array ( [routeCode] => C105 [routeName] => Biological Sciences with Molecular Genetics [type] => Core [block] => 3 [year] => ) [4] => Array ( [routeCode] => C107 [routeName] => Biological Sciences with Virology [type] => Core [block] => 3 [year] => ) ) )

the first of the arrays : Array ( [0] => Array ( [courseCode] => UBSA-C701 [courseName] => Undergraduate Biochemistry (with Intercalated Year) [routes] => Array ( [0] => Array ( [routeCode] => C701 [routeName] => Biochemistry with Intercalated Year [type] => Core [block] => 4 [year] => ) ) )

contains 1 sub-array called routes
the second array, however contains 5 subarrays,
[1] => Array ( [courseCode] => UBSA-3 [courseName] => Undergraduate Biological Sciences [routes] => Array ( [0] => Array ( [routeCode] => C102 [routeName] => Biological Sciences with Cell Biology [type] => Core [block] => 3 [year] => ) [1] => Array ( [routeCode] => C103 [routeName] => Biological Sciences with Environmental Resources [type] => Core [block] => 3 [year] => ) [2] => Array ( [routeCode] => C104 [routeName] => Biological Sciences with Microbiology [type] => Core [block] => 3 [year] => ) [3] => Array ( [routeCode] => C105 [routeName] => Biological Sciences with Molecular Genetics [type] => Core [block] => 3 [year] => ) [4] => Array ( [routeCode] => C107 [routeName] => Biological Sciences with Virology [type] => Core [block] => 3 [year] => ) )

will therefore display:

UBSA-C701 : Undergraduate Biochemistry (with Intercalated Year)
Year 4 C701 Biochemistry with Intercalated Year
UBSA-3 : Undergraduate Biological Sciences
Year 3 C102 Biological Sciences with Cell Biology
Year 3 C103 Biological Sciences with Environmental Resources
Year 3 C104 Biological Sciences with Microbiology
Year 3 C105 Biological Sciences with Molecular Genetics
Year 3 C107 Biological Sciences with Virology

what we want to show is this...
UBSA-C701 : Undergraduate Biochemistry (with Intercalated Year)
UBSA-3 : Undergraduate Biological Sciences
Year 3 C102 Biological Sciences with Cell Biology
Year 3 C103 Biological Sciences with Environmental Resources
Year 3 C104 Biological Sciences with Microbiology
Year 3 C105 Biological Sciences with Molecular Genetics
Year 3 C107 Biological Sciences with Virology

so in other words, if the array has 1 and ONLY 1 sub array, then we combine, if not, leave intact

I've tried several ways, but to no success.
In reply to Frederik Evans

Re: how to display an array of arrays in Moodle Mustache

by Richard Jones -
Picture of Plugin developers Picture of Testers
Looks like you need to nest the loops (or use a sub-template for the inner items). Something like:

{{#array1}}
{{courseCode}} {{#array2}} {{courseName}} {{/array2}} {{/array1}}

Clearly some kind of html table is ideal to display this data. There is an example in this branch where reports.mustache includes pagerow.mustache:

https://github.com/richardjonesnz/moodle-mod_collaborate/blob/week8_final/templates/.

The reports data is generated by reports.php and classes/output/reports.php which causes Moodle to look for the template of the same name.

HTH
Average of ratings:Useful (1)