8 two-wheel tips for MODX Revolution

MODX Revolution I work not so long ago, but, nevertheless, at the moment, for me, this CMS is a gem. Flexibility, scalability and intuitiveness (If for a moment forget about the ill-fated MODX Manager), attract it all the same as in the beginning.
Below are the notes I made in the course of working with MODX Revolution over the last year. These simple techniques, if I knew about them earlier would have helped me save incredible amounts of time. The target audience of these notes — beginners, only recently, to understand what MODX. Frank "bike", out of respect for you in the notes include not become.

1. Simplify MODX Manager


With the increase of the project, the deployment of the resource tree when loading the Manager can take so much time, how much is enough to shake your psyche. Turn those categories and the contexts in which you, currently, do not work. When you navigate to a new page, the resource tree will show only that you have left untapped.

Another thing that has a very adverse effect on the performance of RSS-feeds on the main page of the Manager, informing about new versions and security fixes. Disabled as follows: System -> System Settings -> MODX News Feed Enabled/MODX Security Feed Enabled -> No.

2. Passing parameters to the snippet


Suppose you have a snippet named Monkeys, will appear on the page the line:

the
echo $howMany. '' .'Monkeys';


This spippy you need to display on multiple pages with different value of the variable $howMany. This is done quite simply:

the
[[Monkeys?&howMany=`12`]]


Thus, in the snippet, you can pass, for example, if you have the necessary order logic, the language in which you want to display the content.

3. Call MODX in a static PHP file


In case, if the snippet you are using only for include/require_once a static file, the previous reception still work as expected. But what if you want to use the $modx object? For example, take pagetitle of a certain resource? Everything is also very simple:

the
require_once '/var/www/config.core.php';
require_once MODX_CORE_PATH.'model/modx/modx.class.php';
$modx = new modX();
$modx- > initialize('web');


Now, such things you can use as much as you want:

the
$resource = $modx- > getObject('modResource', $modx- > resourceIdentifier);
$pagetitle = $resource->get('pagetitle');


3. Switch context depending on the domain


In the above reception flashed such a line:

the
$modx- > initialize('web');


This is to initialize the default context, which, in fact, all of the above will work. But what if several contexts, and each should be displayed in your specific domain? Easy!

the
switch ($_SERVER['HTTP_HOST']) {

case 'domainOne.com':
$modx- > initialize('web');
break;

case 'domainTwo':
$modx- > initialize('two');
break;

case 'domainThree':
$modx- > initialize('three');
break;

default:
$modx- > initialize('web');
break;

}


This switch must be run in either plug-in, the event OnHandleRequest, or a little "early" at the end index.php what you probably no one now living would not advise.

4. Switch on context


When you need to "diversify" your code for each of your contexts, you can use the following:

the
if($modx->context->get('key') != "mgr"){

switch ($modx->context->get('key')) {

case 'web':
$howMany = 3;
break;

case 'two':
$howMany = 8;
break;

case 'three':
$howMany = 12;
break;

default:
$howMany = 12;
break;

}
}

echo $howMany. '' .'Monkeys';


The condition if($modx->context->get('key') != "mgr"), as you may have guessed, prevents the code to run directly in the Manager.

5. Configure context


Another way to vary the content in existing contexts — individual settings. Click on the relevant context and also in the walls Context Settings. By pressing Create New, enter Key, and accordingly Value. Key you created, invoked in the document/chunk as follows:
the
[[!++Key]]


6. Called in the context of the desired chunk without creating a separate settings


Previous method, of course, good, but what if we only need different chunks depending on the context? Very useful in this regard, the key [[*context_key]], returns the name of the current context. For example, create two chunks: Footer_Web and Footer_One, where Web and One — the context names. Now, call them on the page / in a template:

the
[[$Footer_[[*context_key]]]]


7. modMail


modMail — class, extensible modPHPMailer. By default built into MODX and is used as follows:

the
$message = $modx- > getChunk('myEmailTemplate');

$modx- > getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'me@example.org');
$modx->mail->set(modMail::MAIL_FROM_NAME,'Johnny Tester');
$modx->mail->set(modMail::MAIL_SUBJECT,'Check out my new email template!');
$modx- > mail- > address('to','user@example.com');
$modx- > mail- > address('reply-to','me@xexample.org');
$modx->mail->setHTML(true);

if (!$modx->mail->send()) {
$modx- > log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to  send  the email: '.$modx- > mail- > mailer- > ErrorInfo);
}

$modx->mail->reset();


*Excerpt from the official documentatie.

8. From snippet to chunk


The above example takes the chunk myEmailTemplate as a template for our letters. If the letter you want to display, for example Username, derived from shape, we makes the following:

the
$Username = $_POST['Username'];

$message = $modx- > getChunk('myEmailTemplate', array(
'username' => $Username,
));


Thus, we pass our Username to the chunk myEmailTemplate. Get it in chunk, in this way:

the
<p>Username: [[+username]]</p>


All for today. If anyone would be interested in such notes, ready with joy to continue. Thank you for your attention.
Article based on information from habrahabr.ru

Комментарии

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

Car navigation in detail

Google has launched an online training course advanced search

PostgreSQL: Analytics for DBA