- What is a resource?
- Manage Resources
- Resource Fields
- Common Resource Fields
- Resource field settings
- Using Resource Fields
- Accessing resource fields in a snippet
- Resource Reference
- URL Parameters for Link Tags
- URL Schemes in Link Tags
- Description of resource types
- Weblink
- Example
- Symbolic Link
- Use cases
- Explanations
- Static Resource
- About Aliases
- Content Types
- Usage
- Creating new content types
What is a resource?
A resource is a representation of a page in MODx. By default, MODx has 4 types of resources – documents, links, symbolic links, and files. The default resource type is a document. It simply represents a web page on your site.
There are 4 types of resources, and they are documents, Weblinks, Symlinks, and Static Resources.
Each resource also has a unique identifier, or “resource ID”. This allows MODX to know which resource to select when loading a web page. IDs are also needed for working with resources (for example, selecting by ID). Additionally, if you want to link resources, you can use their IDs to accomplish this. This way, MODX will create a link to your resource, and you won’t have to worry about changing the alias, content type, or anything else – MODX will apply all the changes automatically.
Manage Resources
Resources are displayed in the resource tree in the left navigation menu of the manager. To edit, simply click on the page you want to edit. You can also right-click on a resource and select Edit. This will load the resource edit page:
The content of a resource can be edited in the large field (Content) in the lower area. Other fields associated with the resource can also be edited using the tabs at the top of the page.
Resource Fields
By default, resources have a list of predefined fields. Here they are:
Common Resource Fields
Title | Description |
---|---|
id | Resource ID. |
template | Link to Template that this resource uses |
published | If the resource is published or viewable on the front-end. |
pagetitle | Resource Title. |
longtitle | Long Title resource. |
description | Description of the resource. |
introtext | Introductory excerpt of the resource content. Useful for blogs or search. |
alias | URL alias. Used if your site uses friendly URLs. A resource with the alias “home” and content type “html” will output “home.html” if it is not a container. |
parent | Parent resource identifier. |
link_attributes | Used in menu building snippets to add attributes to links. Can be used for other purposes as well. |
menutitle | A short display of the resource title in the menu. |
menuindex | Resource index in the tree menu. A higher value means a lower position of the resource in the menu. |
hidemenu | “Hide from menu”, if set, the resource will not be displayed in most menu generation snippets. |
content | Resource content. Often the main working field. |
Resource field settings
Name | Description |
---|---|
isfolder | Marks the resource as a “Container”. |
searchable | Makes the resource searchable. |
cacheable | If checked, the resource will be saved to the cache after the first access to him. |
createdby | The ID of the user who created the resource. |
editedby | The ID of the user who created the resource. |
deleted | Marks the resource as deleted. |
deletedby | The ID of the user who deleted the resource. |
publishedby | The ID of the last user who published the resource. |
createdon | Creation date resource. |
publishedon | The date the resource was published. |
editedon | The date the resource was edited. |
pub_date | The scheduled date the resource was published. |
unpub_date | The scheduled date the resource will be unpublished. |
Using Resource Fields
Resource field data can be obtained using the following syntax: Template Variables for example:
[[*pagetitle]] // Displays the resource title.
[[*id]] // Displays the resource ID.
[[*createdby]] //Displays the ID of the user who created the resource.
They can also have Output filters “Filters vvode and output (output modifiers)”) applied to them:
// Renders a limited version of the introtext field.
// If it is longer than 100 characters, appends ...
[[*introtext:ellipsis=`100`]]
// Renders the data of the introtext field, truncating the value at the hundredth character.
[[*editedby:userinfo=`username`]]
// Renders the name of the user who last modified the resource
[[*publishedby:userinfo=`email`]]
// Renders the email of the user who published the resource
Accessing resource fields in a snippet
Getting the value of resource fields in a Snippet is quite easy. MODx gives you a Resource object, in any snippet you can get the value of any resource field by referencing $modx->resource
. This snippet example will return the current page title:
/* Displays the current resource title */
$output = $modx->resource->get('pagetitle');
return strrev($output);
Resource Reference
In MODX, resource references are dynamically managed through “reference tags”. They look like this:
[[~123]]
where “123” is the resource ID you want to reference. You can place these tags anywhere, and MODX will dynamically generate a URL for the specified resource.
Also see Named Anchor.
URL Parameters for Link Tags
Adding URL parameters to a link tag is pretty simple in Revolution. Let’s say we have a resource ID of 42, which resolves to the URL “store/items.html”. We want to add a “tag” parameter to the tag with a value of “Snacks” and a “sort” parameter of “Taste”. Here’s how you would do that:
[[~42? &tag=`Snacks` &sort=`Taste`]]
This would render as:
store/items.html?tag=Snacks&sort=Taste
Note that these are backticks instead of apostrophes.
URL Schemes in Link Tags
You can specify a scheme for a resource in your tag:
[[~123? &scheme=`https`]]
Will display the URL using “https” instead of the scheme specified by the current settings (i.e. system or context settings).
Available schemes:
Name | Description |
---|---|
-1 | (default) URL relative to site_url |
0 | See http |
1 | See https |
full | Provides a link as an absolute URL with site_url appended |
abs | Provides a link as an absolute URL with site_url appended base_url |
http | Renders the link as an absolute URL, forces http scheme |
https | Renders the link as an absolute URL, forces https scheme |
Description of resource types
Weblink
A Weblink is a document of type “link”. Its template is not used to display or format the link, rather the template in this case simply serves as a container for any template variables you might want to add to your WebLink. The WebLink simply serves as a link that will be part of the generated menu.
The content of a Weblink is just a URL; the parser doesn’t even parse it, MODX just redirects to the URL. Once it sees that it is a “link”, it simply uses the content as an argument to sendRedirect($url).
You can use an external URL for the content, or a resource ID to link to a resource in the MODX resource tree.
Example
Let’s say you want a footer menu with links to Terms of Service, Privacy Policy, and more. But you also want a “Contact Us” link there. Contact Us is one of your main pages, which is at the top level of your tree and appears in your main menu. You would put these resources in a single “special pages” folder, perhaps unpublished so they don’t show up in your main menu, and use that folder ID as the resource ID for the menu snippet. In that folder, you would also put a link to your site’s contact page. That way, the menu will contain a link to the contact page even though that resource isn’t in the folder.
Or you could do it the other way around, place the Contact Us resource in your unpublished “service pages” folder and link to it at the top level so it shows up in the main menu.
Originally, the menu snippet created a link to the weblink itself, just like any other MODX resource, thus causing the page to be loaded by the parser, causing a redirect.
Symbolic Link
WithSymlinks map a URL to an existing resource. They are similar to Web links in that they redirect to another resource or URL; however, symbolic links will preserve the current URL. The name is taken from Linux, and the purpose is similar.
Use cases
Many sites may never use Symlinks, but they can solve certain problems.
- You can use a symbolic link as a kind of 301 redirect.
- You can avoid duplicate content: create the content once, then point to it with a symbolic link.
- You can apply different permissions to the same content: put symbolic links in different resource groups.
Explanations
The target page must be published to resolve the symbolic link. If the target page is not published, a 503 error will be generated.
Similarly, the target page must exist, otherwise an error page will be generated. You are not currently prevented from entering invalid page IDs when creating a symbolic link.
Static Resource
A static resource is a resource abstraction of an actual file on the file system. You can think of the abstraction as a virtual representation of any file on your web server. Like other MODX resources, this abstraction can have permissions associated with it, and it appears in the MODX Manager in the resource tree, allowing it to be displayed in dynamic menus and search results. A MODX static resource can represent any file on your web server (as long as the permissions are allowed).
Static resources can also have tags in their content fields to define the file path – so you can specify your own paths to configure as system settings, or use Snippets to find the path dynamically.
They behave exactly like a Document (standard resource). Content in a Static Resource is parsed and displayed exactly like a Document, but you will often change the Content Types and content layout to suit the type of file you are representing.
About Aliases
When creating a Static Resource, the alias should not include the file extension: the file extension is determined by the Content Type. For example, if you are creating a Static Resource that points to a PDF file on your web server, you might give the page the alias “test”. Once you have selected “PDF” as the Content Type, the “.pdf” extension will be added to the alias (assuming you defined the PDF Content Type using “.pdf” as the file extension). If you have created a custom content type for PDF, you do not need to set the content placement field to “attachment”. It can be left as inline, as modern browsers can negotiate PDFs and can then also choose their own preferred way to handle PDFs – for example, Chrome will display the PDF in the browser, whereas others may download the file.
To ensure that your file is served correctly, you should use an (empty) template. The static resource becomes a reference to the file as long as you use this template. If you need to add meta information to the resource via some template variables, and you create your own “empty” template to include this, then you must set the template content to [[*content]]
before it will work correctly.
Content Types
The content type of a resource is what MODX will send in the Content-Type
server header line. They are associated with file extensions and tell the MODX parser what type of extension to render the page with.
For example, resources with the alias “test” and the content type “CSS” with the file extension “.css” will be displayed as test.css
instead of test.html
. This allows you to create any file type directly from the resources themselves.
Usage
When editing a resource, in the “Settings” tab, select the content type you want to use:
Then save the Resource. This will automatically associate the Resources with the selected content type.
Creating new content types
In the main menu, hover over the “Content” section and select the “Content Types” subsection. You will see a grid filled with all current content types. Click on the “Create” button and a window will appear:
You will see the following fields:
- Name – The name of the content type. This is mainly for organizational and labeling purposes and does not affect the function of the type.
- MIME Type – Here you can set the MIME type for the extension, which will tell the browser what type of file the Resource is. A list of available MIME types can be found here.
- File Extension – This is the file extension of your resource. Specified with a dot, such as “.docx”, “.xlsx”.
- Binary – The binary file type or
text/ascii
? - Description – an optional field to describe the content type.
Once you have filled in all the fields, click the “save” button, and your new content type will appear in the list of types. After that, you can select it in the resource settings.
Resource alias When you create a resource with the desired content type, the file extension of this content type will be added to its alias. For example, if you created a resource with the content type JSON
and the alias example
, you will get the alias example.json
(if you have friendly URLs enabled).