Return full page html as string

Hi,

is it possible to return the whole $page as string.
I want to load a page with ajax not only the data also the template.
What is the best way to do this?

I have my about.json.php file to load the page as json object, like this:

$data[‘page’] = $page->html();
echo json_encode($data);

This but with a whole template:

$html .= snippet(‘footer’, compact(‘footer’), true);

Is that possbile?

Thx for help.

$string = $kirby->render(page('blog'));

cool thanks alot, this works with every page! Without the about page :wink:
/about.json

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@ to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

What is the error message if you enable debugging?

<b>Fatal error</b>: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32768 bytes) in <b>/kirby/vendor/filp/whoops/src/Whoops/Util/Misc.php</b> on line <b>1</b><br />

<b>Fatal error</b>: Allowed memory size of 268435456 bytes exhausted (tried to allocate 262144 bytes) in <b>/kirby/vendor/composer/ClassLoader.php</b> on line <b>329</b><br />

You probably need to increase the amount of ram available to PHP (in the php.ini file) - however you just managed to use 268 megabytes of ram! wha exactly is on the pages??? Thats pretty high. is there alot of images involved?

I wonder if it is running into a loop, at least this is what I get when I try to render a page into itself.

Hi,
it’s the basic kirby about page ;).
Maybe it runs in a loop cause i try to pass the about page in the about.json?

Yes, that might be the poblem. I think the render method is then called within the render method for the same page, resulting in the loop.maybe it would make sense to use routes rather then content reps here. But why do you need completely rendered templates instead of snippets?

Hi,

we try to build the page loads with ajax but using the whole kirby cms to manage the content.
But yeah, you are right it’s also possible to just use snippets as page content templates.

And what’s about the routing, what is the best way for - route: page/about use home template with snippet about?

thx for your help and answers

What I suggested were two different options, with a route you could do it like this:

c::set('routes', [
  [
    'pattern' => 'about',
    'action'  => function() {
      $data = [];
      $html = kirby()->render(page('about'));
      $data['html'] = $html;
      return response::json(json_encode($data));
    }
  ]
]);

You can also call the route about.json, if you want to use about in a regular fashion.

thx alot! is this possbile?

c::set('routes', [
  [
    'pattern' => 'about',
    'action'  => function() {
      
    $data = about snippet

      return array('home', $data);
    }
  ]
]);

and in the home template i want to load the about snippet now.

Yes, you can assign a snippet to a variable by setting the third option of the snippet helper to true:

$data = snippet('about', [], true);

You would have to assign the data that is used in the snippet, though, for example, the page and other variables you use within that snippet.

$data = snippet('about', ['page' => page('about')], true);
1 Like

Hi texnixe,

i have to put the $data into an array to prevent this error.

array_merge(): Argument #2 is not an array

$data = snippet(‘about’, [], true);
return array(‘home’, [$data]);

But how to access the data in the home template now?

In the home controller, you can add a fourth $args argument to access data passed through a route.

https://getkirby.com/docs/developer-guide/advanced/controllers#arguments-from-the-router

okay, i’ll try, thx alot!

I try to display the about snippet in the about template.

<?php snippet('about', ['page' => page('about')], false) ?>

But i am always getting this error:
<h1><?= $page->title()->html() ?></h1>
Undefined variable: page

What goes wrong, i also tried it with:

<?php snippet('about', ['page' => page)], false) ?>

and

<?php snippet('about', ['page' => $page], false) ?>

without success.

Sorry, I have lost track. Didn’t you say you wanted to display the snippet in the home template?

Hi,
yes in both, in the home with ajax load and in the about page as normal snippet.

I think I have to see how the code plays together, something is wrong here. If you want, send. me a link to a zip via PM.