Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-73332

Fix template shortentext helper - length as variable

    XMLWordPrintable

Details

    Description

      In the documentation on templates, the section on the shortentext helper states:

      "The helper takes two comma separated arguments. The first is the desired length and the second is the text to be shortened. Both can be provided as context variables." (emphasis mine)

      However, I was trying to provide the length argument as a context variable, and it wasn't working. After looking at lib/classes/output/mustache_shorten_text_helper.php, it was obvious that second argument, text, was looking for context variables but the first, length,  was not. 

       // Split the text into an array of variables.
       list($length, $text) = explode(',', $args, 2);
       $length = trim($length);
       $text = trim($text);
       
       // Allow mustache tags in the text.
       $text = $helper->render($text);
       
       return shorten_text($text, $length)

      Again, emphasis mine.

      After some testing, adding

      $length = $helper->render($length);

      does, indeed, seems to fix the problem and align the code with the documentation.

      Obviously this isn't a well-exercised issue - or it would have been found by now, so I'm including a patch for the current main branch.

       

       

       

      Attachments

        1. MDL-73332_master_step 3.png
          MDL-73332_master_step 3.png
          175 kB
        2. MDL-73332_master_step 7.png
          MDL-73332_master_step 7.png
          55 kB
        3. MDL-73332_v311_step 3.png
          MDL-73332_v311_step 3.png
          188 kB
        4. MDL-73332_v311_step 7.png
          MDL-73332_v311_step 7.png
          63 kB
        5. MDL-73332_v400_step 3.png
          MDL-73332_v400_step 3.png
          163 kB
        6. MDL-73332_v400_step 7.png
          MDL-73332_v400_step 7.png
          47 kB

        Activity

          People

            martygilbert Marty Gilbert
            martygilbert Marty Gilbert
            Sujith Haridasan Sujith Haridasan
            Victor Déniz Falcón Victor Déniz Falcón
            Angelia Dela Cruz Angelia Dela Cruz
            Adrian Greeve, David Woloszyn, Huong Nguyen, Jake Dallimore, Meirza, Michael Hawkins, Raquel Ortega, Safat Shahin, Stevani Andolo, David Woloszyn, Huong Nguyen, Jake Dallimore, Meirza, Michael Hawkins, Raquel Ortega, Safat Shahin, Stevani Andolo, Adrian Greeve, David Woloszyn, Huong Nguyen, Jake Dallimore, Meirza, Michael Hawkins, Raquel Ortega, Safat Shahin, Stevani Andolo
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              22/Aug/22

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 2 hours, 10 minutes
                2h 10m