Creating and Using Links

Creating and Using Links

With Scrivito, editors don’t need to worry about links and their up-to-dateness as the CMS manages them automatically. For example, when creating a page from within a navigation, the page can afterwards be moved to a different location without having to manually adjust the links pointing to any page involved in this operation. Linking a piece of text to a page is as easy as selecting the text and choosing the target via the Content Browser.

Developers can easily have sortable navigations generated automatically. See the Scrivito.ChildListTag documentation for details, or the tutorial on Building a Child Navigation Widget.

For those of you wanting to construct links programmatically, here’s an overview of Scrivito’s built-in support for handling them.

Scrivito has two attribute types for maintaining links, link and linklist. link is intended for maintaining single links, while linklist lets you create and maintain “see also” link lists, i.e. classical “related links” pointing to pages recommended for further reading.

Note that references to CMS-internal objects, be it pages, images or the like, can be maintained more efficiently using attributes of the reference and referencelist types since they’re not packed with mechanisms for handling URLs.

For dealing with the contents of a link attribute or an element of a linklist, Scrivito provides the Link class whose instances have the following attributes:

Attribute nameGetterReturns Type/ClassPurpose
hashhash()StringURL part referencing an anchor on the destination page (#hash)
objobj()ObjFor an internal link, the CMS object it points to (link destination)
objIdobjId()StringFor an internal link, the ID of the destination CMS object
queryquery()StringThe URL part specifying the parameters (?lang=en)
relrel()StringThe rel tag attribute of the link specifying its meaning, e.g. “next” or “search”
targettarget()StringName of the window in which the link destination should be opened
titletitle()StringFor rendering the “title” tag attribute of the link (link description)
urlurl()StringThe URL an external link points to

Note that obj and url are mutually exclusive as links can be either internal (obj) or external (url), not both. In addition to its attributes, a Link instance has two boolean methods for checking whether it’s an external or internal link, isExternal and isInternal, respectively.

Usage examples

If you are in need of an example of how to use Link objects, the Scrivito Example App includes a LinkContainerWidget and a LinkWidget you could use and adapt as required.