General developer forum

Moodle url's and mustache templates

 
Picture of Richard Jones
Moodle url's and mustache templates
Particularly helpful MoodlersPlugin developersTesters

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: -
Picture of Richard Jones
Re: Moodle url's and mustache templates - SOLVED
Particularly helpful MoodlersPlugin developersTesters

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


 
Average of ratings: -
Picture of Darko Miletić
Re: Moodle url's and mustache templates - SOLVED
Core developersParticularly helpful Moodlers

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)
Picture of Richard Jones
Re: Moodle url's and mustache templates - SOLVED
Particularly helpful MoodlersPlugin developersTesters

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

 
Average of ratings: -
Picture of Darko Miletić
Re: Moodle url's and mustache templates - SOLVED
Core developersParticularly helpful Moodlers

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

 
Average of ratings: -
Picture of Richard Jones
Re: Moodle url's and mustache templates - SOLVED
Particularly helpful MoodlersPlugin developersTesters

Interesting.

One of us should file a bug report for this page:

https://docs.moodle.org/dev/Coding_style

wink


 
Average of ratings: -