A
A
AJ2012-12-29 01:04:56
MODX
AJ, 2012-12-29 01:04:56

Can one sippet be a continuation of another in php code?

Good, habr.
Worth Revo 2.2.6
Today I decided to divide one large snippet of 2000 lines into several logical sections and place them in separate snippets for more convenient adjustment and debugging. But immediately ran into a problem.
Conditionally: the previous snippet is initializing and assigning a value to a variable. In the future, you need to work with this variable. Is this even possible?
It's just that my MySQL first swears at the syntax, after that, when I write what I'm looking for directly, it displays a blank screen, i.e. does not return anything from the database.
In this regard, the question is: can one snippet continue with another according to the include principle?
Thanks in advance for your replies.
PS - I tried passing parameters via $modx->runSnippet, it doesn't help.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
N
Nikolai Lanets, 2013-01-09
@2ball

As with any element (chunk, snippet, etc.), there is a $scriptProperties parameter inside the snippet.
You can pass it as an argument to the next snippet, for example, inside your snippet, call $modx->runSnippet($name, $scriptProperties); (and so many nestings.). At the same time, you can override and / or supplement $scriptProperties along the way.
See snippet cache code.

<?php
function elements_modsnippet_35($scriptProperties= array()) {
    global $modx;
    if (is_array($scriptProperties)) {
         extract($scriptProperties, EXTR_SKIP);
    } 
    print_R($scriptProperties); 
    exit;
}

exit was left on purpose so that the output can be seen immediately.

R
rasstroen, 2012-12-29
@rasstroen

include or require actually continues the code, yes, only global variables are not very good. Better classes, methods and the autoloader function.
The snippet will be one, but it will have different methods as a class. The methods will access the properties of this class.

A
Andrey Ch, 2012-12-29
@Andchir

Dig in this direction:
rtfm.modx.com/display/revolution20/modX.getService
You can load your class into $modx with a plugin. And then any methods and properties of this class will be available in any snippet.

A
AJ, 2012-12-29
@2ball

Generally. The optimal solution to this problem was the use of global variables via $_SESSION with the obligatory unset after the execution of the main snippet.
The principle is the following. In the snippet that collects all the others, a global variable is set, from all the others it is called without problems.
Normal use of global variables does not produce any results.
Apparently, they are forcibly killed by MODx itself during the processing of snippets in order to avoid errors and random mess with variable names. Which is quite logical.

E
Evengard, 2012-12-30
@Evengard

In fact, you can pass your own variables to snippets. It is clear that the snippets themselves will be isolated, but if you throw them (for example, using references) with a kind of wrapper snippet, then everything is possible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question