Problem with mustache template

Problem with mustache template

by Kieran Briggs -
Number of replies: 6

Hi all, 


I've read through the documentation and mustache guide on their site and thought I had a handle on it but it appears not.  I'm trying to show a list of items from a a database table using a template.  I started by creating the template and then the data example to look at in the template viewer, but its not showing me the content and I dont know why.  The relevent bits are below.

<h4>{{section}}</h4>
<ul class="comments">
{{#messages}}
<li id="{{messageid}}" class="item">
<div class="userpix">{{userpix}}</div>
<div class="msg-body">
<div class="header">
<div class="username">{{username}}</div>
<div class="date">{{date}}</div>
</div>
<div class="message">{{message}}</div>
</div>
<div class="social"><a href=#">Like</a> | <a href=#">Delete</a></div>
</li>
{{/messages}}
</ul>
Example context (json):
{
"section": "Topic 5",
"messages": [
{"messageid": 12, "userpix": "Picture Here", "username": "Kieran Briggs", "date": "22 April", "message": "content of the message"},
{"messageid": 14, "userpix": "Picture Here", "username": "Tony Stark", "date": "2 April", "message": "I am Iron Man"}
]
}
Could someone tell me what i'm doing wrong?  I really want to learn templating but its proving harder than I thought
Average of ratings: -
In reply to Kieran Briggs

Re: Problem with mustache template

by Darko Miletić -
Did you purge the Moodle cache? Templates are cached just like language

strings.



El vie., 7 jun. 2019 11:26, Kieran Briggs (via Moodle.org) <
In reply to Kieran Briggs

Re: Problem with mustache template

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators

Hi Kieran,

how and where do you call this template? Maybe the data are not passed in the right way.
@Darko: Templates are not cached like lang strings. You don't need to clear the cache if you create new or change existing templates.

Best regards
Andreas

In reply to Andreas Grabs

Re:

by Kieran Briggs -
I've not got to the data yet, I'm building the template off the sample context JSON in the comments section. I can't get it to display the list items at all.
In reply to Kieran Briggs

Re:

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators
Hi Kieran,

it would be nice if you'd give us a bit more information.
What file do you use for your mustache and where is it located?
How looks the code in your mustache file? The code in your starting post can not be your template because it is syntactically wrong/incomplete.
Thanks!
Andreas
In reply to Andreas Grabs

Re:

by Kieran Briggs -
I think that is what i'm doing wrong then. I haven't created the code yet to give me the data. I'm using the Template gallery and comments in the mustache file to create the sample output, then when i've got it working, i'm going to create the function that pulls the data from the database.
In reply to Kieran Briggs

Re:

by Stephen Sharpe -

Hi Kieran

This is a very long overdue reply but it appears you have something missing from your template. The original error message:

This template has no example context, so it cannot be rendered here. To add an example context to this template, insert in a Mustache comment "Example context (json):", followed by the json encoded sample context for the template.

The above error message explains the need for "Example context (json):" but it doesn't indicate that you also need to enter both the name of the plugin and the name of the template. For example - "@template theme_boost/admin_setting_tabs" above the json context. So for example:

{{!
    @template theme_boost/admin_setting_tabs

    Example context (json):
    {
        "tabs": [
         {
            "name": "tab1",
            "active": 0,
            "displayname": "Inactive tab1",
            "html": "<p>Tab 1 content</p>"
         },
         {
            "name": "tab2",
            "active": 1,
            "displayname": "Active tab2",
            "html": "<p>Tab 2 content</p>"
         }
        ]
    }
}}

Hopefully this helps you or someone else struggling to get example content to appear for mustache templates.

Thanks!
Stephen