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

**URL:** <https://community.modx.com/t/solved-saving-the-parsed-body-html-content-into-a-variable/5113>\
**Category:** Support\
**Created:** [March 29, 2022, 6:28pm UTC](https://community.modx.com/t/solved-saving-the-parsed-body-html-content-into-a-variable/5113 "2022-03-29T18:28:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ysanmiguel](https://community.modx.com/user_avatar/community.modx.com/ysanmiguel/32/285_2.png) [@ysanmiguel](https://community.modx.com/u/ysanmiguel)\
**Post date:** [March 29, 2022, 6:28pm UTC](https://community.modx.com/t/solved-saving-the-parsed-body-html-content-into-a-variable/5113/1 "2022-03-29T18:28:50Z")

</div>

### Summary

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!

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [March 29, 2022, 6:55pm UTC](https://community.modx.com/t/solved-saving-the-parsed-body-html-content-into-a-variable/5113/2 "2022-03-29T18:55:19Z")

</div>

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:

> <https://github.com/opengeek/renderResources/blob/8b9bfcc47b6c6ff945c8339fb6d88ce73bf7e87d/core/components/renderresources/snippet.renderresources.php#L448-L450>

---

<div class="post-metadata">

**Author:** ![ysanmiguel](https://community.modx.com/user_avatar/community.modx.com/ysanmiguel/32/285_2.png) [@ysanmiguel](https://community.modx.com/u/ysanmiguel)\
**Post date:** [March 31, 2022, 8:10am UTC](https://community.modx.com/t/solved-saving-the-parsed-body-html-content-into-a-variable/5113/3 "2022-03-31T08:10:10Z")

</div>

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

```auto
//$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.

Cheers!!!

---

<div class="post-metadata">

**Author:** ![system](https://community.modx.com/user_avatar/community.modx.com/system/32/2_2.png) [@system](https://community.modx.com/u/system)\
**Post date:** [April 2, 2022, 8:10am UTC](https://community.modx.com/t/solved-saving-the-parsed-body-html-content-into-a-variable/5113/4 "2022-04-02T08:10:39Z")

</div>

Closed on April 2, 2022, 8:10am UTC

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”.
