Output user profile picture using Mustache helper

Output user profile picture using Mustache helper

by Richard Jones -
Number of replies: 2
Picture of Plugin developers Picture of Testers

Hi

Is something like this possible:

{{#user}}
        {{# pix }} {{.}}, core, user image {{/ pix }}
 {{/user}}

Where the user array is a list of user id's?

Thanks

Richard

Update: I did find the userpix folder in Moodle root which can probably help me achieve this another way but still curious.

Average of ratings: -
In reply to Richard Jones

Re: Output user profile picture using Mustache helper

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi Richard,

I do not think so. But you could use a bit of code in PHP, like this, to store all the profilepic urls you need in the data you pass to the template...
This assumes you are running the PHP part from your renderer file

global $DB,$CFG;
require_once("$CFG->libdir/outputcomponents.php");

$users = $DB->get_records('user',array());
foreach($users as $user) {
$user_picture = new user_picture($user);
$user->picurl = $user_picture->get_url($this->output);
}
$data=[];
$data['users']=$users;
return $this->render_from_template('mod_mysupermod/userpics', $data);
And then the template would look like this:
{{#users}}
       <img src="{{{picurl}}}">
 {{/user}}
(I copied and pasted bits of code of mine to make that, but did not test it. It should get you close, even if it does not go)
In reply to Justin Hunt

Re: Output user profile picture using Mustache helper

by Richard Jones -
Picture of Plugin developers Picture of Testers

Hi Justin

Thanks for the response and I hope you're well old buddy.

I got something similar from the userpix/index.php example:

        foreach ($students as $student) {
            $studentlist = array();
            $studentlist['name'] = $student->firstname;
            $rs = $DB->get_record_select("user", "id = '$student->id'", null, user_picture::fields());
            $studentlist['pic'] = $this->output->user_picture($rs);
            $data->students[] = $studentlist;
        }

Where students is a list of course student users from an SQL function in the block code. 

Then in the template:

    {{#students}}
      <ul class="list-group">
        <li class="list-group-item list-group-item-warning">  {{{pic}}} {{name}}</li>
    </ul>
    {{/students}}

And the result looks like this:

the block

So there's life in the old block yet smile

Absolutely the pix template helper is only designed to take pix from the relevant pix folder (a moment's thought would have told me that).  I need to find out how to write a template helper but that's something for another day.

However, maybe this will help someone searching the forums for something similar.

Best wishes

Richard

Average of ratings: Useful (2)