{highlight}

Colours source code on the server - PAD, PHP, HTML, CSS, JavaScript, JSON, YAML, SQL or shell - with optional line numbers and marked lines.

Text and formatting Either form

Syntax

{highlight 'lang'} ... {/highlight}
{highlight 'lang', lines, mark='3, 5-7'} ... {/highlight}
{highlight file='_data/products.json'}

Parameters

lang
The language: pad, php, html (xml, svg), css, js (ts, json5), json, yaml, sql, bash (sh) or text. With file= it defaults to the file's extension, otherwise to text.

Options

file
A file of the application to show instead of the content - never one outside it, under _config/ or a dotfile.
lines
Bare option: number the lines.
mark
Lines to set apart: '4', '3, 5-7'.

Description

{highlight 'php'} ... {/highlight} writes its content as coloured source code: every piece HTML-escaped and wrapped in a <span class="hl-..."> - comments, strings, numbers, keywords, variables, tags, attributes, functions - inside <pre class="pad-highlight" data-lang="php"><code>. It is done on the server by lib/highlight.php; no JavaScript library is loaded.

The content is taken as it stands, before the engine walks it: the braces of PHP, CSS, JSON or of PAD itself need no {ignore}, and a field inside is shown rather than resolved. The indent the lines share is taken off, so the sample can be indented with the template. One exception: a {# comment #} is gone before any tag sees it, so a PAD sample keeps none.

file= colours a file of the application instead of the content - its language from the extension unless the first parameter says otherwise. It never reads a file outside the application, under _config/ or a dotfile; such a name, or one that is not there, is a strict error, as is a language that does not exist. lines numbers the lines and mark sets some apart.

The default colours are written once a page under :where(), following the page's color-scheme, so the page's own CSS wins. The highlight pipe does the same for a value - {$query | highlight('sql')} - and {source} shows the files behind a page as tabs, each coloured this way.

Examples

PHP with numbered lines

lines numbers the lines and mark sets line 4 apart; the braces need no ignore.

Templateexamples/highlight/1-php.pad
{highlight 'php', lines, mark='4'}
  <?php
    function total ( $rows ) {
      $sum = 0;
      foreach ( $rows as $row ) $sum += $row ['price'];
      return $sum;
    }
  ?>
{/highlight}
Resultopen alone
<?php
  function total ( $rows ) {
    $sum = 0;
    foreach ( $rows as $row ) $sum += $row ['price'];
    return $sum;
  }
?>

A PAD sample

PAD itself is shown as written: the tags and fields inside are coloured, not run.

Templateexamples/highlight/2-pad.pad
{highlight 'pad'}
  {orders sort='date', first=3}
    <li>{$number}: {$total | money}</li>
  {/orders}
{/highlight}
Resultopen alone
{orders sort='date', first=3}
  <li>{$number}: {$total | money}</li>
{/orders}

A file of the application

file= shows the translation catalog of this application, its language from the extension.

Templateexamples/highlight/3-file.pad
{highlight file='_lang/en.json'}
Resultopen alone
{
  "trans.cart.title": "Your <em>shopping</em> cart",
  "trans.cart.checkout": "Go to checkout",
  "trans.cart.items": "Your cart is empty|One item in your cart|%d items in your cart",
  "trans.greeting": "Hello :name, welcome back to :place."
}

See also

  • {source} Shows the files behind a page as tabs, each coloured as highlight colours it - the code shown is the code that runs.
  • {markdown} Renders its content, or the value of its parameter, as Markdown and writes the HTML.
  • {ignore} Keeps PAD from reading the braces in its content as tags - for inline JavaScript, CSS, JSON and code samples.