OutputtingHTML - mudrd8mz/moodle-dev-chronicles GitHub Wiki

"Web programming is the science of coming up with increasingly complicated ways of concatenating strings." -- Greg Brockman

Essentials

At the lowest code level, Moodle uses echo to output a text string. Today it is a common syntax to use this language construct as if it was a function - with parentheses - and with a single parameter.

echo('<h1>It works!</h1>');

We do not use print in Moodle.

Not every content can be sent directly to the browser via echo(), mostly for security reasons. Moodle core libraries provide some output formatting helpers. Rough guideline for when to use which helper follows:

                         +--------------+
                      No |  Should it   | Yes
                +--------| support HTML |-------+
                |        +--------------+       |
                |                               |
                v                               v
       +-------------------+             +-----------------+
       | Should it support |             | Does it contain |
    No | multilanguage and | Yes         |  user content?  |
   +---|  other filters?   |---+         +-----------------+
   |   +-------------------+   |        No |             | Yes
   |                           |           |             |
   v                           v           v             v
+-----+          +-----------------+   +--------+      +-------------+
| s() |          | format_string() |   | echo() |      |  Should it  |
+-----+          +-----------------+   +--------+      | support JS? |
HTML tags        Activity titles,      Help strings    +-------------+
attributes       forum post subjects,                    |         |
                 book chapter titles                  No |     Yes |
                                                         |         |
                                                         v         v
                                             +---------------+  +---------------+
                                             | format_text() |  | format_text() |
                                             | noclean=false |  | noclean=true  |
                                             +---------------+  +---------------+
                                             Forum post         Page recourse
                                             content            content, activity
                                                                descriptions
⚠️ **GitHub.com Fallback** ⚠️