Fbml.registerCustomTags
From Facebook Developer Wiki
Contents |
Description
Registers custom tags you can include in your that applications' FBML markup. Custom tags consist of FBML snippets that are rendered during parse time on the containing page that references the custom tag.
Custom tags can be either private or public. For more information, see Custom Tags. When you register public custom tags, you should add the source code for the tags to the Custom Tags Directory.
To use these custom tags in an FBML snippet, you must define a namespace with the fb:fbml tag. This imports an application's custom tags into an FBML namespace. See fb:fbml for more information on how to use custom tags.
You can include <script> tags in the tag's FBML markup, and Facebook will interpret the tag as regular FBJS.
Custom tags can have attributes whose values are substituted into the tag's FBML before it's parsed. For example, a video tag can have an id attribute that indicates which video to show. Attributes can be required or optional. Optional attributes have default values that are used when the attribute is not specified.
To include attribute placeholders in a tag's FBML, use the ${attribute_name} syntax. For example, the video tag with an id attribute could have the following FBML:
This example shows how you would include this tag in an FBML snippet, and specify the ID of the video you want to show:
In the containing page, the <videos:video id="1234" /> tag will be expanded into the following FBML:
Note: Values of inline custom tag attributes are HTML escaped before they are substituted into the custom tag's source FBML. For example,
is transformed into
before the tag is rendered.
Using Complex Attributes
You can also define attributes as child elements of the custom tag. These are called complex attributes. Complex attributes make it easier to assign multi-line strings to attribute values. They may contain any valid HTML character, but they may not contain any FBML tags nor most HTML tags (except for font style elements). The above example is equivalent to:
Every custom tag has an implicit attribute called '_fb_tag_id'. The values of this attribute are guaranteed to be unique for all custom tags on a page. This attribute is useful for defining element IDs that are specific to the tag that defined them. A common use case is when making an AJAX request whose ondone handler manipulates the DOM of the custom tag (see Performance Notes below for more information).
To escape the ${attribute_name} syntax, use a preceding backslash. For example, the string '\${id}' in the tag's FBML will be rendered as '${id}' in the containing page -- it will not be substituted by the attribute value.
Important: fbml.registerCustomTags overwrites the previous definitions of any existing tags that have the same names as the new tags. This can potentially break any FBML that relies on the old definition of the tags. Use this function with extreme care, especially if other applications may be using your public tags. Be very careful when you remove attributes, and when you add new attributes, make sure you give them default values to avoid breaking existing pages that don't use the new attributes (more on tag attributes below).
Important: If your custom tags define JavaScript variables or functions or CSS classes, name them in a way that will minimize the chance for collision with other elements defined on the page. A good practice is to prefix the names of such elements with your application's name.
Note: If a tag is public, any application can fetch its source FBML by calling fbml.getCustomTags. Don't register public tags with the expectation that the source FBML won't be available to other developers.
Note: Don't use fb:ref tags with handle attributes in the source FBML for a custom tags. When the page is rendered, the handles are scoped to the application that uses the custom tag, so the handles would not be resolved properly. Instead, use fb:ref tags with url attributes.
Performance Notes
For the best possible performance for users, you should avoid including heavy JavaScript libraries or performing expensive operations such as AJAX calls in the headers and bodies of custom tags. You should only do this in footer_fbml, as this prevents long delays when the browser renders the body of the page. It will also give you the opportunity to minimize the number of AJAX calls by batching them in a single request.
For example, if you wanted to create a tag that renders a gallery of images based on an ID, you could define its fbml property as follows:
The problem with this solution is that it doesn't delay the AJAX call until the browser is done rendering the document's body. Furthermore, if the tag appear multiple times on a page, every instance will trigger a separate AJAX call, which would result in poor performance.
A better solution would be to define the tag's properties as follows:
header_fbml:
fbml:
footer_fbml:
For best performance, it's also recommended to include static files instead of inline CSS and FBJS. For more information, see Include files.
Parameters
| Required | Name | Type | Description | |
| required | tags | array | a JSON-encoded list of tag objects. Each tag object is an object with the following properties:
|
|---|
