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

Debug option to automatic insert HTML comments with the template name into the output

XMLWordPrintable

    • MOODLE_402_STABLE
    • MOODLE_402_STABLE
    • MDL-77182-master
    • Hide
      1. Login as admin
      2. Ensure you have the following settings:
        1. Theme designer mode (themedesignermode) -> disabled
        2. Cache Javascript (cachejs) -> enabled
        3. Cache templates (cachetemplates) -> enabled
      3. Go to site administration -> Development -> Debugging
      4. Inspect the page with the browser and keep the inspector opened (usually F12 or right-click on the page and click on "inspect")
      5. Enable the "Include template information in the HTML" setting (debugtemplateinfo)
      6. Save changes
      7. Check in the inspector the page HTML has many comments like "<!-- template(PHP): ..."
      8. Disable the "Include template information in the HTML" setting (debugtemplateinfo)
      9. Save the changes
      10. Check that now, in the inspector, the page HTML does not have comments like "<!-- template(PHP): ..."
      Show
      Login as admin Ensure you have the following settings: Theme designer mode (themedesignermode) -> disabled Cache Javascript (cachejs) -> enabled Cache templates (cachetemplates) -> enabled Go to site administration -> Development -> Debugging Inspect the page with the browser and keep the inspector opened (usually F12 or right-click on the page and click on "inspect") Enable the "Include template information in the HTML" setting (debugtemplateinfo) Save changes Check in the inspector the page HTML has many comments like "<!-- template(PHP): ..." Disable the "Include template information in the HTML" setting (debugtemplateinfo) Save the changes Check that now, in the inspector, the page HTML does not have comments like "<!-- template(PHP): ..."
    • 1
    • HQ 2023 Sprint i1.1 Moppies

      There is a particular developer setting called "debugstringids" which forces the injection of the string ID to all string.  However, there is no easy way to identify which template is responsible for a specific page part.

      The idea is to implement a new developer setting called "debugtemplatenames" which adds HTML comments when a mustache file is rendered.

      For example, imagine the "core/mytemplate" template:

       

      <p>This is an example</p>
      

      With the setting the resulting HTML could be:

       

      1) If it is rendered using PHP:

       

      <!-- template(PHP): core/mytemplate --><p>This is an example</p><!-- /template(PHP): core/mytemplate -->

      2) If it is rendered using JS:

       

      <!-- template(JS): core/mytemplate --><p>This is an example</p><!-- /template(JS): core/mytemplate --> 

      Differentiating between PHP and JS is entirely optional, but because the loading workflow is different, it could be easy to do.

      Of course, adding those comments can break some frontend JS, but as a tool to identify templates will be helpful.

      Technical shaping

      Template rendering is quite complex. However, I identified two main files involved in the template content loading :

      • lib/classes/output/mustache_template_source_loader.php used form the PHP rendering
      • lib/classes/output/mustache_filesystem_loader.php used form js load template webservice

      In the mustache_template_source_loader it is possible to path the load method to add the information.

      public function load(
          string $component,
          string $name,
          string $themename,
          bool $includecomments = false
      ) : string {
          global $CFG;
          // Get the template source from the callback.
          $source = ($this->gettemplatesource)($component, $name, $themename);   
          if (!$includecomments) {
              $source = $this->strip_template_comments($source);
          }
          if ($CFG->debugtemplatenames) {
             result = "<!-- /template(JS): $name -->" . $result . "<!-- /template(JS): $name -->"
          }
          return $source";
          } 

      In the mustache_filesystem_loader is possible to override the original load function with something like:

      public function load($name) {
          global $CFG;
          $result = parent::load($name);
          if ($CFG->debugtemplatenames) {
              result = "<!-- /template(PHP): $name -->" . $result . "<!-- /template(PHP): $name -->"
          }
          return $result;
      } 

       

            tusefomal Ferran Recio
            tusefomal Ferran Recio
            Carlos Escobedo Carlos Escobedo
            Ilya Tregubov Ilya Tregubov
            Kim Jared Lucas Kim Jared Lucas
            Votes:
            2 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved:

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

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.