CINXE.COM
template package - html/template - Go Packages
<!DOCTYPE html> <html lang="en" data-layout="responsive" data-local=""> <head> <script> window.addEventListener('error', window.__err=function f(e){f.p=f.p||[];f.p.push(e)}); </script> <script> (function() { const theme = document.cookie.match(/prefers-color-scheme=(light|dark|auto)/)?.[1] if (theme) { document.querySelector('html').setAttribute('data-theme', theme); } }()) </script> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="Description" content="Package template (html/template) implements data-driven templates for generating HTML output safe against code injection."> <meta class="js-gtmID" data-gtmid="GTM-W8MVQXG"> <link rel="shortcut icon" href="/static/shared/icon/favicon.ico"> <link rel="canonical" href="https://pkg.go.dev/html/template"> <link href="/static/frontend/frontend.min.css?version=prod-frontend-00095-8lw" rel="stylesheet"> <link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Go Packages"> <title>template package - html/template - Go Packages</title> <link href="/static/frontend/unit/unit.min.css?version=prod-frontend-00095-8lw" rel="stylesheet"> <link href="/static/frontend/unit/main/main.min.css?version=prod-frontend-00095-8lw" rel="stylesheet"> </head> <body> <script> function loadScript(src, mod = true) { let s = document.createElement('script'); s.src = src; if (mod) { s.type = 'module'; s.async = true; s.defer = true } document.head.appendChild(s); } loadScript("/third_party/dialog-polyfill/dialog-polyfill.js", false) loadScript("/static/frontend/frontend.js"); </script> <header class="go-Header go-Header--full js-siteHeader"> <div class="go-Header-inner go-Header-inner--dark"> <nav class="go-Header-nav"> <a href="https://go.dev/" class="js-headerLogo" data-gtmc="nav link" data-test-id="go-header-logo-link" role="heading" aria-level="1"> <img class="go-Header-logo" src="/static/shared/logo/go-white.svg" alt="Go"> </a> <div class="skip-navigation-wrapper"> <a class="skip-to-content-link" aria-label="Skip to main content" href="#main-content"> Skip to Main Content </a> </div> <div class="go-Header-rightContent"> <div class="go-SearchForm js-searchForm"> <form class="go-InputGroup go-ShortcutKey go-SearchForm-form" action="/search" data-shortcut="/" data-shortcut-alt="search" data-gtmc="search form" aria-label="Search for a package" role="search" > <input name="q" class="go-Input js-searchFocus" aria-label="Search for a package" type="search" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" placeholder="Search packages or symbols" value="" /> <input name="m" value="" hidden> <button class="go-Button go-Button--inverted" aria-label="Submit search"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/search_gm_grey_24dp.svg" alt="" /> </button> </form> <button class="go-SearchForm-expandSearch js-expandSearch" data-gtmc="nav button" aria-label="Open search" data-test-id="expand-search"> <img class="go-Icon go-Icon--inverted" height="24" width="24" src="/static/shared/icon/search_gm_grey_24dp.svg" alt=""> </button> </div> <ul class="go-Header-menu"> <li class="go-Header-menuItem"> <a class="js-desktop-menu-hover" href="#" data-gtmc="nav link"> Why Go <img class="go-Icon" height="24" width="24" src="/static/shared/icon/arrow_drop_down_gm_grey_24dp.svg" alt="submenu dropdown icon"> </a> <ul class="go-Header-submenu go-Header-submenu--why js-desktop-submenu-hover" aria-label="submenu"> <li class="go-Header-submenuItem"> <div> <a href="https://go.dev/solutions#case-studies"> <span>Case Studies</span> </a> </div> <p>Common problems companies solve with Go</p> </li> <li class="go-Header-submenuItem"> <div> <a href="https://go.dev/solutions#use-cases"> <span>Use Cases</span> </a> </div> <p>Stories about how and why companies use Go</p> </li> <li class="go-Header-submenuItem"> <div> <a href="https://go.dev/security/policy/"> <span>Security Policy</span> </a> </div> <p>How Go can help keep you secure by default</p> </li> </ul> </li> <li class="go-Header-menuItem"> <a href="https://go.dev/learn/" data-gtmc="nav link">Learn</a> </li> <li class="go-Header-menuItem"> <a class="js-desktop-menu-hover" href="#" data-gtmc="nav link"> Docs <img class="go-Icon" height="24" width="24" src="/static/shared/icon/arrow_drop_down_gm_grey_24dp.svg" alt="submenu dropdown icon"> </a> <ul class="go-Header-submenu go-Header-submenu--docs js-desktop-submenu-hover" aria-label="submenu"> <li class="go-Header-submenuItem"> <div> <a href="https://go.dev/doc/effective_go"> <span>Effective Go</span> </a> </div> <p>Tips for writing clear, performant, and idiomatic Go code</p> </li> <li class="go-Header-submenuItem"> <div> <a href="https://go.dev/doc/"> <span>Go User Manual</span> </a> </div> <p>A complete introduction to building software with Go</p> </li> <li class="go-Header-submenuItem"> <div> <a href="https://pkg.go.dev/std"> <span>Standard library</span> </a> </div> <p>Reference documentation for Go's standard library</p> </li> <li class="go-Header-submenuItem"> <div> <a href="https://go.dev/doc/devel/release"> <span>Release Notes</span> </a> </div> <p>Learn what's new in each Go release</p> </li> </ul> </li> <li class="go-Header-menuItem go-Header-menuItem--active"> <a href="/" data-gtmc="nav link">Packages</a> </li> <li class="go-Header-menuItem"> <a class="js-desktop-menu-hover" href="#" data-gtmc="nav link"> Community <img class="go-Icon" height="24" width="24" src="/static/shared/icon/arrow_drop_down_gm_grey_24dp.svg" alt="submenu dropdown icon"> </a> <ul class="go-Header-submenu go-Header-submenu--community js-desktop-submenu-hover" aria-label="submenu"> <li class="go-Header-submenuItem"> <div> <a href="https://go.dev/talks/"> <span>Recorded Talks</span> </a> </div> <p>Videos from prior events</p> </li> <li class="go-Header-submenuItem"> <div> <a href="https://www.meetup.com/pro/go"> <span>Meetups</span> <i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/launch_gm_grey_24dp.svg" alt=""> </i> </a> </div> <p>Meet other local Go developers</p> </li> <li class="go-Header-submenuItem"> <div> <a href="https://github.com/golang/go/wiki/Conferences"> <span>Conferences</span> <i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/launch_gm_grey_24dp.svg" alt=""> </i> </a> </div> <p>Learn and network with Go developers from around the world</p> </li> <li class="go-Header-submenuItem"> <div> <a href="https://go.dev/blog"> <span>Go blog</span> </a> </div> <p>The Go project's official blog.</p> </li> <li class="go-Header-submenuItem"> <div> <a href="https://go.dev/help"> <span>Go project</span> </a> </div> <p>Get help and stay informed from Go</p> </li> <li class="go-Header-submenuItem"> <div> Get connected </div> <p></p> <div class="go-Header-socialIcons"> <a class="go-Header-socialIcon" aria-label="Get connected with google-groups (Opens in new window)" title="Get connected with google-groups (Opens in new window)" href="https://groups.google.com/g/golang-nuts"> <img src="/static/shared/logo/social/google-groups.svg" /> </a> <a class="go-Header-socialIcon" aria-label="Get connected with github (Opens in new window)" title="Get connected with github (Opens in new window)" href="https://github.com/golang"> <img src="/static/shared/logo/social/github.svg" /> </a> <a class="go-Header-socialIcon" aria-label="Get connected with twitter (Opens in new window)" title="Get connected with twitter (Opens in new window)" href="https://twitter.com/golang"> <img src="/static/shared/logo/social/twitter.svg" /> </a> <a class="go-Header-socialIcon" aria-label="Get connected with reddit (Opens in new window)" title="Get connected with reddit (Opens in new window)" href="https://www.reddit.com/r/golang/"> <img src="/static/shared/logo/social/reddit.svg" /> </a> <a class="go-Header-socialIcon" aria-label="Get connected with slack (Opens in new window)" title="Get connected with slack (Opens in new window)" href="https://invite.slack.golangbridge.org/"> <img src="/static/shared/logo/social/slack.svg" /> </a> <a class="go-Header-socialIcon" aria-label="Get connected with stack-overflow (Opens in new window)" title="" href="https://stackoverflow.com/collectives/go"> <img src="/static/shared/logo/social/stack-overflow.svg" /> </a> </div> </li> </ul> </li> </ul> <button class="go-Header-navOpen js-headerMenuButton go-Header-navOpen--white" data-gtmc="nav button" aria-label="Open navigation"> </button> </div> </nav> </div> </header> <aside class="go-NavigationDrawer js-header"> <nav class="go-NavigationDrawer-nav"> <div class="go-NavigationDrawer-header"> <a href="https://go.dev/"> <img class="go-NavigationDrawer-logo" src="/static/shared/logo/go-blue.svg" alt="Go."> </a> </div> <ul class="go-NavigationDrawer-list"> <li class="go-NavigationDrawer-listItem js-mobile-subnav-trigger go-NavigationDrawer-hasSubnav"> <a href="#"> <span>Why Go</span> <i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/navigate_next_gm_grey_24dp.svg" alt=""> </i> </a> <div class="go-NavigationDrawer go-NavigationDrawer-submenuItem"> <div class="go-NavigationDrawer-nav"> <div class="go-NavigationDrawer-header"> <a href="#"> <i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/navigate_before_gm_grey_24dp.svg" alt=""> </i> Why Go </a> </div> <ul class="go-NavigationDrawer-list"> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/solutions#case-studies"> Case Studies </a> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/solutions#use-cases"> Use Cases </a> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/security/policy/"> Security Policy </a> </li> </ul> </div> </div> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/learn/">Learn</a> </li> <li class="go-NavigationDrawer-listItem js-mobile-subnav-trigger go-NavigationDrawer-hasSubnav"> <a href="#"> <span>Docs</span> <i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/navigate_next_gm_grey_24dp.svg" alt=""> </i> </a> <div class="go-NavigationDrawer go-NavigationDrawer-submenuItem"> <div class="go-NavigationDrawer-nav"> <div class="go-NavigationDrawer-header"> <a href="#"><i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/navigate_before_gm_grey_24dp.svg" alt=""> </i> Docs </a> </div> <ul class="go-NavigationDrawer-list"> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/doc/effective_go"> Effective Go </a> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/doc/"> Go User Manual </a> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://pkg.go.dev/std"> Standard library </a> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/doc/devel/release"> Release Notes </a> </li> </ul> </div> </div> </li> <li class="go-NavigationDrawer-listItem go-NavigationDrawer-listItem--active"> <a href="/">Packages</a> </li> <li class="go-NavigationDrawer-listItem js-mobile-subnav-trigger go-NavigationDrawer-hasSubnav"> <a href="#"> <span>Community</span> <i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/navigate_next_gm_grey_24dp.svg" alt=""> </i> </a> <div class="go-NavigationDrawer go-NavigationDrawer-submenuItem"> <div class="go-NavigationDrawer-nav"> <div class="go-NavigationDrawer-header"> <a href="#"> <i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/navigate_before_gm_grey_24dp.svg" alt=""> </i> Community </a> </div> <ul class="go-NavigationDrawer-list"> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/talks/"> Recorded Talks </a> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://www.meetup.com/pro/go"> Meetups <i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/launch_gm_grey_24dp.svg" alt=""> </i> </a> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://github.com/golang/go/wiki/Conferences"> Conferences <i class="material-icons"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/launch_gm_grey_24dp.svg" alt=""> </i> </a> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/blog"> Go blog </a> </li> <li class="go-NavigationDrawer-listItem"> <a href="https://go.dev/help"> Go project </a> </li> <li class="go-NavigationDrawer-listItem"> <div>Get connected</div> <div class="go-Header-socialIcons"> <a class="go-Header-socialIcon" href="https://groups.google.com/g/golang-nuts"><img src="/static/shared/logo/social/google-groups.svg" /></a> <a class="go-Header-socialIcon" href="https://github.com/golang"><img src="/static/shared/logo/social/github.svg" /></a> <a class="go-Header-socialIcon" href="https://twitter.com/golang"><img src="/static/shared/logo/social/twitter.svg" /></a> <a class="go-Header-socialIcon" href="https://www.reddit.com/r/golang/"><img src="/static/shared/logo/social/reddit.svg" /></a> <a class="go-Header-socialIcon" href="https://invite.slack.golangbridge.org/"><img src="/static/shared/logo/social/slack.svg" /></a> <a class="go-Header-socialIcon" href="https://stackoverflow.com/collectives/go"><img src="/static/shared/logo/social/stack-overflow.svg" /></a> </div> </li> </ul> </div> </div> </li> </ul> </nav> </aside> <div class="go-NavigationDrawer-scrim js-scrim" role="presentation"></div> <main class="go-Main" id="main-content"> <div class="go-Main-banner" role="alert"></div> <header class="go-Main-header js-mainHeader"> <nav class="go-Main-headerBreadcrumb go-Breadcrumb" aria-label="Breadcrumb" data-test-id="UnitHeader-breadcrumb"> <ol> <li data-test-id="UnitHeader-breadcrumbItem"> <a href="/" data-gtmc="breadcrumb link">Discover Packages</a> </li> <li data-test-id="UnitHeader-breadcrumbItem"> <a href="/std" data-gtmc="breadcrumb link">Standard library</a> </li> <li data-test-id="UnitHeader-breadcrumbItem"> <a href="/html" data-gtmc="breadcrumb link">html</a> </li> <li> <a href="/html/template@go1.24.0" data-gtmc="breadcrumb link" aria-current="location" data-test-id="UnitHeader-breadcrumbCurrent"> template </a> <button class="go-Button go-Button--inline go-Clipboard js-clipboard" title="Copy path to clipboard. html/template" aria-label="Copy Path to Clipboard" data-to-copy="html/template" data-gtmc="breadcrumbs button" > <img class="go-Icon go-Icon--accented" height="24" width="24" src="/static/shared/icon/content_copy_gm_grey_24dp.svg" alt="" > </button> </li> </ol> </nav> <div class="go-Main-headerContent"> <div class="go-Main-headerTitle js-stickyHeader"> <a class="go-Main-headerLogo" href="https://go.dev/" aria-hidden="true" tabindex="-1" data-gtmc="header link" aria-label="Link to Go Homepage"> <img height="78" width="207" src="/static/shared/logo/go-blue.svg" alt="Go"> </a> <h1 class="UnitHeader-titleHeading" data-test-id="UnitHeader-title">template</h1> <span class="go-Chip go-Chip--inverted">package</span> <span class="go-Chip go-Chip--inverted">standard library</span> <button class="go-Button go-Button--inline go-Clipboard js-clipboard" title="Copy path to clipboard. html/template" aria-label="Copy Path to Clipboard" data-to-copy="html/template" data-gtmc="title button" tabindex="-1" > <img class="go-Icon go-Icon--accented" height="24" width="24" src="/static/shared/icon/content_copy_gm_grey_24dp.svg" alt="" /> </button> </div> <div class="go-Main-headerDetails"> <span class="go-Main-headerDetailItem" data-test-id="UnitHeader-version"> <a href="?tab=versions" aria-label="Version: go1.24.0" data-gtmc="header link" aria-describedby="version-description"> <span class="go-textSubtle" aria-hidden="true">Version: </span> go1.24.0 </a> <div class="screen-reader-only" id="version-description" hidden> Opens a new window with list of versions in this module. </div> <span class="DetailsHeader-badge--latest" data-test-id="UnitHeader-minorVersionBanner"> <span class="go-Chip DetailsHeader-span--latest">Latest</span> <span class="go-Chip DetailsHeader-span--notAtLatest"> Latest <details class="go-Tooltip js-tooltip" data-gtmc="tooltip"> <summary> <img class="go-Icon go-Icon--inverted" height="24" width="24" src="/static/shared/icon/alert_gm_grey_24dp.svg" alt="Warning"> </summary> <p>This package is not in the latest version of its module.</p> </details> </span> <a href="/html/template" aria-label="Go to Latest Version" data-gtmc="header link"> <span class="go-Chip go-Chip--alert DetailsHeader-span--goToLatest">Go to latest</span> </a> </span> </span> <span class="go-Main-headerDetailItem" data-test-id="UnitHeader-commitTime"> Published: Feb 11, 2025 </span> <span class="go-Main-headerDetailItem" data-test-id="UnitHeader-licenses"> License: <a href="/html/template?tab=licenses" data-test-id="UnitHeader-license" data-gtmc="header link" aria-describedby="license-description">BSD-3-Clause</a> </span> <div class="screen-reader-only" id="license-description" hidden> Opens a new window with license information. </div> <span class="go-Main-headerDetailItem" data-test-id="UnitHeader-imports"> <a href="/html/template?tab=imports" aria-label="Imports: 20" data-gtmc="header link" aria-describedby="imports-description"> <span class="go-textSubtle">Imports: </span>20 </a> </span> <div class="screen-reader-only" id="imports-description" hidden> Opens a new window with list of imports. </div> <span class="go-Main-headerDetailItem" data-test-id="UnitHeader-importedby"> <a href="/html/template?tab=importedby" aria-label="Imported By: 108,144" data-gtmc="header link" aria-describedby="importedby-description"> <span class="go-textSubtle">Imported by: </span>108,144 </a> </span> <div class="screen-reader-only" id="importedby-description" hidden> Opens a new window with list of known importers. </div> </div> <div class="UnitHeader-overflowContainer"> <svg class="UnitHeader-overflowImage" xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"> <path d="M0 0h24v24H0z" fill="none"/> <path d="M12 8c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"/> </svg> <select class="UnitHeader-overflowSelect js-selectNav" tabindex="-1"> <option value="/">Main</option> <option value="/html/template?tab=versions"> Versions </option> <option value="/html/template?tab=licenses"> Licenses </option> <option value="/html/template?tab=imports"> Imports </option> <option value="/html/template?tab=importedby"> Imported By </option> </select> </div> </div> </header> <aside class="go-Main-aside js-mainAside"> <div class="UnitMeta"> <h2 class="go-textLabel">Details</h2> <ul class="UnitMeta-details"> <li> <details class="go-Tooltip js-tooltip" data-gtmc="tooltip"> <summary class="go-textSubtle"> <img class="go-Icon go-Icon--accented" tabindex="0" role="button"src="/static/shared/icon/check_circle_gm_grey_24dp.svg" alt="checked" aria-label="Valid file, toggle tooltip"height="24" width="24"> Valid <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/go.mod" target="_blank" rel="noopener">go.mod</a> file <img class="go-Icon" role="button" tabindex="0" src="/static/shared/icon/help_gm_grey_24dp.svg" alt="" aria-label="Toggle go.mod validity tooltip" height="24" width="24"> </summary> <p aria-live="polite" role="tooltip"> The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go. </p> </details> </li> <li> <details class="go-Tooltip js-tooltip" data-gtmc="tooltip"> <summary class="go-textSubtle"> <img class="go-Icon go-Icon--accented" tabindex="0" role="button"src="/static/shared/icon/check_circle_gm_grey_24dp.svg" alt="checked" aria-label="Valid file, toggle tooltip"height="24" width="24"> Redistributable license <img class="go-Icon" role="button" tabindex="0" src="/static/shared/icon/help_gm_grey_24dp.svg" alt="" aria-label="Toggle redistributable help tooltip" height="24" width="24"> </summary> <p aria-live="polite" role="tooltip"> Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed. </p> </details> </li> <li> <details class="go-Tooltip js-tooltip" data-gtmc="tooltip"> <summary class="go-textSubtle"> <img class="go-Icon go-Icon--accented" tabindex="0" role="button"src="/static/shared/icon/check_circle_gm_grey_24dp.svg" alt="checked" aria-label="Valid file, toggle tooltip"height="24" width="24"> Tagged version <img class="go-Icon" role="button" tabindex="0" src="/static/shared/icon/help_gm_grey_24dp.svg" alt="" aria-label="Toggle tagged version tooltip" height="24" width="24"> </summary> <p aria-live="polite" role="tooltip">Modules with tagged versions give importers more predictable builds.</p> </details> </li> <li> <details class="go-Tooltip js-tooltip" data-gtmc="tooltip"> <summary class="go-textSubtle"> <img class="go-Icon go-Icon--accented" tabindex="0" role="button"src="/static/shared/icon/check_circle_gm_grey_24dp.svg" alt="checked" aria-label="Valid file, toggle tooltip"height="24" width="24"> Stable version <img class="go-Icon" role="button" tabindex="0" aria-label="Toggle stable version tooltip" src="/static/shared/icon/help_gm_grey_24dp.svg" alt="" height="24" width="24"> </summary> <p aria-live="polite" role="tooltip">When a project reaches major version v1 it is considered stable.</p> </details> </li> <li class="UnitMeta-detailsLearn"> <a href="/about#best-practices" data-gtmc="meta link">Learn more about best practices</a> </li> </ul> <h2 class="go-textLabel">Repository</h2> <div class="UnitMeta-repo"> <a href="https://cs.opensource.google/go/go" title="https://cs.opensource.google/go/go" target="_blank" rel="noopener"> cs.opensource.google/go/go </a> </div> <h2 class="go-textLabel" data-test-id="links-heading">Links</h2> <ul class="UnitMeta-links"> <li> <a href="https://go.dev/security/policy" title="Report security issues in the Go standard library and sub-repositories"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/security_grey_24dp.svg" alt=""> Report a Vulnerability </a> </li> </ul> </div> </aside> <nav class="go-Main-nav go-Main-nav--sticky js-mainNav" aria-label="Outline"> <div class="go-Main-navDesktop"> <div class="UnitOutline-jumpTo"> <button class="UnitOutline-jumpToInput go-ShortcutKey js-jumpToInput" aria-controls="jump-to-modal" aria-label="Open Jump to Identifier" data-shortcut="f" data-shortcut-alt="find" data-test-id="jump-to-button" data-gtmc="outline button"> Jump to ... </button> </div> <ul class="go-Tree js-tree" role="tree" aria-label="Outline"> <li> <a href="#section-documentation" data-gtmc="outline link"> Documentation </a> <ul> <li> <a href="#pkg-overview" data-gtmc="doc outline link">Overview</a> </li> <li class="DocNav-overview"> <a href="#pkg-index" data-gtmc="doc outline link"> Index </a> <ul> <li> <a href="#pkg-examples" data-gtmc="doc outline link"> Examples </a> </li> </ul> </li> <li class="DocNav-constants"> <a href="#pkg-constants" data-gtmc="doc outline link"> Constants </a> </li> <li class="DocNav-variables"> <a href="#pkg-variables" data-gtmc="doc outline link"> Variables </a> </li> <li class="DocNav-functions"> <a href="#pkg-functions" data-gtmc="doc outline link"> Functions </a> <ul> <li> <a href="#HTMLEscape" title="HTMLEscape(w, b)" data-gtmc="doc outline link"> HTMLEscape(w, b) </a> </li> <li> <a href="#HTMLEscapeString" title="HTMLEscapeString(s)" data-gtmc="doc outline link"> HTMLEscapeString(s) </a> </li> <li> <a href="#HTMLEscaper" title="HTMLEscaper(args)" data-gtmc="doc outline link"> HTMLEscaper(args) </a> </li> <li> <a href="#IsTrue" title="IsTrue(val)" data-gtmc="doc outline link"> IsTrue(val) </a> </li> <li> <a href="#JSEscape" title="JSEscape(w, b)" data-gtmc="doc outline link"> JSEscape(w, b) </a> </li> <li> <a href="#JSEscapeString" title="JSEscapeString(s)" data-gtmc="doc outline link"> JSEscapeString(s) </a> </li> <li> <a href="#JSEscaper" title="JSEscaper(args)" data-gtmc="doc outline link"> JSEscaper(args) </a> </li> <li> <a href="#URLQueryEscaper" title="URLQueryEscaper(args)" data-gtmc="doc outline link"> URLQueryEscaper(args) </a> </li> </ul> </li> <li class="DocNav-types"> <a href="#pkg-types" data-gtmc="doc outline link"> Types </a> <ul> <li> <a href="#CSS" title="type CSS" data-gtmc="doc outline link"> type CSS </a> </li> <li> <a href="#Error" title="type Error" data-gtmc="doc outline link"> type Error </a> <ul> <li> <a href="#Error.Error" title="(e) Error()" data-gtmc="doc outline link"> (e) Error() </a> </li> </ul> </li> <li> <a href="#ErrorCode" title="type ErrorCode" data-gtmc="doc outline link"> type ErrorCode </a> </li> <li> <a href="#FuncMap" title="type FuncMap" data-gtmc="doc outline link"> type FuncMap </a> </li> <li> <a href="#HTML" title="type HTML" data-gtmc="doc outline link"> type HTML </a> </li> <li> <a href="#HTMLAttr" title="type HTMLAttr" data-gtmc="doc outline link"> type HTMLAttr </a> </li> <li> <a href="#JS" title="type JS" data-gtmc="doc outline link"> type JS </a> </li> <li> <a href="#JSStr" title="type JSStr" data-gtmc="doc outline link"> type JSStr </a> </li> <li> <a href="#Srcset" title="type Srcset" data-gtmc="doc outline link"> type Srcset </a> </li> <li> <a href="#Template" title="type Template" data-gtmc="doc outline link"> type Template </a> <ul> <li> <a href="#Must" title="Must(t, err)" data-gtmc="doc outline link"> Must(t, err) </a> </li> <li> <a href="#New" title="New(name)" data-gtmc="doc outline link"> New(name) </a> </li> <li> <a href="#ParseFS" title="ParseFS(fs, patterns)" data-gtmc="doc outline link"> ParseFS(fs, patterns) </a> </li> <li> <a href="#ParseFiles" title="ParseFiles(filenames)" data-gtmc="doc outline link"> ParseFiles(filenames) </a> </li> <li> <a href="#ParseGlob" title="ParseGlob(pattern)" data-gtmc="doc outline link"> ParseGlob(pattern) </a> </li> <li> <a href="#Template.AddParseTree" title="(t) AddParseTree(name, tree)" data-gtmc="doc outline link"> (t) AddParseTree(name, tree) </a> </li> <li> <a href="#Template.Clone" title="(t) Clone()" data-gtmc="doc outline link"> (t) Clone() </a> </li> <li> <a href="#Template.DefinedTemplates" title="(t) DefinedTemplates()" data-gtmc="doc outline link"> (t) DefinedTemplates() </a> </li> <li> <a href="#Template.Delims" title="(t) Delims(left, right)" data-gtmc="doc outline link"> (t) Delims(left, right) </a> </li> <li> <a href="#Template.Execute" title="(t) Execute(wr, data)" data-gtmc="doc outline link"> (t) Execute(wr, data) </a> </li> <li> <a href="#Template.ExecuteTemplate" title="(t) ExecuteTemplate(wr, name, data)" data-gtmc="doc outline link"> (t) ExecuteTemplate(wr, name, data) </a> </li> <li> <a href="#Template.Funcs" title="(t) Funcs(funcMap)" data-gtmc="doc outline link"> (t) Funcs(funcMap) </a> </li> <li> <a href="#Template.Lookup" title="(t) Lookup(name)" data-gtmc="doc outline link"> (t) Lookup(name) </a> </li> <li> <a href="#Template.Name" title="(t) Name()" data-gtmc="doc outline link"> (t) Name() </a> </li> <li> <a href="#Template.New" title="(t) New(name)" data-gtmc="doc outline link"> (t) New(name) </a> </li> <li> <a href="#Template.Option" title="(t) Option(opt)" data-gtmc="doc outline link"> (t) Option(opt) </a> </li> <li> <a href="#Template.Parse" title="(t) Parse(text)" data-gtmc="doc outline link"> (t) Parse(text) </a> </li> <li> <a href="#Template.ParseFS" title="(t) ParseFS(fs, patterns)" data-gtmc="doc outline link"> (t) ParseFS(fs, patterns) </a> </li> <li> <a href="#Template.ParseFiles" title="(t) ParseFiles(filenames)" data-gtmc="doc outline link"> (t) ParseFiles(filenames) </a> </li> <li> <a href="#Template.ParseGlob" title="(t) ParseGlob(pattern)" data-gtmc="doc outline link"> (t) ParseGlob(pattern) </a> </li> <li> <a href="#Template.Templates" title="(t) Templates()" data-gtmc="doc outline link"> (t) Templates() </a> </li> </ul> </li> <li> <a href="#URL" title="type URL" data-gtmc="doc outline link"> type URL </a> </li> </ul> </li> </ul> </li> <li> <a href="#section-sourcefiles" data-gtmc="outline link"> Source Files </a> </li> </ul> </div> <div class="go-Main-navMobile js-mainNavMobile"> <label class="go-Label"> <select class="go-Select"> <option selected disabled>Documentation</option> </select> </label> </div> </nav> <article class="go-Main-article js-mainContent"> <div class="UnitDetails" data-test-id="UnitDetails" style="display: block;"> <div class="UnitDetails-content js-unitDetailsContent" data-test-id="UnitDetails-content"> <div class="UnitDoc"> <h2 class="UnitDoc-title" id="section-documentation"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/code_gm_grey_24dp.svg" alt=""> Documentation <a class="UnitDoc-idLink" href="#section-documentation" title="Go to Documentation" aria-label="Go to Documentation">露</a> </h2> <div class="Documentation js-documentation"> <div class="Documentation-content js-docContent"> <section class="Documentation-overview"> <h3 tabindex="-1" id="pkg-overview" class="Documentation-overviewHeader">Overview <a href="#pkg-overview" title="Go to Overview" aria-label="Go to Overview">露</a></h3> <div role="navigation" aria-label="Table of Contents"> <ul class="Documentation-toc Documentation-toc-columns"> <li class="Documentation-tocItem"><a href="#hdr-Introduction">Introduction</a></li> <li class="Documentation-tocItem"><a href="#hdr-Contexts">Contexts</a></li> <li class="Documentation-tocItem"><a href="#hdr-Namespaced_and_data__attributes">Namespaced and data- attributes</a></li> <li class="Documentation-tocItem"><a href="#hdr-Errors">Errors</a></li> <li class="Documentation-tocItem"><a href="#hdr-A_fuller_picture">A fuller picture</a></li> <li class="Documentation-tocItem"><a href="#hdr-Contexts-1">Contexts</a></li> <li class="Documentation-tocItem"><a href="#hdr-Typed_Strings">Typed Strings</a></li> <li class="Documentation-tocItem"><a href="#hdr-Security_Model">Security Model</a></li> </ul> </div> <p>Package template (html/template) implements data-driven templates for generating HTML output safe against code injection. It provides the same interface as <a href="/text/template">text/template</a> and should be used instead of <a href="/text/template">text/template</a> whenever the output is HTML. </p><p>The documentation here focuses on the security features of the package. For information about how to program the templates themselves, see the documentation for <a href="/text/template">text/template</a>. </p><h4 id="hdr-Introduction">Introduction <a class="Documentation-idLink" href="#hdr-Introduction" title="Go to Introduction" aria-label="Go to Introduction">露</a></h4><p>This package wraps <a href="/text/template">text/template</a> so you can share its template API to parse and execute HTML templates safely. </p><pre>tmpl, err := template.New("name").Parse(...) // Error checking elided err = tmpl.Execute(out, data) </pre><p>If successful, tmpl will now be injection-safe. Otherwise, err is an error defined in the docs for ErrorCode. </p><p>HTML templates treat data values as plain text which should be encoded so they can be safely embedded in an HTML document. The escaping is contextual, so actions can appear within JavaScript, CSS, and URI contexts. </p><p>The security model used by this package assumes that template authors are trusted, while Execute's data parameter is not. More details are provided below. </p><p>Example </p><pre>import "text/template" ... t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`) err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned')</script>") </pre><p>produces </p><pre>Hello, <script>alert('you have been pwned')</script>! </pre><p>but the contextual autoescaping in html/template </p><pre>import "html/template" ... t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`) err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned')</script>") </pre><p>produces safe, escaped HTML output </p><pre>Hello, &lt;script&gt;alert(&#39;you have been pwned&#39;)&lt;/script&gt;! </pre><h4 id="hdr-Contexts">Contexts <a class="Documentation-idLink" href="#hdr-Contexts" title="Go to Contexts" aria-label="Go to Contexts">露</a></h4><p>This package understands HTML, CSS, JavaScript, and URIs. It adds sanitizing functions to each simple action pipeline, so given the excerpt </p><pre><a href="/search?q={{.}}">{{.}}</a> </pre><p>At parse time each {{.}} is overwritten to add escaping functions as necessary. In this case it becomes </p><pre><a href="/search?q={{. | urlescaper | attrescaper}}">{{. | htmlescaper}}</a> </pre><p>where urlescaper, attrescaper, and htmlescaper are aliases for internal escaping functions. </p><p>For these internal escaping functions, if an action pipeline evaluates to a nil interface value, it is treated as though it were an empty string. </p><h4 id="hdr-Namespaced_and_data__attributes">Namespaced and data- attributes <a class="Documentation-idLink" href="#hdr-Namespaced_and_data__attributes" title="Go to Namespaced and data- attributes" aria-label="Go to Namespaced and data- attributes">露</a></h4><p>Attributes with a namespace are treated as if they had no namespace. Given the excerpt </p><pre><a my:href="{{.}}"></a> </pre><p>At parse time the attribute will be treated as if it were just "href". So at parse time the template becomes: </p><pre><a my:href="{{. | urlescaper | attrescaper}}"></a> </pre><p>Similarly to attributes with namespaces, attributes with a "data-" prefix are treated as if they had no "data-" prefix. So given </p><pre><a data-href="{{.}}"></a> </pre><p>At parse time this becomes </p><pre><a data-href="{{. | urlescaper | attrescaper}}"></a> </pre><p>If an attribute has both a namespace and a "data-" prefix, only the namespace will be removed when determining the context. For example </p><pre><a my:data-href="{{.}}"></a> </pre><p>This is handled as if "my:data-href" was just "data-href" and not "href" as it would be if the "data-" prefix were to be ignored too. Thus at parse time this becomes just </p><pre><a my:data-href="{{. | attrescaper}}"></a> </pre><p>As a special case, attributes with the namespace "xmlns" are always treated as containing URLs. Given the excerpts </p><pre><a xmlns:title="{{.}}"></a> <a xmlns:href="{{.}}"></a> <a xmlns:onclick="{{.}}"></a> </pre><p>At parse time they become: </p><pre><a xmlns:title="{{. | urlescaper | attrescaper}}"></a> <a xmlns:href="{{. | urlescaper | attrescaper}}"></a> <a xmlns:onclick="{{. | urlescaper | attrescaper}}"></a> </pre><h4 id="hdr-Errors">Errors <a class="Documentation-idLink" href="#hdr-Errors" title="Go to Errors" aria-label="Go to Errors">露</a></h4><p>See the documentation of ErrorCode for details. </p><h4 id="hdr-A_fuller_picture">A fuller picture <a class="Documentation-idLink" href="#hdr-A_fuller_picture" title="Go to A fuller picture" aria-label="Go to A fuller picture">露</a></h4><p>The rest of this package comment may be skipped on first reading; it includes details necessary to understand escaping contexts and error messages. Most users will not need to understand these details. </p><h4 id="hdr-Contexts-1">Contexts <a class="Documentation-idLink" href="#hdr-Contexts-1" title="Go to Contexts" aria-label="Go to Contexts">露</a></h4><p>Assuming {{.}} is `O'Reilly: How are <i>you</i>?`, the table below shows how {{.}} appears when used in the context to the left. </p><pre>Context {{.}} After {{.}} O'Reilly: How are &lt;i&gt;you&lt;/i&gt;? <a title='{{.}}'> O&#39;Reilly: How are you? <a href="/{{.}}"> O&#39;Reilly: How are %3ci%3eyou%3c/i%3e? <a href="?q={{.}}"> O&#39;Reilly%3a%20How%20are%3ci%3e...%3f <a onx='f("{{.}}")'> O\x27Reilly: How are \x3ci\x3eyou...? <a onx='f({{.}})'> "O\x27Reilly: How are \x3ci\x3eyou...?" <a onx='pattern = /{{.}}/;'> O\x27Reilly: How are \x3ci\x3eyou...\x3f </pre><p>If used in an unsafe context, then the value might be filtered out: </p><pre>Context {{.}} After <a href="{{.}}"> #ZgotmplZ </pre><p>since "O'Reilly:" is not an allowed protocol like "http:". </p><p>If {{.}} is the innocuous word, `left`, then it can appear more widely, </p><pre>Context {{.}} After {{.}} left <a title='{{.}}'> left <a href='{{.}}'> left <a href='/{{.}}'> left <a href='?dir={{.}}'> left <a style="border-{{.}}: 4px"> left <a style="align: {{.}}"> left <a style="background: '{{.}}'> left <a style="background: url('{{.}}')> left <style>p.{{.}} {color:red}</style> left </pre><p>Non-string values can be used in JavaScript contexts. If {{.}} is </p><pre>struct{A,B string}{ "foo", "bar" } </pre><p>in the escaped template </p><pre><script>var pair = {{.}};</script> </pre><p>then the template output is </p><pre><script>var pair = {"A": "foo", "B": "bar"};</script> </pre><p>See package json to understand how non-string content is marshaled for embedding in JavaScript contexts. </p><h4 id="hdr-Typed_Strings">Typed Strings <a class="Documentation-idLink" href="#hdr-Typed_Strings" title="Go to Typed Strings" aria-label="Go to Typed Strings">露</a></h4><p>By default, this package assumes that all pipelines produce a plain text string. It adds escaping pipeline stages necessary to correctly and safely embed that plain text string in the appropriate context. </p><p>When a data value is not plain text, you can make sure it is not over-escaped by marking it with its type. </p><p>Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping. </p><p>The template </p><pre>Hello, {{.}}! </pre><p>can be invoked with </p><pre>tmpl.Execute(out, template.HTML(`<b>World</b>`)) </pre><p>to produce </p><pre>Hello, <b>World</b>! </pre><p>instead of the </p><pre>Hello, &lt;b&gt;World&lt;b&gt;! </pre><p>that would have been produced if {{.}} was a regular string. </p><h4 id="hdr-Security_Model">Security Model <a class="Documentation-idLink" href="#hdr-Security_Model" title="Go to Security Model" aria-label="Go to Security Model">露</a></h4><p><a href="https://rawgit.com/mikesamuel/sanitized-jquery-templates/trunk/safetemplate.html#problem_definition">https://rawgit.com/mikesamuel/sanitized-jquery-templates/trunk/safetemplate.html#problem_definition</a> defines "safe" as used by this package. </p><p>This package assumes that template authors are trusted, that Execute's data parameter is not, and seeks to preserve the properties below in the face of untrusted data: </p><p>Structure Preservation Property: "... when a template author writes an HTML tag in a safe templating language, the browser will interpret the corresponding portion of the output as a tag regardless of the values of untrusted data, and similarly for other structures such as attribute boundaries and JS and CSS string boundaries." </p><p>Code Effect Property: "... only code specified by the template author should run as a result of injecting the template output into a page and all code specified by the template author should run as a result of the same." </p><p>Least Surprise Property: "A developer (or code reviewer) familiar with HTML, CSS, and JavaScript, who knows that contextual autoescaping happens should be able to look at a {{.}} and correctly infer what sanitization happens." </p><p>Previously, ECMAScript 6 template literal were disabled by default, and could be enabled with the GODEBUG=jstmpllitinterp=1 environment variable. Template literals are now supported by default, and setting jstmpllitinterp has no effect. </p> <details tabindex="-1" id="example-package" class="Documentation-exampleDetails js-exampleContainer"> <summary class="Documentation-exampleDetailsHeader">Example <a href="#example-package" title="Go to Example" aria-label="Go to Example">露</a></summary> <div class="Documentation-exampleDetailsBody"> <pre class="Documentation-exampleCode"> package main import ( "html/template" "log" "os" ) func main() { const tpl = ` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>{{.Title}}</title> </head> <body> {{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}} </body> </html>` check := func(err error) { if err != nil { log.Fatal(err) } } t, err := template.New("webpage").Parse(tpl) check(err) data := struct { Title string Items []string }{ Title: "My page", Items: []string{ "My photos", "My blog", }, } err = t.Execute(os.Stdout, data) check(err) noItems := struct { Title string Items []string }{ Title: "My another page", Items: []string{}, } err = t.Execute(os.Stdout, noItems) check(err) } </pre> <pre><span class="Documentation-exampleOutputLabel">Output:</span> <span class="Documentation-exampleOutput"><!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>My page</title> </head> <body> <div>My photos</div><div>My blog</div> </body> </html> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>My another page</title> </head> <body> <div><strong>no rows</strong></div> </body> </html> </span></pre> </div> <div class="Documentation-exampleButtonsContainer"> <p class="Documentation-exampleError" role="alert" aria-atomic="true"></p> <button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button> <button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button> <button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button> </div></details> <details tabindex="-1" id="example-package-Autoescaping" class="Documentation-exampleDetails js-exampleContainer"> <summary class="Documentation-exampleDetailsHeader">Example (Autoescaping) <a href="#example-package-Autoescaping" title="Go to Example (Autoescaping)" aria-label="Go to Example (Autoescaping)">露</a></summary> <div class="Documentation-exampleDetailsBody"> <pre class="Documentation-exampleCode"> package main import ( "html/template" "log" "os" ) func main() { check := func(err error) { if err != nil { log.Fatal(err) } } t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`) check(err) err = t.ExecuteTemplate(os.Stdout, "T", "<script>alert('you have been pwned')</script>") check(err) } </pre> <pre><span class="Documentation-exampleOutputLabel">Output:</span> <span class="Documentation-exampleOutput">Hello, &lt;script&gt;alert(&#39;you have been pwned&#39;)&lt;/script&gt;! </span></pre> </div> <div class="Documentation-exampleButtonsContainer"> <p class="Documentation-exampleError" role="alert" aria-atomic="true"></p> <button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button> <button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button> <button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button> </div></details> <details tabindex="-1" id="example-package-Escape" class="Documentation-exampleDetails js-exampleContainer"> <summary class="Documentation-exampleDetailsHeader">Example (Escape) <a href="#example-package-Escape" title="Go to Example (Escape)" aria-label="Go to Example (Escape)">露</a></summary> <div class="Documentation-exampleDetailsBody"> <pre class="Documentation-exampleCode"> package main import ( "fmt" "html/template" "os" ) func main() { const s = `"Fran & Freddie's Diner" <tasty@example.com>` v := []any{`"Fran & Freddie's Diner"`, ' ', `<tasty@example.com>`} fmt.Println(template.HTMLEscapeString(s)) template.HTMLEscape(os.Stdout, []byte(s)) fmt.Fprintln(os.Stdout, "") fmt.Println(template.HTMLEscaper(v...)) fmt.Println(template.JSEscapeString(s)) template.JSEscape(os.Stdout, []byte(s)) fmt.Fprintln(os.Stdout, "") fmt.Println(template.JSEscaper(v...)) fmt.Println(template.URLQueryEscaper(v...)) } </pre> <pre><span class="Documentation-exampleOutputLabel">Output:</span> <span class="Documentation-exampleOutput">&#34;Fran &amp; Freddie&#39;s Diner&#34; &lt;tasty@example.com&gt; &#34;Fran &amp; Freddie&#39;s Diner&#34; &lt;tasty@example.com&gt; &#34;Fran &amp; Freddie&#39;s Diner&#34;32&lt;tasty@example.com&gt; \"Fran \u0026 Freddie\'s Diner\" \u003Ctasty@example.com\u003E \"Fran \u0026 Freddie\'s Diner\" \u003Ctasty@example.com\u003E \"Fran \u0026 Freddie\'s Diner\"32\u003Ctasty@example.com\u003E %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E </span></pre> </div> <div class="Documentation-exampleButtonsContainer"> <p class="Documentation-exampleError" role="alert" aria-atomic="true"></p> <button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button> <button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button> <button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button> </div></details> </section><section class="Documentation-index"> <h3 id="pkg-index" class="Documentation-indexHeader">Index <a href="#pkg-index" title="Go to Index" aria-label="Go to Index">露</a></h3> <ul class="Documentation-indexList"> <li class="Documentation-indexFunction"> <a href="#HTMLEscape">func HTMLEscape(w io.Writer, b []byte)</a></li> <li class="Documentation-indexFunction"> <a href="#HTMLEscapeString">func HTMLEscapeString(s string) string</a></li> <li class="Documentation-indexFunction"> <a href="#HTMLEscaper">func HTMLEscaper(args ...any) string</a></li> <li class="Documentation-indexFunction"> <a href="#IsTrue">func IsTrue(val any) (truth, ok bool)</a></li> <li class="Documentation-indexFunction"> <a href="#JSEscape">func JSEscape(w io.Writer, b []byte)</a></li> <li class="Documentation-indexFunction"> <a href="#JSEscapeString">func JSEscapeString(s string) string</a></li> <li class="Documentation-indexFunction"> <a href="#JSEscaper">func JSEscaper(args ...any) string</a></li> <li class="Documentation-indexFunction"> <a href="#URLQueryEscaper">func URLQueryEscaper(args ...any) string</a></li> <li class="Documentation-indexType"> <a href="#CSS">type CSS</a></li> <li class="Documentation-indexType"> <a href="#Error">type Error</a></li> <li><ul class="Documentation-indexTypeMethods"> <li> <a href="#Error.Error">func (e *Error) Error() string</a></li> </ul></li> <li class="Documentation-indexType"> <a href="#ErrorCode">type ErrorCode</a></li> <li class="Documentation-indexType"> <a href="#FuncMap">type FuncMap</a></li> <li class="Documentation-indexType"> <a href="#HTML">type HTML</a></li> <li class="Documentation-indexType"> <a href="#HTMLAttr">type HTMLAttr</a></li> <li class="Documentation-indexType"> <a href="#JS">type JS</a></li> <li class="Documentation-indexType"> <a href="#JSStr">type JSStr</a></li> <li class="Documentation-indexType"> <a href="#Srcset">type Srcset</a></li> <li class="Documentation-indexType"> <a href="#Template">type Template</a></li> <li><ul class="Documentation-indexTypeFunctions"> <li> <a href="#Must">func Must(t *Template, err error) *Template</a></li> <li> <a href="#New">func New(name string) *Template</a></li> <li> <a href="#ParseFS">func ParseFS(fs fs.FS, patterns ...string) (*Template, error)</a></li> <li> <a href="#ParseFiles">func ParseFiles(filenames ...string) (*Template, error)</a></li> <li> <a href="#ParseGlob">func ParseGlob(pattern string) (*Template, error)</a></li> </ul></li> <li><ul class="Documentation-indexTypeMethods"> <li> <a href="#Template.AddParseTree">func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error)</a></li> <li> <a href="#Template.Clone">func (t *Template) Clone() (*Template, error)</a></li> <li> <a href="#Template.DefinedTemplates">func (t *Template) DefinedTemplates() string</a></li> <li> <a href="#Template.Delims">func (t *Template) Delims(left, right string) *Template</a></li> <li> <a href="#Template.Execute">func (t *Template) Execute(wr io.Writer, data any) error</a></li> <li> <a href="#Template.ExecuteTemplate">func (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) error</a></li> <li> <a href="#Template.Funcs">func (t *Template) Funcs(funcMap FuncMap) *Template</a></li> <li> <a href="#Template.Lookup">func (t *Template) Lookup(name string) *Template</a></li> <li> <a href="#Template.Name">func (t *Template) Name() string</a></li> <li> <a href="#Template.New">func (t *Template) New(name string) *Template</a></li> <li> <a href="#Template.Option">func (t *Template) Option(opt ...string) *Template</a></li> <li> <a href="#Template.Parse">func (t *Template) Parse(text string) (*Template, error)</a></li> <li> <a href="#Template.ParseFS">func (t *Template) ParseFS(fs fs.FS, patterns ...string) (*Template, error)</a></li> <li> <a href="#Template.ParseFiles">func (t *Template) ParseFiles(filenames ...string) (*Template, error)</a></li> <li> <a href="#Template.ParseGlob">func (t *Template) ParseGlob(pattern string) (*Template, error)</a></li> <li> <a href="#Template.Templates">func (t *Template) Templates() []*Template</a></li> </ul></li> <li class="Documentation-indexType"> <a href="#URL">type URL</a></li> </ul> </section><section class="Documentation-examples"> <h4 tabindex="-1" id="pkg-examples" class="Documentation-examplesHeader">Examples <a class="Documentation-idLink" href="#pkg-examples" title="Go to Examples" aria-label="Go to Examples">露</a></h4> <ul class="Documentation-examplesList"> <li><a href="#example-package" class="js-exampleHref">Package</a></li> <li><a href="#example-package-Autoescaping" class="js-exampleHref">Package (Autoescaping)</a></li> <li><a href="#example-package-Escape" class="js-exampleHref">Package (Escape)</a></li> <li><a href="#example-Template-Block" class="js-exampleHref">Template (Block)</a></li> <li><a href="#example-Template-Glob" class="js-exampleHref">Template (Glob)</a></li> <li><a href="#example-Template-Helpers" class="js-exampleHref">Template (Helpers)</a></li> <li><a href="#example-Template-Parsefiles" class="js-exampleHref">Template (Parsefiles)</a></li> <li><a href="#example-Template-Share" class="js-exampleHref">Template (Share)</a></li> <li><a href="#example-Template.Delims" class="js-exampleHref">Template.Delims</a></li> </ul> </section><h3 tabindex="-1" id="pkg-constants" class="Documentation-constantsHeader">Constants <a href="#pkg-constants" title="Go to Constants" aria-label="Go to Constants">露</a></h3> <section class="Documentation-constants"><p class="Documentation-empty">This section is empty.</p></section> <h3 tabindex="-1" id="pkg-variables" class="Documentation-variablesHeader">Variables <a href="#pkg-variables" title="Go to Variables" aria-label="Go to Variables">露</a></h3> <section class="Documentation-variables"><p class="Documentation-empty">This section is empty.</p></section> <h3 tabindex="-1" id="pkg-functions" class="Documentation-functionsHeader">Functions <a href="#pkg-functions" title="Go to Functions" aria-label="Go to Functions">露</a></h3> <section class="Documentation-functions"><div class="Documentation-function"> <h4 tabindex="-1" id="HTMLEscape" data-kind="function" class="Documentation-functionHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/escape.go;l=962">HTMLEscape</a> <a class="Documentation-idLink" href="#HTMLEscape" title="Go to HTMLEscape" aria-label="Go to HTMLEscape">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func HTMLEscape(w <a href="/io">io</a>.<a href="/io#Writer">Writer</a>, b []<a href="/builtin#byte">byte</a>)</pre> </div> <p>HTMLEscape writes to w the escaped HTML equivalent of the plain text data b. </p> </div><div class="Documentation-function"> <h4 tabindex="-1" id="HTMLEscapeString" data-kind="function" class="Documentation-functionHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/escape.go;l=967">HTMLEscapeString</a> <a class="Documentation-idLink" href="#HTMLEscapeString" title="Go to HTMLEscapeString" aria-label="Go to HTMLEscapeString">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func HTMLEscapeString(s <a href="/builtin#string">string</a>) <a href="/builtin#string">string</a></pre> </div> <p>HTMLEscapeString returns the escaped HTML equivalent of the plain text data s. </p> </div><div class="Documentation-function"> <h4 tabindex="-1" id="HTMLEscaper" data-kind="function" class="Documentation-functionHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/escape.go;l=973">HTMLEscaper</a> <a class="Documentation-idLink" href="#HTMLEscaper" title="Go to HTMLEscaper" aria-label="Go to HTMLEscaper">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func HTMLEscaper(args ...<a href="/builtin#any">any</a>) <a href="/builtin#string">string</a></pre> </div> <p>HTMLEscaper returns the escaped HTML equivalent of the textual representation of its arguments. </p> </div><div class="Documentation-function"> <h4 tabindex="-1" id="IsTrue" data-kind="function" class="Documentation-functionHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=483">IsTrue</a> <a class="Documentation-idLink" href="#IsTrue" title="Go to IsTrue" aria-label="Go to IsTrue">露</a></span> <span class="Documentation-sinceVersion"> <span class="Documentation-sinceVersionLabel">added in</span> <span class="Documentation-sinceVersionVersion">go1.6</span> </span> </h4> <div class="Documentation-declaration"> <pre>func IsTrue(val <a href="/builtin#any">any</a>) (truth, ok <a href="/builtin#bool">bool</a>)</pre> </div> <p>IsTrue reports whether the value is 'true', in the sense of not the zero of its type, and whether the value has a meaningful truth value. This is the definition of truth used by if and other such actions. </p> </div><div class="Documentation-function"> <h4 tabindex="-1" id="JSEscape" data-kind="function" class="Documentation-functionHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/escape.go;l=978">JSEscape</a> <a class="Documentation-idLink" href="#JSEscape" title="Go to JSEscape" aria-label="Go to JSEscape">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func JSEscape(w <a href="/io">io</a>.<a href="/io#Writer">Writer</a>, b []<a href="/builtin#byte">byte</a>)</pre> </div> <p>JSEscape writes to w the escaped JavaScript equivalent of the plain text data b. </p> </div><div class="Documentation-function"> <h4 tabindex="-1" id="JSEscapeString" data-kind="function" class="Documentation-functionHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/escape.go;l=983">JSEscapeString</a> <a class="Documentation-idLink" href="#JSEscapeString" title="Go to JSEscapeString" aria-label="Go to JSEscapeString">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func JSEscapeString(s <a href="/builtin#string">string</a>) <a href="/builtin#string">string</a></pre> </div> <p>JSEscapeString returns the escaped JavaScript equivalent of the plain text data s. </p> </div><div class="Documentation-function"> <h4 tabindex="-1" id="JSEscaper" data-kind="function" class="Documentation-functionHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/escape.go;l=989">JSEscaper</a> <a class="Documentation-idLink" href="#JSEscaper" title="Go to JSEscaper" aria-label="Go to JSEscaper">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func JSEscaper(args ...<a href="/builtin#any">any</a>) <a href="/builtin#string">string</a></pre> </div> <p>JSEscaper returns the escaped JavaScript equivalent of the textual representation of its arguments. </p> </div><div class="Documentation-function"> <h4 tabindex="-1" id="URLQueryEscaper" data-kind="function" class="Documentation-functionHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/escape.go;l=995">URLQueryEscaper</a> <a class="Documentation-idLink" href="#URLQueryEscaper" title="Go to URLQueryEscaper" aria-label="Go to URLQueryEscaper">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func URLQueryEscaper(args ...<a href="/builtin#any">any</a>) <a href="/builtin#string">string</a></pre> </div> <p>URLQueryEscaper returns the escaped value of the textual representation of its arguments in a form suitable for embedding in a URL query. </p> </div></section> <h3 tabindex="-1" id="pkg-types" class="Documentation-typesHeader">Types <a href="#pkg-types" title="Go to Types" aria-label="Go to Types">露</a></h3> <section class="Documentation-types"><div class="Documentation-type"> <h4 tabindex="-1" id="CSS" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/content.go;l=25">CSS</a> <a class="Documentation-idLink" href="#CSS" title="Go to CSS" aria-label="Go to CSS">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type CSS <a href="/builtin#string">string</a></pre> </div> <p>CSS encapsulates known safe content that matches any of: </p><ol class="Documentation-numberList"> <li value="1">The CSS3 stylesheet production, such as `p { color: purple }`.</li> <li value="2">The CSS3 rule production, such as `a[href=~"https:"].foo#bar`.</li> <li value="3">CSS3 declaration productions, such as `color: red; margin: 2px`.</li> <li value="4">The CSS3 value production, such as `rgba(0, 0, 255, 127)`.</li> </ol><p>See <a href="https://www.w3.org/TR/css3-syntax/#parsing">https://www.w3.org/TR/css3-syntax/#parsing</a> and <a href="https://web.archive.org/web/20090211114933/http://w3.org/TR/css3-syntax#style">https://web.archive.org/web/20090211114933/http://w3.org/TR/css3-syntax#style</a> </p><p>Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output. </p> </div><div class="Documentation-type"> <h4 tabindex="-1" id="Error" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/error.go;l=13">Error</a> <a class="Documentation-idLink" href="#Error" title="Go to Error" aria-label="Go to Error">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type Error struct { <span id="Error.ErrorCode" data-kind="field"> <span class="comment">// ErrorCode describes the kind of error.</span> </span> ErrorCode <a href="#ErrorCode">ErrorCode</a> <span id="Error.Node" data-kind="field"> <span class="comment">// Node is the node that caused the problem, if known.</span> </span> <span class="comment">// If not nil, it overrides Name and Line.</span> Node <a href="/text/template/parse">parse</a>.<a href="/text/template/parse#Node">Node</a> <span id="Error.Name" data-kind="field"> <span class="comment">// Name is the name of the template in which the error was encountered.</span> </span> Name <a href="/builtin#string">string</a> <span id="Error.Line" data-kind="field"> <span class="comment">// Line is the line number of the error in the template source or 0.</span> </span> Line <a href="/builtin#int">int</a> <span id="Error.Description" data-kind="field"> <span class="comment">// Description is a human-readable description of the problem.</span> </span> Description <a href="/builtin#string">string</a> }</pre> </div> <p>Error describes a problem encountered during template Escaping. </p> <div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Error.Error" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Error) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/error.go;l=231">Error</a> <a class="Documentation-idLink" href="#Error.Error" title="Go to Error.Error" aria-label="Go to Error.Error">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (e *<a href="#Error">Error</a>) Error() <a href="/builtin#string">string</a></pre> </div> </div> </div><div class="Documentation-type"> <h4 tabindex="-1" id="ErrorCode" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/error.go;l=28">ErrorCode</a> <a class="Documentation-idLink" href="#ErrorCode" title="Go to ErrorCode" aria-label="Go to ErrorCode">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type ErrorCode <a href="/builtin#int">int</a></pre> </div> <p>ErrorCode is a code for a kind of error. </p> <div class="Documentation-typeConstant"> <div class="Documentation-declaration"> <pre>const ( <span id="OK" data-kind="constant"> <span class="comment">// OK indicates the lack of an error.</span> </span> OK <a href="#ErrorCode">ErrorCode</a> = <a href="/builtin#iota">iota</a> <span id="ErrAmbigContext" data-kind="constant"> <span class="comment">// ErrAmbigContext: "... appears in an ambiguous context within a URL"</span> </span> <span class="comment">// Example:</span> <span class="comment">// <a href="</span> <span class="comment">// {{if .C}}</span> <span class="comment">// /path/</span> <span class="comment">// {{else}}</span> <span class="comment">// /search?q=</span> <span class="comment">// {{end}}</span> <span class="comment">// {{.X}}</span> <span class="comment">// "></span> <span class="comment">// Discussion:</span> <span class="comment">// {{.X}} is in an ambiguous URL context since, depending on {{.C}},</span> <span class="comment">// it may be either a URL suffix or a query parameter.</span> <span class="comment">// Moving {{.X}} into the condition removes the ambiguity:</span> <span class="comment">// <a href="{{if .C}}/path/{{.X}}{{else}}/search?q={{.X}}"></span> ErrAmbigContext <span id="ErrBadHTML" data-kind="constant"> <span class="comment">// ErrBadHTML: "expected space, attr name, or end of tag, but got ...",</span> </span> <span class="comment">// "... in unquoted attr", "... in attribute name"</span> <span class="comment">// Example:</span> <span class="comment">// <a href = /search?q=foo></span> <span class="comment">// <href=foo></span> <span class="comment">// <form na<e=...></span> <span class="comment">// <option selected<</span> <span class="comment">// Discussion:</span> <span class="comment">// This is often due to a typo in an HTML element, but some runes</span> <span class="comment">// are banned in tag names, attribute names, and unquoted attribute</span> <span class="comment">// values because they can tickle parser ambiguities.</span> <span class="comment">// Quoting all attributes is the best policy.</span> ErrBadHTML <span id="ErrBranchEnd" data-kind="constant"> <span class="comment">// ErrBranchEnd: "{{if}} branches end in different contexts"</span> </span> <span class="comment">// Example:</span> <span class="comment">// {{if .C}}<a href="{{end}}{{.X}}</span> <span class="comment">// Discussion:</span> <span class="comment">// Package html/template statically examines each path through an</span> <span class="comment">// {{if}}, {{range}}, or {{with}} to escape any following pipelines.</span> <span class="comment">// The example is ambiguous since {{.X}} might be an HTML text node,</span> <span class="comment">// or a URL prefix in an HTML attribute. The context of {{.X}} is</span> <span class="comment">// used to figure out how to escape it, but that context depends on</span> <span class="comment">// the run-time value of {{.C}} which is not statically known.</span> <span class="comment">//</span> <span class="comment">// The problem is usually something like missing quotes or angle</span> <span class="comment">// brackets, or can be avoided by refactoring to put the two contexts</span> <span class="comment">// into different branches of an if, range or with. If the problem</span> <span class="comment">// is in a {{range}} over a collection that should never be empty,</span> <span class="comment">// adding a dummy {{else}} can help.</span> ErrBranchEnd <span id="ErrEndContext" data-kind="constant"> <span class="comment">// ErrEndContext: "... ends in a non-text context: ..."</span> </span> <span class="comment">// Examples:</span> <span class="comment">// <div</span> <span class="comment">// <div title="no close quote></span> <span class="comment">// <script>f()</span> <span class="comment">// Discussion:</span> <span class="comment">// Executed templates should produce a DocumentFragment of HTML.</span> <span class="comment">// Templates that end without closing tags will trigger this error.</span> <span class="comment">// Templates that should not be used in an HTML context or that</span> <span class="comment">// produce incomplete Fragments should not be executed directly.</span> <span class="comment">//</span> <span class="comment">// {{define "main"}} <script>{{template "helper"}}</script> {{end}}</span> <span class="comment">// {{define "helper"}} document.write(' <div title=" ') {{end}}</span> <span class="comment">//</span> <span class="comment">// "helper" does not produce a valid document fragment, so should</span> <span class="comment">// not be Executed directly.</span> ErrEndContext <span id="ErrNoSuchTemplate" data-kind="constant"> <span class="comment">// ErrNoSuchTemplate: "no such template ..."</span> </span> <span class="comment">// Examples:</span> <span class="comment">// {{define "main"}}<div {{template "attrs"}}>{{end}}</span> <span class="comment">// {{define "attrs"}}href="{{.URL}}"{{end}}</span> <span class="comment">// Discussion:</span> <span class="comment">// Package html/template looks through template calls to compute the</span> <span class="comment">// context.</span> <span class="comment">// Here the {{.URL}} in "attrs" must be treated as a URL when called</span> <span class="comment">// from "main", but you will get this error if "attrs" is not defined</span> <span class="comment">// when "main" is parsed.</span> ErrNoSuchTemplate <span id="ErrOutputContext" data-kind="constant"> <span class="comment">// ErrOutputContext: "cannot compute output context for template ..."</span> </span> <span class="comment">// Examples:</span> <span class="comment">// {{define "t"}}{{if .T}}{{template "t" .T}}{{end}}{{.H}}",{{end}}</span> <span class="comment">// Discussion:</span> <span class="comment">// A recursive template does not end in the same context in which it</span> <span class="comment">// starts, and a reliable output context cannot be computed.</span> <span class="comment">// Look for typos in the named template.</span> <span class="comment">// If the template should not be called in the named start context,</span> <span class="comment">// look for calls to that template in unexpected contexts.</span> <span class="comment">// Maybe refactor recursive templates to not be recursive.</span> ErrOutputContext <span id="ErrPartialCharset" data-kind="constant"> <span class="comment">// ErrPartialCharset: "unfinished JS regexp charset in ..."</span> </span> <span class="comment">// Example:</span> <span class="comment">// <script>var pattern = /foo[{{.Chars}}]/</script></span> <span class="comment">// Discussion:</span> <span class="comment">// Package html/template does not support interpolation into regular</span> <span class="comment">// expression literal character sets.</span> ErrPartialCharset <span id="ErrPartialEscape" data-kind="constant"> <span class="comment">// ErrPartialEscape: "unfinished escape sequence in ..."</span> </span> <span class="comment">// Example:</span> <span class="comment">// <script>alert("\{{.X}}")</script></span> <span class="comment">// Discussion:</span> <span class="comment">// Package html/template does not support actions following a</span> <span class="comment">// backslash.</span> <span class="comment">// This is usually an error and there are better solutions; for</span> <span class="comment">// example</span> <span class="comment">// <script>alert("{{.X}}")</script></span> <span class="comment">// should work, and if {{.X}} is a partial escape sequence such as</span> <span class="comment">// "xA0", mark the whole sequence as safe content: JSStr(`\xA0`)</span> ErrPartialEscape <span id="ErrRangeLoopReentry" data-kind="constant"> <span class="comment">// ErrRangeLoopReentry: "on range loop re-entry: ..."</span> </span> <span class="comment">// Example:</span> <span class="comment">// <script>var x = [{{range .}}'{{.}},{{end}}]</script></span> <span class="comment">// Discussion:</span> <span class="comment">// If an iteration through a range would cause it to end in a</span> <span class="comment">// different context than an earlier pass, there is no single context.</span> <span class="comment">// In the example, there is missing a quote, so it is not clear</span> <span class="comment">// whether {{.}} is meant to be inside a JS string or in a JS value</span> <span class="comment">// context. The second iteration would produce something like</span> <span class="comment">//</span> <span class="comment">// <script>var x = ['firstValue,'secondValue]</script></span> ErrRangeLoopReentry <span id="ErrSlashAmbig" data-kind="constant"> <span class="comment">// ErrSlashAmbig: '/' could start a division or regexp.</span> </span> <span class="comment">// Example:</span> <span class="comment">// <script></span> <span class="comment">// {{if .C}}var x = 1{{end}}</span> <span class="comment">// /-{{.N}}/i.test(x) ? doThis : doThat();</span> <span class="comment">// </script></span> <span class="comment">// Discussion:</span> <span class="comment">// The example above could produce `var x = 1/-2/i.test(s)...`</span> <span class="comment">// in which the first '/' is a mathematical division operator or it</span> <span class="comment">// could produce `/-2/i.test(s)` in which the first '/' starts a</span> <span class="comment">// regexp literal.</span> <span class="comment">// Look for missing semicolons inside branches, and maybe add</span> <span class="comment">// parentheses to make it clear which interpretation you intend.</span> ErrSlashAmbig <span id="ErrPredefinedEscaper" data-kind="constant"> <span class="comment">// ErrPredefinedEscaper: "predefined escaper ... disallowed in template"</span> </span> <span class="comment">// Example:</span> <span class="comment">// <div class={{. | html}}>Hello<div></span> <span class="comment">// Discussion:</span> <span class="comment">// Package html/template already contextually escapes all pipelines to</span> <span class="comment">// produce HTML output safe against code injection. Manually escaping</span> <span class="comment">// pipeline output using the predefined escapers "html" or "urlquery" is</span> <span class="comment">// unnecessary, and may affect the correctness or safety of the escaped</span> <span class="comment">// pipeline output in Go 1.8 and earlier.</span> <span class="comment">//</span> <span class="comment">// In most cases, such as the given example, this error can be resolved by</span> <span class="comment">// simply removing the predefined escaper from the pipeline and letting the</span> <span class="comment">// contextual autoescaper handle the escaping of the pipeline. In other</span> <span class="comment">// instances, where the predefined escaper occurs in the middle of a</span> <span class="comment">// pipeline where subsequent commands expect escaped input, e.g.</span> <span class="comment">// {{.X | html | makeALink}}</span> <span class="comment">// where makeALink does</span> <span class="comment">// return `<a href="`+input+`">link</a>`</span> <span class="comment">// consider refactoring the surrounding template to make use of the</span> <span class="comment">// contextual autoescaper, i.e.</span> <span class="comment">// <a href="{{.X}}">link</a></span> <span class="comment">//</span> <span class="comment">// To ease migration to Go 1.9 and beyond, "html" and "urlquery" will</span> <span class="comment">// continue to be allowed as the last command in a pipeline. However, if the</span> <span class="comment">// pipeline occurs in an unquoted attribute value context, "html" is</span> <span class="comment">// disallowed. Avoid using "html" and "urlquery" entirely in new templates.</span> ErrPredefinedEscaper <span id="ErrJSTemplate" data-kind="constant"> <span class="comment">// ErrJSTemplate: "... appears in a JS template literal"</span> </span> <span class="comment">// Example:</span> <span class="comment">// <script>var tmpl = `{{.Interp}}`</script></span> <span class="comment">// Discussion:</span> <span class="comment">// Package html/template does not support actions inside of JS template</span> <span class="comment">// literals.</span> <span class="comment">//</span> <span class="comment">// Deprecated: ErrJSTemplate is no longer returned when an action is present</span> <span class="comment">// in a JS template literal. Actions inside of JS template literals are now</span> <span class="comment">// escaped as expected.</span> ErrJSTemplate )</pre> </div> <p>We define codes for each error that manifests while escaping templates, but escaped templates may also fail at runtime. </p><p>Output: "ZgotmplZ" Example: </p><pre><img src="{{.X}}"> where {{.X}} evaluates to `javascript:...` </pre><p>Discussion: </p><pre>"ZgotmplZ" is a special value that indicates that unsafe content reached a CSS or URL context at runtime. The output of the example will be <img src="#ZgotmplZ"> If the data comes from a trusted source, use content types to exempt it from filtering: URL(`javascript:...`). </pre> </div> </div><div class="Documentation-type"> <h4 tabindex="-1" id="FuncMap" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=331">FuncMap</a> <a class="Documentation-idLink" href="#FuncMap" title="Go to FuncMap" aria-label="Go to FuncMap">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type FuncMap = <a href="/text/template">template</a>.<a href="/text/template#FuncMap">FuncMap</a></pre> </div> </div><div class="Documentation-type"> <h4 tabindex="-1" id="HTML" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/content.go;l=35">HTML</a> <a class="Documentation-idLink" href="#HTML" title="Go to HTML" aria-label="Go to HTML">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type HTML <a href="/builtin#string">string</a></pre> </div> <p>HTML encapsulates a known safe HTML document fragment. It should not be used for HTML from a third-party, or HTML with unclosed tags or comments. The outputs of a sound HTML sanitizer and a template escaped by this package are fine for use with HTML. </p><p>Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output. </p> </div><div class="Documentation-type"> <h4 tabindex="-1" id="HTMLAttr" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/content.go;l=43">HTMLAttr</a> <a class="Documentation-idLink" href="#HTMLAttr" title="Go to HTMLAttr" aria-label="Go to HTMLAttr">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type HTMLAttr <a href="/builtin#string">string</a></pre> </div> <p>HTMLAttr encapsulates an HTML attribute from a trusted source, for example, ` dir="ltr"`. </p><p>Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output. </p> </div><div class="Documentation-type"> <h4 tabindex="-1" id="JS" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/content.go;l=61">JS</a> <a class="Documentation-idLink" href="#JS" title="Go to JS" aria-label="Go to JS">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type JS <a href="/builtin#string">string</a></pre> </div> <p>JS encapsulates a known safe EcmaScript5 Expression, for example, `(x + y * z())`. Template authors are responsible for ensuring that typed expressions do not break the intended precedence and that there is no statement/expression ambiguity as when passing an expression like "{ foo: bar() }\n['foo']()", which is both a valid Expression and a valid Program with a very different meaning. </p><p>Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output. </p><p>Using JS to include valid but untrusted JSON is not safe. A safe alternative is to parse the JSON with json.Unmarshal and then pass the resultant object into the template, where it will be converted to sanitized JSON when presented in a JavaScript context. </p> </div><div class="Documentation-type"> <h4 tabindex="-1" id="JSStr" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/content.go;l=74">JSStr</a> <a class="Documentation-idLink" href="#JSStr" title="Go to JSStr" aria-label="Go to JSStr">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type JSStr <a href="/builtin#string">string</a></pre> </div> <p>JSStr encapsulates a sequence of characters meant to be embedded between quotes in a JavaScript expression. The string must match a series of StringCharacters: </p><pre>StringCharacter :: SourceCharacter but not `\` or LineTerminator | EscapeSequence </pre><p>Note that LineContinuations are not allowed. JSStr("foo\\nbar") is fine, but JSStr("foo\\\nbar") is not. </p><p>Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output. </p> </div><div class="Documentation-type"> <h4 tabindex="-1" id="Srcset" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/content.go;l=93">Srcset</a> <a class="Documentation-idLink" href="#Srcset" title="Go to Srcset" aria-label="Go to Srcset">露</a></span> <span class="Documentation-sinceVersion"> <span class="Documentation-sinceVersionLabel">added in</span> <span class="Documentation-sinceVersionVersion">go1.10</span> </span> </h4> <div class="Documentation-declaration"> <pre>type Srcset <a href="/builtin#string">string</a></pre> </div> <p>Srcset encapsulates a known safe srcset attribute (see <a href="https://w3c.github.io/html/semantics-embedded-content.html#element-attrdef-img-srcset">https://w3c.github.io/html/semantics-embedded-content.html#element-attrdef-img-srcset</a>). </p><p>Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output. </p> </div><div class="Documentation-type"> <h4 tabindex="-1" id="Template" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=21">Template</a> <a class="Documentation-idLink" href="#Template" title="Go to Template" aria-label="Go to Template">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type Template struct { <span id="Template.Tree" data-kind="field"> <span class="comment">// The underlying template's parse tree, updated to be HTML-safe.</span> </span> Tree *<a href="/text/template/parse">parse</a>.<a href="/text/template/parse#Tree">Tree</a> <span class="comment">// contains filtered or unexported fields</span> }</pre> </div> <p>Template is a specialized Template from "text/template" that produces a safe HTML document fragment. </p> <details tabindex="-1" id="example-Template-Block" class="Documentation-exampleDetails js-exampleContainer"> <summary class="Documentation-exampleDetailsHeader">Example (Block) <a href="#example-Template-Block" title="Go to Example (Block)" aria-label="Go to Example (Block)">露</a></summary> <div class="Documentation-exampleDetailsBody"> <pre class="Documentation-exampleCode"> package main import ( "html/template" "log" "os" "strings" ) func main() { const ( master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}` overlay = `{{define "list"}} {{join . ", "}}{{end}} ` ) var ( funcs = template.FuncMap{"join": strings.Join} guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"} ) masterTmpl, err := template.New("master").Funcs(funcs).Parse(master) if err != nil { log.Fatal(err) } overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay) if err != nil { log.Fatal(err) } if err := masterTmpl.Execute(os.Stdout, guardians); err != nil { log.Fatal(err) } if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil { log.Fatal(err) } } </pre> <pre><span class="Documentation-exampleOutputLabel">Output:</span> <span class="Documentation-exampleOutput">Names: - Gamora - Groot - Nebula - Rocket - Star-Lord Names: Gamora, Groot, Nebula, Rocket, Star-Lord </span></pre> </div> <div class="Documentation-exampleButtonsContainer"> <p class="Documentation-exampleError" role="alert" aria-atomic="true"></p> <button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button> <button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button> <button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button> </div></details> <details tabindex="-1" id="example-Template-Glob" class="Documentation-exampleDetails js-exampleContainer"> <summary class="Documentation-exampleDetailsHeader">Example (Glob) <a href="#example-Template-Glob" title="Go to Example (Glob)" aria-label="Go to Example (Glob)">露</a></summary> <div class="Documentation-exampleDetailsBody"> <p>Here we demonstrate loading a set of templates from a directory. </p> <pre class="Documentation-exampleCode"> package main import ( "io" "log" "os" "path/filepath" "text/template" ) // templateFile defines the contents of a template to be stored in a file, for testing. type templateFile struct { name string contents string } func createTestDir(files []templateFile) string { dir, err := os.MkdirTemp("", "template") if err != nil { log.Fatal(err) } for _, file := range files { f, err := os.Create(filepath.Join(dir, file.name)) if err != nil { log.Fatal(err) } defer f.Close() _, err = io.WriteString(f, file.contents) if err != nil { log.Fatal(err) } } return dir } func main() { // Here we create a temporary directory and populate it with our sample // template definition files; usually the template files would already // exist in some location known to the program. dir := createTestDir([]templateFile{ // T0.tmpl is a plain template file that just invokes T1. {"T0.tmpl", `T0 invokes T1: ({{template "T1"}})`}, // T1.tmpl defines a template, T1 that invokes T2. {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`}, // T2.tmpl defines a template T2. {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`}, }) // Clean up after the test; another quirk of running as an example. defer os.RemoveAll(dir) // pattern is the glob pattern used to find all the template files. pattern := filepath.Join(dir, "*.tmpl") // Here starts the example proper. // T0.tmpl is the first name matched, so it becomes the starting template, // the value returned by ParseGlob. tmpl := template.Must(template.ParseGlob(pattern)) err := tmpl.Execute(os.Stdout, nil) if err != nil { log.Fatalf("template execution: %s", err) } } </pre> <pre><span class="Documentation-exampleOutputLabel">Output:</span> <span class="Documentation-exampleOutput">T0 invokes T1: (T1 invokes T2: (This is T2)) </span></pre> </div> <div class="Documentation-exampleButtonsContainer"> <p class="Documentation-exampleError" role="alert" aria-atomic="true"></p> <button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button> <button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button> <button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button> </div></details> <details tabindex="-1" id="example-Template-Helpers" class="Documentation-exampleDetails js-exampleContainer"> <summary class="Documentation-exampleDetailsHeader">Example (Helpers) <a href="#example-Template-Helpers" title="Go to Example (Helpers)" aria-label="Go to Example (Helpers)">露</a></summary> <div class="Documentation-exampleDetailsBody"> <p>This example demonstrates one way to share some templates and use them in different contexts. In this variant we add multiple driver templates by hand to an existing bundle of templates. </p> <pre class="Documentation-exampleCode"> package main import ( "io" "log" "os" "path/filepath" "text/template" ) // templateFile defines the contents of a template to be stored in a file, for testing. type templateFile struct { name string contents string } func createTestDir(files []templateFile) string { dir, err := os.MkdirTemp("", "template") if err != nil { log.Fatal(err) } for _, file := range files { f, err := os.Create(filepath.Join(dir, file.name)) if err != nil { log.Fatal(err) } defer f.Close() _, err = io.WriteString(f, file.contents) if err != nil { log.Fatal(err) } } return dir } func main() { // Here we create a temporary directory and populate it with our sample // template definition files; usually the template files would already // exist in some location known to the program. dir := createTestDir([]templateFile{ // T1.tmpl defines a template, T1 that invokes T2. {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`}, // T2.tmpl defines a template T2. {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`}, }) // Clean up after the test; another quirk of running as an example. defer os.RemoveAll(dir) // pattern is the glob pattern used to find all the template files. pattern := filepath.Join(dir, "*.tmpl") // Here starts the example proper. // Load the helpers. templates := template.Must(template.ParseGlob(pattern)) // Add one driver template to the bunch; we do this with an explicit template definition. _, err := templates.Parse("{{define `driver1`}}Driver 1 calls T1: ({{template `T1`}})\n{{end}}") if err != nil { log.Fatal("parsing driver1: ", err) } // Add another driver template. _, err = templates.Parse("{{define `driver2`}}Driver 2 calls T2: ({{template `T2`}})\n{{end}}") if err != nil { log.Fatal("parsing driver2: ", err) } // We load all the templates before execution. This package does not require // that behavior but html/template's escaping does, so it's a good habit. err = templates.ExecuteTemplate(os.Stdout, "driver1", nil) if err != nil { log.Fatalf("driver1 execution: %s", err) } err = templates.ExecuteTemplate(os.Stdout, "driver2", nil) if err != nil { log.Fatalf("driver2 execution: %s", err) } } </pre> <pre><span class="Documentation-exampleOutputLabel">Output:</span> <span class="Documentation-exampleOutput">Driver 1 calls T1: (T1 invokes T2: (This is T2)) Driver 2 calls T2: (This is T2) </span></pre> </div> <div class="Documentation-exampleButtonsContainer"> <p class="Documentation-exampleError" role="alert" aria-atomic="true"></p> <button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button> <button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button> <button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button> </div></details> <details tabindex="-1" id="example-Template-Parsefiles" class="Documentation-exampleDetails js-exampleContainer"> <summary class="Documentation-exampleDetailsHeader">Example (Parsefiles) <a href="#example-Template-Parsefiles" title="Go to Example (Parsefiles)" aria-label="Go to Example (Parsefiles)">露</a></summary> <div class="Documentation-exampleDetailsBody"> <p>Here we demonstrate loading a set of templates from files in different directories </p> <pre class="Documentation-exampleCode"> package main import ( "io" "log" "os" "path/filepath" "text/template" ) // templateFile defines the contents of a template to be stored in a file, for testing. type templateFile struct { name string contents string } func createTestDir(files []templateFile) string { dir, err := os.MkdirTemp("", "template") if err != nil { log.Fatal(err) } for _, file := range files { f, err := os.Create(filepath.Join(dir, file.name)) if err != nil { log.Fatal(err) } defer f.Close() _, err = io.WriteString(f, file.contents) if err != nil { log.Fatal(err) } } return dir } func main() { // Here we create different temporary directories and populate them with our sample // template definition files; usually the template files would already // exist in some location known to the program. dir1 := createTestDir([]templateFile{ // T1.tmpl is a plain template file that just invokes T2. {"T1.tmpl", `T1 invokes T2: ({{template "T2"}})`}, }) dir2 := createTestDir([]templateFile{ // T2.tmpl defines a template T2. {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`}, }) // Clean up after the test; another quirk of running as an example. defer func(dirs ...string) { for _, dir := range dirs { os.RemoveAll(dir) } }(dir1, dir2) // Here starts the example proper. // Let's just parse only dir1/T0 and dir2/T2 paths := []string{ filepath.Join(dir1, "T1.tmpl"), filepath.Join(dir2, "T2.tmpl"), } tmpl := template.Must(template.ParseFiles(paths...)) err := tmpl.Execute(os.Stdout, nil) if err != nil { log.Fatalf("template execution: %s", err) } } </pre> <pre><span class="Documentation-exampleOutputLabel">Output:</span> <span class="Documentation-exampleOutput">T1 invokes T2: (This is T2) </span></pre> </div> <div class="Documentation-exampleButtonsContainer"> <p class="Documentation-exampleError" role="alert" aria-atomic="true"></p> <button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button> <button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button> <button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button> </div></details> <details tabindex="-1" id="example-Template-Share" class="Documentation-exampleDetails js-exampleContainer"> <summary class="Documentation-exampleDetailsHeader">Example (Share) <a href="#example-Template-Share" title="Go to Example (Share)" aria-label="Go to Example (Share)">露</a></summary> <div class="Documentation-exampleDetailsBody"> <p>This example demonstrates how to use one group of driver templates with distinct sets of helper templates. </p> <pre class="Documentation-exampleCode"> package main import ( "io" "log" "os" "path/filepath" "text/template" ) // templateFile defines the contents of a template to be stored in a file, for testing. type templateFile struct { name string contents string } func createTestDir(files []templateFile) string { dir, err := os.MkdirTemp("", "template") if err != nil { log.Fatal(err) } for _, file := range files { f, err := os.Create(filepath.Join(dir, file.name)) if err != nil { log.Fatal(err) } defer f.Close() _, err = io.WriteString(f, file.contents) if err != nil { log.Fatal(err) } } return dir } func main() { // Here we create a temporary directory and populate it with our sample // template definition files; usually the template files would already // exist in some location known to the program. dir := createTestDir([]templateFile{ // T0.tmpl is a plain template file that just invokes T1. {"T0.tmpl", "T0 ({{.}} version) invokes T1: ({{template `T1`}})\n"}, // T1.tmpl defines a template, T1 that invokes T2. Note T2 is not defined {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`}, }) // Clean up after the test; another quirk of running as an example. defer os.RemoveAll(dir) // pattern is the glob pattern used to find all the template files. pattern := filepath.Join(dir, "*.tmpl") // Here starts the example proper. // Load the drivers. drivers := template.Must(template.ParseGlob(pattern)) // We must define an implementation of the T2 template. First we clone // the drivers, then add a definition of T2 to the template name space. // 1. Clone the helper set to create a new name space from which to run them. first, err := drivers.Clone() if err != nil { log.Fatal("cloning helpers: ", err) } // 2. Define T2, version A, and parse it. _, err = first.Parse("{{define `T2`}}T2, version A{{end}}") if err != nil { log.Fatal("parsing T2: ", err) } // Now repeat the whole thing, using a different version of T2. // 1. Clone the drivers. second, err := drivers.Clone() if err != nil { log.Fatal("cloning drivers: ", err) } // 2. Define T2, version B, and parse it. _, err = second.Parse("{{define `T2`}}T2, version B{{end}}") if err != nil { log.Fatal("parsing T2: ", err) } // Execute the templates in the reverse order to verify the // first is unaffected by the second. err = second.ExecuteTemplate(os.Stdout, "T0.tmpl", "second") if err != nil { log.Fatalf("second execution: %s", err) } err = first.ExecuteTemplate(os.Stdout, "T0.tmpl", "first") if err != nil { log.Fatalf("first: execution: %s", err) } } </pre> <pre><span class="Documentation-exampleOutputLabel">Output:</span> <span class="Documentation-exampleOutput">T0 (second version) invokes T1: (T1 invokes T2: (T2, version B)) T0 (first version) invokes T1: (T1 invokes T2: (T2, version A)) </span></pre> </div> <div class="Documentation-exampleButtonsContainer"> <p class="Documentation-exampleError" role="alert" aria-atomic="true"></p> <button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button> <button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button> <button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button> </div></details> <div class="Documentation-typeFunc"> <h4 tabindex="-1" id="Must" data-kind="function" class="Documentation-typeFuncHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=366">Must</a> <a class="Documentation-idLink" href="#Must" title="Go to Must" aria-label="Go to Must">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func Must(t *<a href="#Template">Template</a>, err <a href="/builtin#error">error</a>) *<a href="#Template">Template</a></pre> </div> <p>Must is a helper that wraps a call to a function returning (<a href="#Template">*Template</a>, error) and panics if the error is non-nil. It is intended for use in variable initializations such as </p><pre>var t = template.Must(template.New("name").Parse("html")) </pre> </div><div class="Documentation-typeFunc"> <h4 tabindex="-1" id="New" data-kind="function" class="Documentation-typeFuncHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=284">New</a> <a class="Documentation-idLink" href="#New" title="Go to New" aria-label="Go to New">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func New(name <a href="/builtin#string">string</a>) *<a href="#Template">Template</a></pre> </div> <p>New allocates a new HTML template with the given name. </p> </div><div class="Documentation-typeFunc"> <h4 tabindex="-1" id="ParseFS" data-kind="function" class="Documentation-typeFuncHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=491">ParseFS</a> <a class="Documentation-idLink" href="#ParseFS" title="Go to ParseFS" aria-label="Go to ParseFS">露</a></span> <span class="Documentation-sinceVersion"> <span class="Documentation-sinceVersionLabel">added in</span> <span class="Documentation-sinceVersionVersion">go1.16</span> </span> </h4> <div class="Documentation-declaration"> <pre>func ParseFS(fs <a href="/io/fs">fs</a>.<a href="/io/fs#FS">FS</a>, patterns ...<a href="/builtin#string">string</a>) (*<a href="#Template">Template</a>, <a href="/builtin#error">error</a>)</pre> </div> <p>ParseFS is like <a href="#ParseFiles">ParseFiles</a> or <a href="#ParseGlob">ParseGlob</a> but reads from the file system fs instead of the host operating system's file system. It accepts a list of glob patterns. (Note that most file names serve as glob patterns matching only themselves.) </p> </div><div class="Documentation-typeFunc"> <h4 tabindex="-1" id="ParseFiles" data-kind="function" class="Documentation-typeFuncHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=382">ParseFiles</a> <a class="Documentation-idLink" href="#ParseFiles" title="Go to ParseFiles" aria-label="Go to ParseFiles">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func ParseFiles(filenames ...<a href="/builtin#string">string</a>) (*<a href="#Template">Template</a>, <a href="/builtin#error">error</a>)</pre> </div> <p>ParseFiles creates a new <a href="#Template">Template</a> and parses the template definitions from the named files. The returned template's name will have the (base) name and (parsed) contents of the first file. There must be at least one file. If an error occurs, parsing stops and the returned <a href="#Template">*Template</a> is nil. </p><p>When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results. For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template named "foo", while "a/foo" is unavailable. </p> </div><div class="Documentation-typeFunc"> <h4 tabindex="-1" id="ParseGlob" data-kind="function" class="Documentation-typeFuncHeader"> <span>func <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=447">ParseGlob</a> <a class="Documentation-idLink" href="#ParseGlob" title="Go to ParseGlob" aria-label="Go to ParseGlob">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func ParseGlob(pattern <a href="/builtin#string">string</a>) (*<a href="#Template">Template</a>, <a href="/builtin#error">error</a>)</pre> </div> <p>ParseGlob creates a new <a href="#Template">Template</a> and parses the template definitions from the files identified by the pattern. The files are matched according to the semantics of filepath.Match, and the pattern must match at least one file. The returned template will have the (base) name and (parsed) contents of the first file matched by the pattern. ParseGlob is equivalent to calling <a href="#ParseFiles">ParseFiles</a> with the list of files matched by the pattern. </p><p>When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.AddParseTree" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=217">AddParseTree</a> <a class="Documentation-idLink" href="#Template.AddParseTree" title="Go to Template.AddParseTree" aria-label="Go to Template.AddParseTree">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) AddParseTree(name <a href="/builtin#string">string</a>, tree *<a href="/text/template/parse">parse</a>.<a href="/text/template/parse#Tree">Tree</a>) (*<a href="#Template">Template</a>, <a href="/builtin#error">error</a>)</pre> </div> <p>AddParseTree creates a new template with the name and parse tree and associates it with t. </p><p>It returns an error if t or any associated template has already been executed. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.Clone" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=246">Clone</a> <a class="Documentation-idLink" href="#Template.Clone" title="Go to Template.Clone" aria-label="Go to Template.Clone">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) Clone() (*<a href="#Template">Template</a>, <a href="/builtin#error">error</a>)</pre> </div> <p>Clone returns a duplicate of the template, including all associated templates. The actual representation is not copied, but the name space of associated templates is, so further calls to <a href="#Template.Parse">Template.Parse</a> in the copy will add templates to the copy but not to the original. <a href="#Template.Clone">Template.Clone</a> can be used to prepare common templates and use them with variant definitions for other templates by adding the variants after the clone is made. </p><p>It returns an error if t has already been executed. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.DefinedTemplates" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=171">DefinedTemplates</a> <a class="Documentation-idLink" href="#Template.DefinedTemplates" title="Go to Template.DefinedTemplates" aria-label="Go to Template.DefinedTemplates">露</a></span> <span class="Documentation-sinceVersion"> <span class="Documentation-sinceVersionLabel">added in</span> <span class="Documentation-sinceVersionVersion">go1.6</span> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) DefinedTemplates() <a href="/builtin#string">string</a></pre> </div> <p>DefinedTemplates returns a string listing the defined templates, prefixed by the string "; defined templates are: ". If there are none, it returns the empty string. Used to generate an error message. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.Delims" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=348">Delims</a> <a class="Documentation-idLink" href="#Template.Delims" title="Go to Template.Delims" aria-label="Go to Template.Delims">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) Delims(left, right <a href="/builtin#string">string</a>) *<a href="#Template">Template</a></pre> </div> <p>Delims sets the action delimiters to the specified strings, to be used in subsequent calls to <a href="#Template.Parse">Template.Parse</a>, <a href="#ParseFiles">ParseFiles</a>, or <a href="#ParseGlob">ParseGlob</a>. Nested template definitions will inherit the settings. An empty delimiter stands for the corresponding default: {{ or }}. The return value is the template, so calls can be chained. </p> <details tabindex="-1" id="example-Template.Delims" class="Documentation-exampleDetails js-exampleContainer"> <summary class="Documentation-exampleDetailsHeader">Example <a href="#example-Template.Delims" title="Go to Example" aria-label="Go to Example">露</a></summary> <div class="Documentation-exampleDetailsBody"> <pre class="Documentation-exampleCode"> package main import ( "html/template" "log" "os" ) func main() { const text = "<<.Greeting>> {{.Name}}" data := struct { Greeting string Name string }{ Greeting: "Hello", Name: "Joe", } t := template.Must(template.New("tpl").Delims("<<", ">>").Parse(text)) err := t.Execute(os.Stdout, data) if err != nil { log.Fatal(err) } } </pre> <pre><span class="Documentation-exampleOutputLabel">Output:</span> <span class="Documentation-exampleOutput">Hello {{.Name}} </span></pre> </div> <div class="Documentation-exampleButtonsContainer"> <p class="Documentation-exampleError" role="alert" aria-atomic="true"></p> <button class="Documentation-exampleShareButton" aria-label="Share Code">Share</button> <button class="Documentation-exampleFormatButton" aria-label="Format Code">Format</button> <button class="Documentation-exampleRunButton" aria-label="Run Code">Run</button> </div></details> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.Execute" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=120">Execute</a> <a class="Documentation-idLink" href="#Template.Execute" title="Go to Template.Execute" aria-label="Go to Template.Execute">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) Execute(wr <a href="/io">io</a>.<a href="/io#Writer">Writer</a>, data <a href="/builtin#any">any</a>) <a href="/builtin#error">error</a></pre> </div> <p>Execute applies a parsed template to the specified data object, writing the output to wr. If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer. A template may be executed safely in parallel, although if parallel executions share a Writer the output may be interleaved. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.ExecuteTemplate" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=134">ExecuteTemplate</a> <a class="Documentation-idLink" href="#Template.ExecuteTemplate" title="Go to Template.ExecuteTemplate" aria-label="Go to Template.ExecuteTemplate">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) ExecuteTemplate(wr <a href="/io">io</a>.<a href="/io#Writer">Writer</a>, name <a href="/builtin#string">string</a>, data <a href="/builtin#any">any</a>) <a href="/builtin#error">error</a></pre> </div> <p>ExecuteTemplate applies the template associated with t that has the given name to the specified data object and writes the output to wr. If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer. A template may be executed safely in parallel, although if parallel executions share a Writer the output may be interleaved. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.Funcs" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=338">Funcs</a> <a class="Documentation-idLink" href="#Template.Funcs" title="Go to Template.Funcs" aria-label="Go to Template.Funcs">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) Funcs(funcMap <a href="#FuncMap">FuncMap</a>) *<a href="#Template">Template</a></pre> </div> <p>Funcs adds the elements of the argument map to the template's function map. It must be called before the template is parsed. It panics if a value in the map is not a function with appropriate return type. However, it is legal to overwrite elements of the map. The return value is the template, so calls can be chained. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.Lookup" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=355">Lookup</a> <a class="Documentation-idLink" href="#Template.Lookup" title="Go to Template.Lookup" aria-label="Go to Template.Lookup">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) Lookup(name <a href="/builtin#string">string</a>) *<a href="#Template">Template</a></pre> </div> <p>Lookup returns the template with the given name that is associated with t, or nil if there is no such template. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.Name" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=327">Name</a> <a class="Documentation-idLink" href="#Template.Name" title="Go to Template.Name" aria-label="Go to Template.Name">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) Name() <a href="/builtin#string">string</a></pre> </div> <p>Name returns the name of the template. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.New" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=304">New</a> <a class="Documentation-idLink" href="#Template.New" title="Go to Template.New" aria-label="Go to Template.New">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) New(name <a href="/builtin#string">string</a>) *<a href="#Template">Template</a></pre> </div> <p>New allocates a new HTML template associated with the given one and with the same delimiters. The association, which is transitive, allows one template to invoke another with a {{template}} action. </p><p>If a template with the given name already exists, the new HTML template will replace it. The existing template will be reset and disassociated with t. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.Option" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=76">Option</a> <a class="Documentation-idLink" href="#Template.Option" title="Go to Template.Option" aria-label="Go to Template.Option">露</a></span> <span class="Documentation-sinceVersion"> <span class="Documentation-sinceVersionLabel">added in</span> <span class="Documentation-sinceVersionVersion">go1.5</span> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) Option(opt ...<a href="/builtin#string">string</a>) *<a href="#Template">Template</a></pre> </div> <p>Option sets options for the template. Options are described by strings, either a simple string or "key=value". There can be at most one equals sign in an option string. If the option string is unrecognized or otherwise invalid, Option panics. </p><p>Known options: </p><p>missingkey: Control the behavior during execution if a map is indexed with a key that is not present in the map. </p><pre>"missingkey=default" or "missingkey=invalid" The default behavior: Do nothing and continue execution. If printed, the result of the index operation is the string "<no value>". "missingkey=zero" The operation returns the zero value for the map type's element. "missingkey=error" Execution stops immediately with an error. </pre> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.Parse" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=186">Parse</a> <a class="Documentation-idLink" href="#Template.Parse" title="Go to Template.Parse" aria-label="Go to Template.Parse">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) Parse(text <a href="/builtin#string">string</a>) (*<a href="#Template">Template</a>, <a href="/builtin#error">error</a>)</pre> </div> <p>Parse parses text as a template body for t. Named template definitions ({{define ...}} or {{block ...}} statements) in text define additional templates associated with t and are removed from the definition of t itself. </p><p>Templates can be redefined in successive calls to Parse, before the first use of <a href="#Template.Execute">Template.Execute</a> on t or any associated template. A template definition with a body containing only white space and comments is considered empty and will not replace an existing template's body. This allows using Parse to add new named template definitions without overwriting the main template body. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.ParseFS" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=499">ParseFS</a> <a class="Documentation-idLink" href="#Template.ParseFS" title="Go to Template.ParseFS" aria-label="Go to Template.ParseFS">露</a></span> <span class="Documentation-sinceVersion"> <span class="Documentation-sinceVersionLabel">added in</span> <span class="Documentation-sinceVersionVersion">go1.16</span> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) ParseFS(fs <a href="/io/fs">fs</a>.<a href="/io/fs#FS">FS</a>, patterns ...<a href="/builtin#string">string</a>) (*<a href="#Template">Template</a>, <a href="/builtin#error">error</a>)</pre> </div> <p>ParseFS is like <a href="#Template.ParseFiles">Template.ParseFiles</a> or <a href="#Template.ParseGlob">Template.ParseGlob</a> but reads from the file system fs instead of the host operating system's file system. It accepts a list of glob patterns. (Note that most file names serve as glob patterns matching only themselves.) </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.ParseFiles" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=394">ParseFiles</a> <a class="Documentation-idLink" href="#Template.ParseFiles" title="Go to Template.ParseFiles" aria-label="Go to Template.ParseFiles">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) ParseFiles(filenames ...<a href="/builtin#string">string</a>) (*<a href="#Template">Template</a>, <a href="/builtin#error">error</a>)</pre> </div> <p>ParseFiles parses the named files and associates the resulting templates with t. If an error occurs, parsing stops and the returned template is nil; otherwise it is t. There must be at least one file. </p><p>When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results. </p><p>ParseFiles returns an error if t or any associated template has already been executed. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.ParseGlob" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=461">ParseGlob</a> <a class="Documentation-idLink" href="#Template.ParseGlob" title="Go to Template.ParseGlob" aria-label="Go to Template.ParseGlob">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) ParseGlob(pattern <a href="/builtin#string">string</a>) (*<a href="#Template">Template</a>, <a href="/builtin#error">error</a>)</pre> </div> <p>ParseGlob parses the template definitions in the files identified by the pattern and associates the resulting templates with t. The files are matched according to the semantics of filepath.Match, and the pattern must match at least one file. ParseGlob is equivalent to calling t.ParseFiles with the list of files matched by the pattern. </p><p>When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results. </p><p>ParseGlob returns an error if t or any associated template has already been executed. </p> </div><div class="Documentation-typeMethod"> <h4 tabindex="-1" id="Template.Templates" data-kind="method" class="Documentation-typeMethodHeader"> <span>func (*Template) <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go;l=46">Templates</a> <a class="Documentation-idLink" href="#Template.Templates" title="Go to Template.Templates" aria-label="Go to Template.Templates">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>func (t *<a href="#Template">Template</a>) Templates() []*<a href="#Template">Template</a></pre> </div> <p>Templates returns a slice of the templates associated with t, including t itself. </p> </div> </div><div class="Documentation-type"> <h4 tabindex="-1" id="URL" data-kind="type" class="Documentation-typeHeader"> <span>type <a class="Documentation-source" href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/content.go;l=85">URL</a> <a class="Documentation-idLink" href="#URL" title="Go to URL" aria-label="Go to URL">露</a></span> <span class="Documentation-sinceVersion"> </span> </h4> <div class="Documentation-declaration"> <pre>type URL <a href="/builtin#string">string</a></pre> </div> <p>URL encapsulates a known safe URL or URL substring (see <a href="https://rfc-editor.org/rfc/rfc3986.html">RFC 3986</a>). A URL like `javascript:checkThatFormNotEditedBeforeLeavingPage()` from a trusted source should go in the page, but by default dynamic `javascript:` URLs are filtered out since they are a frequently exploited injection vector. </p><p>Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output. </p> </div></section></div> </div> </div> <div class="UnitFiles js-unitFiles"> <h2 class="UnitFiles-title" id="section-sourcefiles"> <img class="go-Icon" height="24" width="24" src="/static/shared/icon/insert_drive_file_gm_grey_24dp.svg" alt=""> Source Files <a class="UnitFiles-idLink" href="#section-sourcefiles" title="Go to Source Files" aria-label="Go to Source Files">露</a> </h2> <div class="UnitFiles-titleLink"> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template" target="_blank" rel="noopener">View all Source files</a> </div> <div> <ul class="UnitFiles-fileList"><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/attr.go" target="_blank" rel="noopener" title="attr.go">attr.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/attr_string.go" target="_blank" rel="noopener" title="attr_string.go">attr_string.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/content.go" target="_blank" rel="noopener" title="content.go">content.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/context.go" target="_blank" rel="noopener" title="context.go">context.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/css.go" target="_blank" rel="noopener" title="css.go">css.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/delim_string.go" target="_blank" rel="noopener" title="delim_string.go">delim_string.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/doc.go" target="_blank" rel="noopener" title="doc.go">doc.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/element_string.go" target="_blank" rel="noopener" title="element_string.go">element_string.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/error.go" target="_blank" rel="noopener" title="error.go">error.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/escape.go" target="_blank" rel="noopener" title="escape.go">escape.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/html.go" target="_blank" rel="noopener" title="html.go">html.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/js.go" target="_blank" rel="noopener" title="js.go">js.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/jsctx_string.go" target="_blank" rel="noopener" title="jsctx_string.go">jsctx_string.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/state_string.go" target="_blank" rel="noopener" title="state_string.go">state_string.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/template.go" target="_blank" rel="noopener" title="template.go">template.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/transition.go" target="_blank" rel="noopener" title="transition.go">transition.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/url.go" target="_blank" rel="noopener" title="url.go">url.go</a> </li><li> <a href="https://cs.opensource.google/go/go/+/go1.24.0:src/html/template/urlpart_string.go" target="_blank" rel="noopener" title="urlpart_string.go">urlpart_string.go</a> </li></ul> </div> </div> </div> </div> <div id="showInternal-description" hidden> Click to show internal directories. </div> <div id="hideInternal-description" hidden> Click to hide internal directories. </div> </article> <footer class="go-Main-footer"></footer> </main> <footer class="go-Footer"> <div class="go-Footer-links"> <div class="go-Footer-linkColumn"> <a href="https://go.dev/solutions" class="go-Footer-link go-Footer-link--primary" data-gtmc="footer link"> Why Go </a> <a href="https://go.dev/solutions#use-cases" class="go-Footer-link" data-gtmc="footer link"> Use Cases </a> <a href="https://go.dev/solutions#case-studies" class="go-Footer-link" data-gtmc="footer link"> Case Studies </a> </div> <div class="go-Footer-linkColumn"> <a href="https://learn.go.dev/" class="go-Footer-link go-Footer-link--primary" data-gtmc="footer link"> Get Started </a> <a href="https://play.golang.org" class="go-Footer-link" data-gtmc="footer link"> Playground </a> <a href="https://tour.golang.org" class="go-Footer-link" data-gtmc="footer link"> Tour </a> <a href="https://stackoverflow.com/questions/tagged/go?tab=Newest" class="go-Footer-link" data-gtmc="footer link"> Stack Overflow </a> <a href="https://go.dev/help" class="go-Footer-link" data-gtmc="footer link"> Help </a> </div> <div class="go-Footer-linkColumn"> <a href="https://pkg.go.dev" class="go-Footer-link go-Footer-link--primary" data-gtmc="footer link"> Packages </a> <a href="/std" class="go-Footer-link" data-gtmc="footer link"> Standard Library </a> <a href="/golang.org/x" class="go-Footer-link" data-gtmc="footer link"> Sub-repositories </a> <a href="https://pkg.go.dev/about" class="go-Footer-link" data-gtmc="footer link"> About Go Packages </a> </div> <div class="go-Footer-linkColumn"> <a href="https://go.dev/project" class="go-Footer-link go-Footer-link--primary" data-gtmc="footer link"> About </a> <a href="https://go.dev/dl/" class="go-Footer-link" data-gtmc="footer link">Download</a> <a href="https://go.dev/blog" class="go-Footer-link" data-gtmc="footer link">Blog</a> <a href="https://github.com/golang/go/issues" class="go-Footer-link" data-gtmc="footer link"> Issue Tracker </a> <a href="https://go.dev/doc/devel/release.html" class="go-Footer-link" data-gtmc="footer link"> Release Notes </a> <a href="https://blog.golang.org/go-brand" class="go-Footer-link" data-gtmc="footer link"> Brand Guidelines </a> <a href="https://go.dev/conduct" class="go-Footer-link" data-gtmc="footer link"> Code of Conduct </a> </div> <div class="go-Footer-linkColumn"> <a href="https://www.twitter.com/golang" class="go-Footer-link go-Footer-link--primary" data-gtmc="footer link"> Connect </a> <a href="https://www.twitter.com/golang" class="go-Footer-link" data-gtmc="footer link"> Twitter </a> <a href="https://github.com/golang" class="go-Footer-link" data-gtmc="footer link">GitHub</a> <a href="https://invite.slack.golangbridge.org/" class="go-Footer-link" data-gtmc="footer link"> Slack </a> <a href="https://reddit.com/r/golang" class="go-Footer-link" data-gtmc="footer link"> r/golang </a> <a href="https://www.meetup.com/pro/go" class="go-Footer-link" data-gtmc="footer link"> Meetup </a> <a href="https://golangweekly.com/" class="go-Footer-link" data-gtmc="footer link"> Golang Weekly </a> </div> </div> <div class="go-Footer-bottom"> <img class="go-Footer-gopher" width="1431" height="901" src="/static/shared/gopher/pilot-bust-1431x901.svg" alt="Gopher in flight goggles"> <ul class="go-Footer-listRow"> <li class="go-Footer-listItem"> <a href="https://go.dev/copyright" data-gtmc="footer link">Copyright</a> </li> <li class="go-Footer-listItem"> <a href="https://go.dev/tos" data-gtmc="footer link">Terms of Service</a> </li> <li class="go-Footer-listItem"> <a href="http://www.google.com/intl/en/policies/privacy/" data-gtmc="footer link" target="_blank" rel="noopener"> Privacy Policy </a> </li> <li class="go-Footer-listItem"> <a href="https://go.dev/s/pkgsite-feedback" target="_blank" rel="noopener" data-gtmc="footer link"> Report an Issue </a> </li> <li class="go-Footer-listItem"> <button class="go-Button go-Button--text go-Footer-toggleTheme js-toggleTheme" aria-label="Theme Toggle"> <img data-value="auto" class="go-Icon go-Icon--inverted" height="24" width="24" src="/static/shared/icon/brightness_6_gm_grey_24dp.svg" alt="System theme"> <img data-value="dark" class="go-Icon go-Icon--inverted" height="24" width="24" src="/static/shared/icon/brightness_2_gm_grey_24dp.svg" alt="Dark theme"> <img data-value="light" class="go-Icon go-Icon--inverted" height="24" width="24" src="/static/shared/icon/light_mode_gm_grey_24dp.svg" alt="Light theme"> <p> Theme Toggle </p> </button> </li> <li class="go-Footer-listItem"> <button class="go-Button go-Button--text go-Footer-keyboard js-openShortcuts" aria-label="Shorcuts Modal"> <img class="go-Icon go-Icon--inverted" height="24" width="24" src="/static/shared/icon/keyboard_grey_24dp.svg" alt=""> <p> Shortcuts Modal </p> </button> </li> </ul> <a class="go-Footer-googleLogo" href="https://google.com" target="_blank"rel="noopener" data-gtmc="footer link"> <img class="go-Footer-googleLogoImg" height="24" width="72" src="/static/shared/logo/google-white.svg" alt="Google logo"> </a> </div> </footer> <dialog id="jump-to-modal" class="JumpDialog go-Modal go-Modal--md js-modal"> <form method="dialog" data-gmtc="jump to form" aria-label="Jump to Identifier"> <div class="Dialog-title go-Modal-header"> <h2>Jump to</h2> <button class="go-Button go-Button--inline" type="button" data-modal-close data-gtmc="modal button" aria-label="Close" > <img class="go-Icon" height="24" width="24" src="/static/shared/icon/close_gm_grey_24dp.svg" alt="" /> </button> </div> <div class="JumpDialog-filter"> <input class="JumpDialog-input go-Input" autocomplete="off" type="text"> </div> <div class="JumpDialog-body go-Modal-body"> <div class="JumpDialog-list"></div> </div> <div class="go-Modal-actions"> <button class="go-Button" data-test-id="close-dialog">Close</button> </div> </form> </dialog> <dialog class="ShortcutsDialog go-Modal go-Modal--sm js-modal"> <form method="dialog"> <div class="go-Modal-header"> <h2>Keyboard shortcuts</h2> <button class="go-Button go-Button--inline" type="button" data-modal-close data-gtmc="modal button" aria-label="Close" > <img class="go-Icon" height="24" width="24" src="/static/shared/icon/close_gm_grey_24dp.svg" alt="" /> </button> </div> <div class="go-Modal-body"> <table> <tbody> <tr><td class="ShortcutsDialog-key"> <strong>?</strong></td><td> : This menu</td> </tr> <tr><td class="ShortcutsDialog-key"> <strong>/</strong></td><td> : Search site</td> </tr> <tr><td class="ShortcutsDialog-key"> <strong>f</strong> or <strong>F</strong></td><td> : Jump to</td> </tr> <tr> <td class="ShortcutsDialog-key"><strong>y</strong> or <strong>Y</strong></td> <td> : Canonical URL</td> </tr> </tbody> </table> </div> <div class="go-Modal-actions"> <button class="go-Button" data-test-id="close-dialog">Close</button> </div> </form> </dialog> <section class="Cookie-notice js-cookieNotice"> <div>go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic. <a target=_blank href="https://policies.google.com/technologies/cookies">Learn more.</a></div> <div><button class="go-Button">Okay</button></div> </section> <script> // this will throw if the querySelector can鈥檛 find the element const gtmId = document.querySelector('.js-gtmID').dataset.gtmid; if (!gtmId) { throw new Error('Google Tag Manager ID not found'); } loadScript(`https://www.googletagmanager.com/gtm.js?id=${gtmId}`); </script> <noscript> <iframe src="https://www.googletagmanager.com/ns.html?id=GTM-W8MVQXG" height="0" width="0" style="display:none;visibility:hidden"> </iframe> </noscript> <div class="js-canonicalURLPath" data-canonical-url-path="/html/template@go1.24.0" hidden></div> <div class="js-playgroundVars" data-modulepath="std" data-version="v1.24.0" hidden></div> <script> loadScript('/static/frontend/unit/main/main.js') </script> <script> loadScript('/static/frontend/unit/unit.js') </script> </body> </html>