Fragment

class xblock.fragment.Fragment(content=None)

A fragment of a web page, for XBlock views to return.

A fragment consists of HTML for the body of the page, and a series of resources needed by the body. Resources are specified with a MIME type (such as “application/javascript” or “text/css”) that determines how they are inserted into the page. The resource is provided either as literal text, or as a URL. Text will be included on the page, wrapped appropriately for the MIME type. URLs will be used as-is on the page.

Resources are only inserted into the page once, even if many Fragments in the page ask for them. Determining duplicates is done by simple text matching.

add_content(content)

Add content to this fragment.

content is a Unicode string, HTML to append to the body of the fragment. It must not contain a <body> tag, or otherwise assume that it is the only content on the page.

add_css(text)

Add literal CSS to the Fragment.

add_css_url(url)

Add a CSS URL to the Fragment.

add_frag_resources(frag)

Add all the resources from frag to my resources.

This is used by an XBlock to collect resources from Fragments produced by its children.

frag is a Fragment.

The content from the Fragment is ignored. The caller must collect together the content into this Fragment’s content.

add_frags_resources(frags)

Add all the resources from frags to my resources.

This is used by an XBlock to collect resources from Fragments produced by its children.

frags is a sequence of Fragments.

The content from the Fragments is ignored. The caller must collect together the content into this Fragment’s content.

add_javascript(text)

Add literal Javascript to the Fragment.

add_javascript_url(url)

Add a Javascript URL to the Fragment.

add_resource(text, mimetype, placement=None)

Add a resource needed by this Fragment.

Other helpers, such as add_css() or add_javascript() are more convenient for those common types of resource.

text: the actual text of this resource, as a unicode string.

mimetype: the MIME type of the resource.

placement: where on the page the resource should be placed:

None: let the Fragment choose based on the MIME type.

“head”: put this resource in the <head> of the page.

“foot”: put this resource at the end of the <body> of the page.

add_resource_url(url, mimetype, placement=None)

Add a resource by URL needed by this Fragment.

Other helpers, such as add_css_url() or add_javascript_url() are more convenent for those common types of resource.

url: the URL to the resource.

Other parameters are as defined for add_resource().

body_html()

Get the body HTML for this Fragment.

Returns a Unicode string, the HTML content for the <body> section of the page.

foot_html()

Get the foot HTML for this Fragment.

Returns a Unicode string, the HTML content for the end of the <body> section of the page.

head_html()

Get the head HTML for this Fragment.

Returns a Unicode string, the HTML content for the <head> section of the page.

initialize_js(js_func)

Register a Javascript function to initialize the Javascript resources.

js_func is the name of a Javascript function defined by one of the Javascript resources. As part of setting up the browser’s runtime environment, the function will be invoked, passing a runtime object and a DOM element.