Fragments API#
Python representation of a web fragment.
- class web_fragments.fragment.Fragment(content=None)#
A fragment of a web page to be included on another page.
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_fragment_resources(fragment)#
Add all the resources from a single fragment to my resources.
This is used to aggregate resources from another fragment that should be considered part of the current fragment.
The content from the Fragment 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()oradd_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()oradd_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().
- add_resources(fragments)#
Add all the resources from fragments to my resources.
This is used to aggregate resources from a sequence of fragments that should be considered part of the current fragment.
The content from the Fragments is ignored. The caller must collect together the content into this Fragment’s content.
- body_html()#
Get the body HTML for this Fragment.
Returns a Unicode string, the HTML content for the
<body>section of the page.
- content#
The html content for this Fragment
- 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.
- classmethod from_dict(pods)#
Returns a new Fragment from a dictionary representation.
- 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, json_args=None)#
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.
- static resource_to_html(resource)#
Returns resource wrapped in the appropriate html tag for it’s mimetype.
- property resources#
Returns list of unique FragmentResource named tuples by order of first appearance.
- resources_to_html(placement)#
Get some resource HTML for this Fragment.
placement is “head” or “foot”.
Returns a unicode string, the HTML for the head or foot of the page.
- to_dict()#
Returns the fragment in a dictionary representation.
- class web_fragments.fragment.FragmentResource(kind, data, mimetype, placement)#
Create new instance of FragmentResource(kind, data, mimetype, placement)
- data#
Alias for field number 1
- kind#
Alias for field number 0
- mimetype#
Alias for field number 2
- placement#
Alias for field number 3
- class web_fragments.views.FragmentView(**kwargs)#
Base class for Django web fragment views.
Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.
- get(request, *args, **kwargs)#
Render a fragment to HTML or return JSON describing it, based on the request.
- render_standalone_response(request, fragment, **kwargs)#
Renders a standalone page as a response for the specified fragment.
- abstractmethod render_to_fragment(request, **kwargs)#
Render this view to a fragment.
- render_to_standalone_html(request, fragment, **kwargs)#
Render the specified fragment to HTML for a standalone page.