Search code examples
phpmodxmodx-evolution

Modx evo array value


Good evening all.

I'm currently rewriting my website and I'm trying to create a snippet that will output various values into an array within a MODX (Evolution) page. I've got a snippet known as SYSTEM_STATUS setup as follows;

<?php
    $statusfile = file_get_contents('URL_REMOVED');
    $statusarray = json_decode($statusfile, true);

    // Parsing omitted

    $_OUTPUTARR = Array('API_IMAGE' => $_APIDBIMAGE,
                        'API_MSG' => $_API_STATUS_MSG,
                        'API_COLOR' => $_APICOLOR,
                        'API_PING' => $statusarray['API_PING'],
                        'SITE_IMAGE' => $_SITEDBIMAGE,
                        'SITE_MSG' => $_SITE_STATUS_MSG,
                        'SITE_COLOR' => $_SITECOLOR,
                        'SITE_PING' => $statusarray['SITE_PING'],
                        'CDN_IMAGE' => $_CFDBIMAGE,
                        'CDN_MSG' => $_CF_STATUS_MSG,
                        'CDN_COLOR' => $_CDNCOLOR,
                        'CDN_PING' => $statusarray['SITE_CF_PING']);
    return $_OUTPUTARR;
?>

That snippet is called at the top of the page using a [[!SYSTEM_STATUS]] snippet tag. However, later in my page, I want to print some of these values from the array out to the page. For example, all _COLOR values are used to change the colour of a box indicating status (CSS property), all _IMAGE values are used to change the image, and all _PING and _MSG values are used to output a status message and a ping value.

What I want to know is how I go about 'extracting' the values from that array and printing them at relevant points in the page. Let's say I want to print the SITE_MSG value to the page as a string. How would I go about doing that in MODX Evolution?

I had assumed it would be something along the lines of [[!SYSTEM_STATUS $X=SITE_MSG]] but I'm honestly not sure whether that's correct - it feels like there's something more I'd need. I'm not sure if I need some chunks or additional snippets or whether I just need a tag with properties of some sort, so any help would be appreciated.


Solution

  • Ok, first off: if you are using Evolution then your snippet tags are wrong. You're using the Revolution tag syntax. They should be:

    • [!SYSTEM_STATUS!] (uncached)
    • [[SYSTEM_STATUS]] (cached)

    There are two ways to print values from a snippet onto a page.

    1 - You can return a value which will be printed exactly where the snippet tag appears on the page. Eg:

    // replaces [!my_snippet!] with 'text'
    return 'text';`
    

    2 - You can set placeholders to output values to different parts of the page.

    $modx->setPlaceholder('placeholder', 'some value');

    Your template might then be:

    [!my_snippet!]
    
    <p>The value returned by my snippet is: [+placeholder+]</p>
    

    You can set as many placeholders as you like.

    http://wiki.modxcms.com/index.php/API:setPlaceholder http://wiki.modxcms.com/index.php/Creating_Snippets http://wiki.modxcms.com/index.php/Snippet_call_anatomy