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

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

  1. ID: [[*id]]<br/>
  2. the
  3. Announcement: [[*introtext]]<br/>
  4. the
  5. menu option: [[*menutitle]]
  6. <hr/>

  7. [[*content]]
  8. </body>

    </html>


the

Parameters


Used to output the field values of the resource.
The challenge is:
the the
Evolution Revolution
[*field*] [[*field]]

Full list of fields you can see in the documentation here.
the

TV settings


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 the
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:
  1. [[*intromsg?name=`Grisha` &messageCount=`123`]]

The result:
    the
  1. 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:
  1. [[*bioMessage:limit=`100`]]

A complete filter list can be viewed tut. In addition filters can be applied to chunks and snippets.

the

Comments


    the
  1. [[# 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:
the the
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:
  1. [[$intro? &name=`Basil` &messageCount=`12`]]

The above example will output:
    the
  1. 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
  1. [[!$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:
the the
Evolution Revolution
[[snippet]] [[snippet]]

The placement of the placeholder
the the
Evolution Revolution
[+placeholder+] [[+placeholder]]

Like chunks in snippets you can pass parameters, like this:
  1. [[!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:
  1. [[!Wayfinder@Menu]]

Will be similar, because the parameters will be substituted automatically:
  1. [[!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.
  1. [[!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

  1. [[ (opening tag)
  2. the
  3. ! (optional, a symbol of the ban caching)
  4. the
  5. elementToken (optional character specifying the type parameter if it's not a snippet, $=chunk *=regular/supplementary resource field,+=, placeholder, etc)
  6. the
  7. elementName (element name)
  8. the
  9. @propertyset (optional PropertySet ID)
  10. the
  11. :filterName=`modifier`:... (optional filters, one or more)
  12. the
  13. ? (optional, defines the beginning line parameters)
  14. &propertyName=`propertyValue` &... (optional any parameters separated by &) the

  15. ]] (closing tag)


the

Pivot plate of the MODx tag syntax


the the the the the the the
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
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Fresh hay from the cow, or 3000 icons submitted!

Knowledge base. Part 2. Freebase: make requests to the Google Knowledge Graph

Group edit the resources (documents) using MIGXDB