Moodle url's and mustache templates

Moodle url's and mustache templates

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

I have created a data object with linkdata and linktext arrays (text is just a string array with 4 items):

$links = array();
$texts = array();
foreach ($text as $t) {
    $link = new moodle_url('/blocks/superiframe/view.php',
        array('blockid' => $blockid, 'size' => $t));
    $links[] = $link->out(false);
    $texts[] = $t;
}

So I get arrays which I add to the data object I'm sending to the template:

$data->linkdata = $links;
$data->linktext = $texts;

array (size=4)
  0 =>  'http://localhost/moodle/blocks/superiframe/view.php?blockid=27&size=custom' (length=74)
  1 =>  'http://localhost/moodle/blocks/superiframe/view.php?blockid=27&size=small' (length=73)
  2 =>  'http://localhost/moodle/blocks/superiframe/view.php?blockid=27&size=medium' (length=74)
  3 =>  'http://localhost/moodle/blocks/superiframe/view.php?blockid=27&size=large' (length=73)
Lovely.  linktext is outputting the link text here using the implicit iterator. sfsg.
<div class = "{{class}}">
  <div  class="container">
    <h3>{{name}}</h3>
      <ul class="list-inline">
        {{#linktext}}
          <li class="list-inline-item">
           {{#.}}
             <a href="{{{link}}}">{{.}}</a>
           {{/.}}
          </li>
        {{/linktext}}
      </ul>
  </div>
</div>

However the links on the page have the block id but not the additional size parameter.

What have I missed?

Full code: https://github.com/richardjonesnz/moodle-block_superiframe/tree/week8_challenge

Thanks if you can help.

Average of ratings: -
In reply to Richard Jones

Re: Moodle url's and mustache templates - SOLVED

by Richard Jones -
Picture of Plugin developers Picture of Testers

Not a mustache issue at all (how often we look in the wrong place).

Data structure (yes I changed $text to $strings):

foreach ($strings as $string) {
    $link = new moodle_url('/blocks/superiframe/view.php',
            array('blockid'=>$blockid, 'size'=>$string));
    $links[] = array('link' => $link->out(false), 'text' => $string);
}

Much simpler template:

<div class = "{{class}}">
  <div  class="container">
    <h3>{{name}}</h3>
      <ul class="list-inline">
        {{#linkdata}}
          <li class="list-inline-item">
             <a href="{{{link}}}">{{text}}</a>
          </li>
        {{/linkdata}}
      </ul>
  </div>
</div>

A thing of beauty.  Just too late to delete the original post smile


In reply to Richard Jones

Re: Moodle url's and mustache templates - SOLVED

by Darko Miletić -

It can be even better:

$link = new moodle_url('/blocks/superiframe/view.php', ['blockid' => $blockid]);
foreach ($strings as $string) {
    $links[] = ['link' => $link->out(false, ['size' => $string]), 'text' => $string];
}

That way you do not create new object as it is repeated.



Average of ratings: Useful (2)
In reply to Darko Miletić

Re: Moodle url's and mustache templates - SOLVED

by Richard Jones -
Picture of Plugin developers Picture of Testers

Thanks Darko

I did not realize moodle_url->out() took other parameters, in fact I did not know it took any until I looked for the difference between out(false) and out(true).  So many hidden corners to know.

On a point of PHP, the use of square brackets does not avoid the array object being created does it?  It's simply shorter to type on my understanding.

Cheers

Richard

In reply to Richard Jones

Re: Moodle url's and mustache templates - SOLVED

by Darko Miletić -

Yes, it is the same, but array() syntax is considered legacy and should be avoided for new code.