risingisland Thanks. I tried <script src="[[snippet]]"></script>, but end up with it outputting the js blob inside of the src="", just like the chunk method. I dont really want to add it to the tree, so I think I will pass on creating a doc with media type.
This is possible, but in a very convoluted way. You can make the
snippet code to be:
$js = "alert( 'Hello, world!' );";
$path = "assets/js/script.js"
file_put_contents(MODX_BASE_PATH . $path, $js);
So what this does is that it writes the content of the
$js variable to
assets/js/script.js and returns the path to that file. One can get smart here by not always overwriting the whole file, but only if there were some changes. You can detect changes by e.g. comparing hashes of the variable
$js and contents of the file, but probably also just caching the snippet would make the trick.
What to beware: please be very aware about the quotation marks you use in your JS code, since it is treated here as a string inside PHP.
Why this solution: calling JS via src requires there to be some resource which is independently accessible. If you don't want to add this to the resource tree, then there has to be some file. So probably the best option is to just generate said file via PHP.
Side question: is there any specific reason why you want to have JS code accessible inside Evo?