MODx: resources, chunks, and some TVs
After one of my friends asked me about what kind of TVs are used in templates, I decided to put everything aside for the evening and to write this article.
We will talk about what is MODx, as it is better "cooking", "feeding" and "consumed".
It is designed primarily for beginners because it contains basic minimum of what you need to know any modxоводу, and of course those who are just curious.
The article focuses mainly on the Revolution and reflects basic differences in syntax from its predecessor, but for backward compatibility I will sometimes insert an analogy with Evolution.
the
Often the resource is a page. In addition, there are other types of resources such as, links, the files themselves, etc. the default type of a new resource — the document, more precisely the view one page of your website.
There are 4 types of resources:
the
Each resource also has a unique identifier, or "Resource Identifier". If you want to link two resources, you should use the ID. So, MODx will generate a link and you don't have to worry about changing the page address or addresses.
As each resource can be assigned a template, the template can define additional fields for that resource.
Resources can act as containers to contain other resources. This forms the structure of the site.
the
The template is the base entity that defines the display of pages in MODx. Templates typically contain the HTML markup tags that determine the location and appearance of your website. When the page is requested, MODx loads the document or resource and its template, then MODx finds all the extra fields in the template and replaces them with corresponding values from the document before sending the page to the user's browser.
The template can be assigned to any resource and defines essentially a web page which will display the contents of this resource.
Template can not be included in another template as part.
Example template:
the
Used to output the field values of the resource.
The challenge is:
the
Full list of fields you can see in the documentation here.
the
TVan Additional field or template variable (TV) is a custom field, or, more precisely, the custom field for a MODx resource. TV-parameters are used to extend the standard fields of the resource. Each resource in MODx has a certain number of default fields, see above in the section about resources.
If faced with the task to add some additional fields to the page, for example, a dropdown list of month names or additional image, or any other type of custom data you can do this by adding the TV option appropriate type. MODx allows you to have virtually unlimited number of TV-parameters.
TV tag is replaced with the appropriate value filled in by the user during the processing of a resource. As each parameter is bound to any pattern and can only be used in conjunction with it.
The challenge is:
the
TV options can be used as chunks adding parameters to them. For example, if the TV parameter 'intromsg' with value:
Then you can fill it with data when you call:
The result:
You can also apply filters to the output so for example you can limit the length of the output string up to 100 characters:
A complete filter list can be viewed tut. In addition filters can be applied to chunks and snippets.
the
the
Chunk — a piece of static text that can be embedded in a template to another chunk, or call in the snippet. Chunk has the same properties as the template except that it does not contain TV-parameters and cannot be assigned to a resource directly.
A chunk can not contain any executable code, but it is possible to call snippets to output dynamic content.
The challenge of the chunk is:
the
In chunk, you can pass parameters. For example, we'll create a chunk with the same content:
Such a chunk can be invoked with parameters, which later will be transferred inside of it:
The above example will output:
In addition to static data in a call to chunk you can place additional shared resource settings, such as:
the
Snippet — PHP code which is executed during processing of the template in MODx. The result of it can be located either at the place of his conclusion, or placeholders, special tags defining where to put certain results.
The call to the snippet is:
the
The placement of the placeholder
the
Like chunks in snippets you can pass parameters, like this:
The passed parameters can be grouped in the so-called default settings (Property Sets) that represent a dynamic group of parameters that can passed to the snippet by specifying the name of the group.
For example create a group 'Menu' with the options `startId`=0 and `level`=1, then the snippet call in this way:
Will be similar, because the parameters will be substituted automatically:
Any default setting can be overwritten. In the following example, the value of the parameter `level` equal to one, will be replaced by 2.
To tell the system not to cache the snippet you want to add an exclamation point before the name:
the
Every MODx Revolution tag can contain other tags in MODx. What would the code less readable are allowed to post your tag code on multiple lines in this common format (in parentheses, my comments, which is not necessary to write =)):
the
the
the
the
Article based on information from habrahabr.ru
We will talk about what is MODx, as it is better "cooking", "feeding" and "consumed".
It is designed primarily for beginners because it contains basic minimum of what you need to know any modxоводу, and of course those who are just curious.
The article focuses mainly on the Revolution and reflects basic differences in syntax from its predecessor, but for backward compatibility I will sometimes insert an analogy with Evolution.
the
Resources (. Resources)
Often the resource is a page. In addition, there are other types of resources such as, links, the files themselves, etc. the default type of a new resource — the document, more precisely the view one page of your website.
There are 4 types of resources:
the
-
the
- Document — the most common resource, in fact the web page of the website. The bulk consists of title, abstract, detailed text, various dates, meta tags and additional fields (TV parameters); the
- Web link — link to an external resource or a web page. the
- symlink — internal link to another resource; the
- Static resource file.
Each resource also has a unique identifier, or "Resource Identifier". If you want to link two resources, you should use the ID. So, MODx will generate a link and you don't have to worry about changing the page address or addresses.
As each resource can be assigned a template, the template can define additional fields for that resource.
Resources can act as containers to contain other resources. This forms the structure of the site.
the
Templates (Templates)
The template is the base entity that defines the display of pages in MODx. Templates typically contain the HTML markup tags that determine the location and appearance of your website. When the page is requested, MODx loads the document or resource and its template, then MODx finds all the extra fields in the template and replaces them with corresponding values from the document before sending the page to the user's browser.
The template can be assigned to any resource and defines essentially a web page which will display the contents of this resource.
Template can not be included in another template as part.
Example template:
<html>
<head>
<title>[[*pagetitle]]</title>
<meta name="description" content="[[*description]]"/>
</head>
<body>
<h1>[[*longtitle]]</h1>
the
- ID: [[*id]]<br/>
the - Announcement: [[*introtext]]<br/>
the - menu option: [[*menutitle]]
<hr/>
- [[*content]]
</body>
</html>
the
Parameters
Used to output the field values of the resource.
The challenge is:
Evolution | Revolution |
[*field*] | [[*field]] |
Full list of fields you can see in the documentation here.
the
TV settings
If faced with the task to add some additional fields to the page, for example, a dropdown list of month names or additional image, or any other type of custom data you can do this by adding the TV option appropriate type. MODx allows you to have virtually unlimited number of TV-parameters.
TV tag is replaced with the appropriate value filled in by the user during the processing of a resource. As each parameter is bound to any pattern and can only be used in conjunction with it.
The challenge is:
Evolution | Revolution |
[*tv*] | [[*tv]] |
TV options can be used as chunks adding parameters to them. For example, if the TV parameter 'intromsg' with value:
Hello [[+name]], you [[+messageCount]] unread.
Then you can fill it with data when you call:
- [[*intromsg?name=`Grisha` &messageCount=`123`]]
The result:
the - Hello Gregory, you have 123 unread messages.
You can also apply filters to the output so for example you can limit the length of the output string up to 100 characters:
- [[*bioMessage:limit=`100`]]
A complete filter list can be viewed tut. In addition filters can be applied to chunks and snippets.
the
Comments
the - [[# In the template is allowed to leave comments, this code to be removed from the page after rendering. ]]
the
Chunks (Chunks)
Chunk — a piece of static text that can be embedded in a template to another chunk, or call in the snippet. Chunk has the same properties as the template except that it does not contain TV-parameters and cannot be assigned to a resource directly.
A chunk can not contain any executable code, but it is possible to call snippets to output dynamic content.
The challenge of the chunk is:
Evolution | Revolution |
{{chunk}} | [[$chunk]] |
In chunk, you can pass parameters. For example, we'll create a chunk with the same content:
Hello [[+name]]. You have [[+messageCount]] messages unread.
Such a chunk can be invoked with parameters, which later will be transferred inside of it:
- [[$intro? &name=`Basil` &messageCount=`12`]]
The above example will output:
the - Hello, Basil. You have 12 unread messages.
In addition to static data in a call to chunk you can place additional shared resource settings, such as:
the - [[!$intro? &name=`[[*usersName]]` &messageCount=`[[*messageCount]]`]]
the
Snippets (Snippets)
Snippet — PHP code which is executed during processing of the template in MODx. The result of it can be located either at the place of his conclusion, or placeholders, special tags defining where to put certain results.
The call to the snippet is:
Evolution | Revolution |
[[snippet]] | [[snippet]] |
The placement of the placeholder
Evolution | Revolution |
[+placeholder+] | [[+placeholder]] |
Like chunks in snippets you can pass parameters, like this:
- [[!Wayfinder? &startId=`0` &level=`1`]]
The passed parameters can be grouped in the so-called default settings (Property Sets) that represent a dynamic group of parameters that can passed to the snippet by specifying the name of the group.
For example create a group 'Menu' with the options `startId`=0 and `level`=1, then the snippet call in this way:
- [[!Wayfinder@Menu]]
Will be similar, because the parameters will be substituted automatically:
- [[!Wayfinder &startId=`0` &level=`1`]]
Any default setting can be overwritten. In the following example, the value of the parameter `level` equal to one, will be replaced by 2.
- [[!Wayfinder@Menu? &level=`2`]]
To tell the system not to cache the snippet you want to add an exclamation point before the name:
[[!noCacheSnippet]]
the
tag Syntax
Every MODx Revolution tag can contain other tags in MODx. What would the code less readable are allowed to post your tag code on multiple lines in this common format (in parentheses, my comments, which is not necessary to write =)):
the
- [[ (opening tag)
the - ! (optional, a symbol of the ban caching)
the - elementToken (optional character specifying the type parameter if it's not a snippet, $=chunk *=regular/supplementary resource field,+=, placeholder, etc)
the - elementName (element name)
the - @propertyset (optional PropertySet ID)
the - :filterName=`modifier`:... (optional filters, one or more)
the - ? (optional, defines the beginning line parameters)
&propertyName=`propertyValue` &... (optional any parameters separated by &)
the
- ]] (closing tag)
the
Pivot plate of the MODx tag syntax
Evolution | Revolution | Example | |
Parameter TV | [*templatevar*] | [[*templatevar]] | [[*pagetitle]] |
chunk | {{chunk}} | [[$chunk]] | [[$header]] |
Snippet (a piece of php code) | [[snippet]] | [[snippet]] or [[!snippet]] | [[getResources]] |
Placeholder | [+placeholder+] | [[+placeholder]] | [[+modx.user.id]] |
Link to resource | [~link~] | [[~link]] | [[~13]] |
System settings | [(system_setting)] | [[++system_setting]] | [[++site_start]] |
the
Resources
the
Комментарии
Отправить комментарий