Чанки | Participants
|
- Statistics
- Participants
- Translate into Russian
- Translation result
- Translated in draft, editing and proof-reading required.
If you do not want to register an account, you can sign in with OpenID.
Chunks | ||
Chunks are useful for reusing blocks of code or HTML to your sites. Chunks cannot contain any logic directly, although they can contain calls to Snippets that do contain logic. | Чанки предназначены для многократного использования блоков HTML-кода внутри сайта. Чанки не могут содержать логических выражений, однако они могут содержать вызовы сниппетов, которые, в свою очередь, могут содержать логические выражения. | |
Usage | ||
1. [[$chunkName]] | ||
Also, you can pass properties through a Chunk. Say you had a chunk named 'intro' with the contents: | Кроме того, вы можете передавать значения при вызове чанка. Например, у вас есть чанк «intro» с таким содержимым: | |
Hello, [[+name]]. You have [[+messageCount]] messages. | Здравствуйте, [[+name]]. У вас [[+messageCount]] сообщений. | |
You could fill those values with: | Вы можете заполнить эти значения следующим образом: | |
1. [[$intro? &name=`George` &messageCount=`12`]] | ||
Which would output: | ||
Hello, George. You have 12 messages. | ||
Processing Chunk via the API | ||
Chunks can be processed from a snippet by the process() function; for example, this code gets the 'rowTpl' Chunk like so: | Чанки могут обрабатываться сниппетами с помощью функции process(); например, это код получает чанк «rowTpl»: | |
# <tr class="[[+rowCls]]" id="row[[+id]]"> | ||
# <td>[[+pagetitle]]</td> | ||
# <td>[[+introtext]]</td> | ||
# </tr> | ||
and processes it with an array of properties for all the published Resources, into a table, setting the class to "alt" if is an even row: | и, используя массив свойств всех опубликованных ресурсов, переводит его в таблицу, устанавливая класс "alt" для чётных строк: | |
1. $modx->getCollection('modResource',array('published')); | $modx->getCollection('modResource',array('published')); | |
2. $chunk = $modx->getChunk('rowTpl'); | ||
3. $i = 0; | ||
4. $output = ''; | ||
5. foreach ($resources as $resource) { | ||
6. $properties = $resource->toArray(); | ||
7. $properties['rowCls'] = $i % 2 ? '' : 'alt'; | ||
8. | ||
9. $output .= $rowChunk->process($properties); | ||
10. $i++; | ||
11. } | ||
12. return '<table><tbody>'.$output.'</tbody></table>'; | return '<table><tbody>'.$output.'</tbody></table>'; | |
Modifying a Chunk Via the API | ||
Chunks can also be manipulated by the MODx API: | ||
# <?php | ||
# /* create a new chunk, give it some content and save it to the database */ | /* создаём новый чанк, присваиваем ему некоторое содержимое и сохраняем в базу данных */ | |
# $chunk = $modx->newObject('modChunk'); | ||
# $chunk->set('name','NewChunkName'); | ||
# $chunk->setContent('<p>This is my new chunk!</p>'); | ||
# $chunk->save(); | ||
# | ||
# /* get an existing chunk, modify the content and save changes to the database */ | /* получаем существующий чанк, изменяем его содержимое и сохраняем изменения в базу данных */ | |
# $chunk = $modx->getObject('modChunk', array('name' => 'MyExistingChunk')); | $chunk = $modx->getObject('modChunk', array('name' => 'MyExistingChunk')); | |
# if ($chunk) { | ||
# $chunk->setContent('<p>This is my existing chunks new content!</p>'); | $chunk->setContent('<p>Это новое содержимое для моего чанка!</p>'); | |
# $chunk->save(); | ||
# } | ||
# | ||
# /* get an existing chunk and delete it from the database */ | /* получаем существующий чанк и удаляем его из базы данных */ |
