We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45533
    • 23 Posts
    Hello,

    i try to pass some Variables from modx to a JS "Chunk".

    This Chunk is Part of a Extra Package - so i want to keep this in a static File instead of the Database.

    I've a local "getChunk" Method in my base Class where i pass a TPL Type into it - if its set to "File" i load the Chunk like this:

    $output .= file_get_contents($chunkKey);
    $this->modx->setPlaceholders($properties);
    


    That works normal if i load a Chunk just for output its contents somewhere... if i load a Chunk like this - to register it as Script - for example:

    $script = $this->snuffbox->getChunk($this->parseChunkKey('market-group-select-treescr'), $scriptFields, 'file');
    $this->modx->regClientStartupScript($script);
    


    The Placeholders does'nt get set... (they does, if i just output the chunk for normal output).

    If i do the same with with a database chunk

    $script = $this->snuffbox->getChunk('testJS', $scriptFields, 'modChunk');
    


    then it works.

    Can anyone explain why ?
      • 3749
      • 24,544 Posts
      getChunk() takes two arguments and you are passing it three. The first one is the name of the chunk to get, so unless $this->parseChunkKey() returns the name of an actual chunk, it's not going to work. The third argument in both your getChunk() calls will be ignored (and should be removed).

      One way to process the placeholders in a file is to do this:

      $chunk = newObject('modChunk');
      $chunk->setContent(file_get_contents($path));
      $output = $chunk->process($properties);
      unset($chunk);
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 45533
        • 23 Posts
        i dont call get chunk directly there... i call a method in my base class which i extend (its a controller class).....

        my get chunk function looks like this:

        
        public function getChunk($chunkKey,$properties,$type = 'modChunk') {
                $output = '';
                switch ($type) {
                    case 'embedded':
                        if (!$this->modx->user->isAuthenticated($this->modx->context->get('key'))) {
                            $this->modx->setPlaceholders($properties);
                        }
                        break;
                    case 'modChunk':
                        $output .= $this->modx->getChunk($chunkKey, $properties);
                        break;
                    case 'file':
                        $name = str_replace(array(
                            '{base_path}',
                            '{assets_path}',
                            '{core_path}',
                        ),array(
                            $this->modx->getOption('base_path'),
                            $this->modx->getOption('assets_path'),
                            $this->modx->getOption('core_path'),
                        ),$name);
                        $output .= file_get_contents($chunkKey);
                        $this->modx->setPlaceholders($properties);
                        break;
                    case 'inline':
                    default:
                        $chunk = $this->modx->newObject('modChunk');
                        $chunk->setContent($chunkKey);
                        $chunk->setCacheable(false);
                        $output .= $chunk->process($properties);
                        break;
                }
                return $output;
        
        


        parseChunkKey just resolves a fileName ... so its basically just returning a string...
          • 3749
          • 24,544 Posts
          Sorry, I didn't notice that you were calling your own getChunk() method.

          In the 'file' case, you're doing a lot of work with the $name variable, but never using it. Should it be $chunkKey?

          Also, if this is running inside MODX, you can use the constants: MODX_BASE_PATH, MODX_CORE_PATH, MODX_ASSETS_PATH, which will be faster. Remember that they all end in slashes, so make sure there isn't an extra one when you pass the argument.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 45533
            • 23 Posts
            yeah in reality the chunkKey is called "name" - i was just quick editing this - cause i used chunkKey in my first Example...


            in some situations that code needs to run outside of modx aswell - but i was'nt aware that it would be much faster... i will review this.

            what i really dont understand is - that setPlaceholders does'nt work for regClientScript (or Startupscript) - when a file Chunk is loaded... (it works like a Charm for modChunk)...

            maybe i should change the file Case completly to your setContent way... (hav'nt checked yet - if that works for my specific Problem - but i guess it will... cause the normal modChunk Case works).

            In that few Situations where i need to string replace things in a file Chunk i just could do it with a different method...

              • 3749
              • 24,544 Posts
              Either getChunk() or $chunk->process() will replace the placeholders from the $fields array in the second argument as long as the $field keys match the placeholders and the placeholders are in this form:

              [[+placeholdername]]


              Once that happens, regClientStartupScript() should work as you want it to. Do a view source to see if you're missing any placeholders. Outside MODX, you should see the placeholder tags if they're not replaced. Inside MODX, they'll be removed.

              setPlaceholders() will only work inside MODX, since MODX replaces the placeholders when the page is rendered.

              For the constants (MODX_CORE_PATH, etc.) to work outside of MODX, you need to include core/config/config.inc.php.

              Outside of MODX, for any calls to $modx->, (including $modx->newObject()), you'll need to instantiate MODX. See this: http://bobsguides.com/blog.html/2013/07/13/using-modx-outside-of-modx/.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting