[Solved] Saving the parsed <body> HTML content into a variable?


Hello everyone, I’m building a plugin that runs on OnDocFormSave, in one of the steps I created a file and save the html from [[*content]] inside it.

fwrite($file, $resource->get('content'));

Everything fine until there, but what I really need is the full html inside the <body></body> tags.

I understand the template has not been parsed in that stage of the process (when click save the resource in the manager) and it will be when I open the saved page in the browser and save id in cache, but how can I get the parsed html and save it into a variable to replace the:

fwrite($file, $resource->get('content'));

for a new variable full of beautiful full processed html?

fwrite($file, $theSuperVarible);

Any help will be welcome, thanks!

Maybe you can take a look at the code of the renderResources extra and see how it’s done there.
Something similar to this may work:

OK done, thanks @halftrainedharry for your help mate, here is the code:

//$scriptProperties['id'] is how you can get the resource ID in the plugin
//this is because the plugin runs OnDocFormSave, that means when I save the resource.
$id = $scriptProperties['id'];
$resource = $modx->getObject('modResource', ['id' => $id]);
//save the resource in the modx var
$modx->resource = $resource;
//save the resoruce html in a var
$html = $modx->resource->process();

//get only the content inside <body>...</body>
preg_match("/<body[^>]*>(.*?)<\/body>/is", $html, $matches);
//include the missing <body> tags and include the result from prev process
$content = "<body>" . $matches[1] . "</body>";

// Get the modParser instance and parce $content
$parser = $modx->getParser();
$maxIterations= (integer) $modx->getOption('parser_max_iterations', null, 10);
$parser->processElementTags('', $content, false, false, '[[', ']]', [], $maxIterations);
$parser->processElementTags('', $content, true, true, '[[', ']]', [], $maxIterations);

//create file with html
$file = fopen($purgePath . '/index.tmp', 'w');
fwrite($file, $content);

with this I got what I needed that is just the html already processed and ready to use saved in a file.


This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.