Table Of Contents

Previous topic

trac.web.auth – Trac Authentication

Next topic

trac.web.href – Creation of URLs

This Page

trac.web.chrome – Trac content generation for the Web

Content presentation for the web layer.

The Chrome module deals with delivering and shaping content to the end user, mostly targeting (X)HTML generation but not exclusively, RSS or other forms of web content are also using facilities provided here.

Interfaces

class trac.web.chrome.INavigationContributor

Extension point interface for components that contribute items to the navigation.

See also trac.web.chrome.INavigationContributor extension point

get_active_navigation_item(req)

This method is only called for the IRequestHandler processing the request.

It should return the name of the navigation item that should be highlighted as active/current.

get_navigation_items(req)

Should return an iterable object over the list of navigation items to add, each being a tuple in the form (category, name, text).

class trac.web.chrome.ITemplateProvider

Extension point interface for components that provide their own Genshi templates and accompanying static resources.

See also trac.web.chrome.ITemplateProvider extension point

get_htdocs_dirs()

Return a list of directories with static resources (such as style sheets, images, etc.)

Each item in the list must be a (prefix, abspath) tuple. The prefix part defines the path in the URL that requests to these resources are prefixed with.

The abspath is the absolute path to the directory containing the resources on the local file system.

get_templates_dirs()

Return a list of directories containing the provided template files.

Components

class trac.web.chrome.Chrome

Web site chrome assembly manager.

Chrome is everything that is not actual page content.

add_auto_preview(req)

Setup auto-preview for <textarea> fields.

add_jquery_ui(req)

Add a reference to the jQuery UI script and link the stylesheet.

add_textarea_grips(req)

Make <textarea class="trac-resizable"> fields resizable if enabled by configuration.

add_wiki_toolbars(req)

Add wiki toolbars to <textarea class="wikitext"> fields.

auto_preview_timeout

Inactivity timeout in seconds after which the automatic wiki preview triggers an update. This option can contain floating-point values. The lower the setting, the more requests will be made to the server. Set this to 0 to disable automatic preview. The default is 2.0 seconds. (‘’since 0.12’‘)

auto_reload

Automatically reload template files after modification.

cc_list(cc_field)

Split a CC: value in a list of addresses.

default_dateinfo_format

The date information format. Valid options are ‘relative’ for displaying relative format and ‘absolute’ for displaying absolute format. (‘’since 1.0’‘)

environment_created()

Create the environment templates directory.

format_emails(context, value, sep=', ')

Normalize a list of e-mails and obfuscate them if needed.

Parameters:
  • context – the context in which the check for obfuscation should be done
  • value – a string containing a comma-separated list of e-mails
  • sep – the separator to use when rendering the list again
genshi_cache_size

The maximum number of templates that the template loader will cache in memory. The default value is 128. You may want to choose a higher value if your site uses a larger number of templates, and you have enough memory to spare, or you can reduce it if you are short on memory.

get_all_templates_dirs()

Return a list of the names of all known templates directories.

get_email_map()

Get the email addresses of all known users.

get_interface_customization_files()

Returns a dictionary containing the lists of files present in the site and shared templates and htdocs directories.

htdocs_location

Base URL for serving the core static resources below /chrome/common/.

It can be left empty, and Trac will simply serve those resources itself.

Advanced users can use this together with [TracAdmin trac-admin ... deploy <deploydir>] to allow serving the static resources for Trac directly from the web server. Note however that this only applies to the <deploydir>/htdocs/common directory, the other deployed resources (i.e. those from plugins) will not be made available this way and additional rewrite rules will be needed in the web server.

iterable_content(stream, method, **kwargs)

Generate an iterable object which iterates str instances from the given stream instance.

Parameters:method – the serialization method; can be either “xml”, “xhtml”, “html”, “text”, or a custom serializer class
jquery_location

Location of the jQuery !JavaScript library (version 1.7.2).

An empty value loads jQuery from the copy bundled with Trac.

Alternatively, jQuery could be loaded from a CDN, for example: http://code.jquery.com/jquery-1.7.2.min.js, http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js or https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js.

(‘’since 1.0’‘)

jquery_ui_location

Location of the jQuery UI !JavaScript library (version 1.8.21).

An empty value loads jQuery UI from the copy bundled with Trac.

Alternatively, jQuery UI could be loaded from a CDN, for example: https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js or http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.21/jquery-ui.min.js.

(‘’since 1.0’‘)

jquery_ui_theme_location

Location of the theme to be used with the jQuery UI !JavaScript library (version 1.8.21).

An empty value loads the custom Trac jQuery UI theme from the copy bundled with Trac.

Alternatively, a jQuery UI theme could be loaded from a CDN, for example: https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/start/jquery-ui.css or http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.21/themes/start/jquery-ui.css.

(‘’since 1.0’‘)

load_template(filename, method=None)

Retrieve a Template and optionally preset the template data.

Also, if the optional method argument is set to 'text', a NewTextTemplate instance will be created instead of a MarkupTemplate.

logo_alt

Alternative text for the header logo.

logo_height

Height of the header logo image in pixels.

URL to link to, from the header logo.

logo_src

URL of the image to use as header logo. It can be absolute, server relative or relative.

If relative, it is relative to one of the /chrome locations: site/your-logo.png if your-logo.png is located in the htdocs folder within your TracEnvironment; common/your-logo.png if your-logo.png is located in the folder mapped to the [#trac-section htdocs_location] URL. Only specifying your-logo.png is equivalent to the latter.

logo_width

Width of the header logo image in pixels.

mainnav_order

Order of the items to display in the mainnav navigation bar, listed by IDs. See also TracNavigation.

metanav_order

Order of the items to display in the metanav navigation bar, listed by IDs. See also TracNavigation.

navigation_contributors

List of components that implement INavigationContributor

never_obfuscate_mailto

Never obfuscate mailto: links explicitly written in the wiki, even if show_email_addresses is false or the user doesn’t have EMAIL_VIEW permission (‘’since 0.11.6’‘).

prepare_request(req, handler=None)

Prepare the basic chrome data for the request.

Parameters:
  • req – the request object
  • handler – the IRequestHandler instance that is processing the request
render_template(req, filename, data, content_type=None, fragment=False, iterable=False)

Render the filename using the data for the context.

The content_type argument is used to choose the kind of template used (NewTextTemplate if 'text/plain', MarkupTemplate otherwise), and tweak the rendering process. Doctype for 'text/html' can be specified by setting the html_doctype attribute (default is XHTML_STRICT)

When fragment is specified, the (filtered) Genshi stream is returned.

When iterable is specified, the content as an iterable instance which is generated from filtered Genshi stream is returned.

resizable_textareas

Make <textarea> fields resizable. Requires !JavaScript. (‘’since 0.12’‘)

shared_htdocs_dir

Path to the //shared htdocs directory//.

Static resources in that directory are mapped to /chrome/shared under the environment URL, in addition to common and site locations.

This can be useful in site.html for common interface customization of multiple Trac environments.

(‘’since 1.0’‘)

shared_templates_dir

Path to the //shared templates directory//.

Templates in that directory are loaded in addition to those in the environments templates directory, but the latter take precedence.

(‘’since 0.11’‘)

show_email_addresses

Show email addresses instead of usernames. If false, email addresses are obfuscated for users that don’t have EMAIL_VIEW permission. (‘’since 0.11’‘)

show_ip_addresses

Show IP addresses for resource edits (e.g. wiki). Since 1.0.5 this option is deprecated and will be removed in 1.3.1. (‘’since 0.11.3’‘)

stream_filters

List of components that implement ITemplateStreamFilter

template_providers

List of components that implement ITemplateProvider

use_chunked_encoding

If enabled, send contents as chunked encoding in HTTP/1.1. Otherwise, send contents with Content-Length header after entire of the contents are rendered. (‘’since 1.0.6’‘)

wiki_toolbars

Add a simple toolbar on top of Wiki `<textarea>`s. (‘’since 1.0.2’‘)

Functions

Most of the helper functions are related to content generation, and in particular, (X)HTML content generation, in one way or another.

trac.web.chrome.web_context(req, resource=None, id=False, version=False, parent=False, absurls=False)

Create a rendering context from a request.

The perm and href properties of the context will be initialized from the corresponding properties of the request object.

>>> from trac.test import Mock, MockPerm
>>> req = Mock(href=Mock(), perm=MockPerm())
>>> context = web_context(req)
>>> context.href is req.href
True
>>> context.perm is req.perm
True
Parameters:
  • req – the HTTP request object
  • resource – the Resource object or realm
  • id – the resource identifier
  • version – the resource version
  • absurls – whether URLs generated by the href object should be absolute (including the protocol scheme and host name)
Returns:

a new rendering context

Return type:

RenderingContext

Since:

version 1.0

trac.web.chrome.add_meta(req, content, http_equiv=None, name=None, scheme=None, lang=None)

Add a <meta> tag into the <head> of the generated HTML.

Web resources

trac.web.chrome.add_stylesheet(req, filename, mimetype='text/css', media=None)

Add a link to a style sheet to the chrome info so that it gets included in the generated HTML page.

If filename is a network-path reference (i.e. starts with a protocol or //), the return value will not be modified. If filename is absolute (i.e. starts with /), the generated link will be based off the application root path. If it is relative, the link will be based off the /chrome/ path.

trac.web.chrome.add_javascript(req, filename)
Deprecated:since 0.10, use add_script instead.
trac.web.chrome.add_script(req, filename, mimetype='text/javascript', charset='utf-8', ie_if=None)

Add a reference to an external javascript file to the template.

If filename is a network-path reference (i.e. starts with a protocol or //), the return value will not be modified. If filename is absolute (i.e. starts with /), the generated link will be based off the application root path. If it is relative, the link will be based off the /chrome/ path.

trac.web.chrome.add_script_data(req, data={}, **kwargs)

Add data to be made available in javascript scripts as global variables.

The keys in data and the keyword argument names provide the names of the global variables. The values are converted to JSON and assigned to the corresponding variables.

Page admonitions

trac.web.chrome.add_warning(req, msg, *args)

Add a non-fatal warning to the request object.

When rendering pages, all warnings will be rendered to the user. Note that the message is escaped (and therefore converted to Markup) before it is stored in the request object.

trac.web.chrome.add_notice(req, msg, *args)

Add an informational notice to the request object.

When rendering pages, all notices will be rendered to the user. Note that the message is escaped (and therefore converted to Markup) before it is stored in the request object.

Contextual Navigation

Add a link to the chrome info that will be inserted as <link> element in the <head> of the generated HTML

trac.web.chrome.add_ctxtnav(req, elm_or_label, href=None, title=None)

Add an entry to the current page’s ctxtnav bar.

trac.web.chrome.prevnext_nav(req, prev_label, next_label, up_label=None)

Add Previous/Up/Next navigation links.

Parameters:
  • req – a Request object
  • prev_label – the label to use for left (previous) link
  • up_label – the label to use for the middle (up) link
  • next_label – the label to use for right (next) link

Miscellaneous

Return an “authenticated” link to link for authenticated users.

If the user is anonymous, returns link unchanged. For authenticated users, returns a link to /login that redirects to link after authentication.