# Return html content of another page in controller

**URL:** <https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 6, 2018, 6:12pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898 "2018-03-06T18:12:54Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [March 6, 2018, 6:12pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/1 "2018-03-06T18:12:54Z")

</div>

Is there a way through Content Representation to get the html of a page and pass it as argument in a plugin running on a controller?

Plus: would it be possible to decide what to get from the html file? Eg fetching only the `<main>` tag and everything inside it?

thanks, af

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [March 6, 2018, 6:31pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/2 "2018-03-06T18:31:54Z")

</div>

I _think_ you can render the page via the `tpl::load` function ([more info](https://getkirby.com/docs/toolkit/api/tpl/load)). That way you wouldn’t need to do the hassle with setting up a “hack”-content representation, but could render the page into a variable inside the controller.

Only grabbing the `<main>` contents, would be possible with a regex on the returned string from the `tpl::load` function I think.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 6, 2018, 6:44pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/3 "2018-03-06T18:44:07Z")

</div>

The important part here is to set the third parameter to true, so that you can store your HTML in a variable:

```auto
$html = tpl::load(kirby()->roots()->templates() . '/template.php', $data, true);

```

You can also use `$kirby->render()`.

```auto
$html = $kirby->render(page('projects'));
echo $html;

```

---

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [March 6, 2018, 9:52pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/4 "2018-03-06T21:52:34Z")

</div>

thank you both (some more chars because otherwise i cannot post)

🙏

---

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [April 10, 2018, 3:46pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/5 "2018-04-10T15:46:24Z")

</div>

so i ended up testing both options only today and:

1. the `tpl::` approach spits back an error saying that `Call to a member function title() on null` from the code i have in my `header.php` snippet

2. the `$kirby->render` approach keeps saying it is rendering a null object, no matter which page or url i tried to load

the only reason i went back to this is that i was using `file_get_contents()` to call another page and render it through mPDF. But on the production server with SSL, this gives me an error (could be because is php 5.6 — i’m not the sysadmin in this case and can’t upgrade the server software stack).

it would be great if i did not have to work with ssl certificates and php, and just being able to fetch the html of this page to feed into the mPDF function 😃

thanks!

* * *

header.php

```auto
<!doctype html>
<html lang="<?= site()->language() ? site()->language()->code() : 'en' ?>">
<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">

  <title><?= $site->title()->html() ?><?php e($page->template() != 'home', ' • ' . $page->title()->html())?></title>
  <meta name="description" content="<?= $site->description()->html() ?>">

  <?= css('assets/dist/css/styles.min.css') ?>

</head>
<body id="t" class="pd-b--3 msc">
  <?php if($page->template() != 'order'): ?>
    <header class="z-2 pd-t--1 pd-b--1 pd-h--1 pd-h--2 __md p-st p--t p--l p--r x x-cl x-rw__ md x-abl bgc-wh" role="banner">
      <h1 class="d-ib ft-2 ft-3 __lg pd-r--2 pd-b--05 pd-b--0__ md"><a href="<?= $site->url() ?>" class="bd-b--0"><?= $site->title()->html() ?></a></h1>
      <?php snippet('nav') ?>
    </header>
  <?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 10, 2018, 7:31pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/6 "2018-04-10T19:31:03Z")

</div>

> [@afincato](#):
>
> the tpl:: approach spits back an error saying that Call to a member function title() on null from the code i have in my header.php snippet

The problem with using the `tpl::load()` method is that you have to pass all data first down to the template in the $data variable and then again down to the snippets.

Can’t you grab the HTML via Ajax?

---

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [April 11, 2018, 8:50am UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/7 "2018-04-11T08:50:45Z")

</div>

Thank you for clarifying.

What about the `$kirby->render()` mode? Why is it throwing an error?  
Also, I can’t find any documentation about it.

Yes, I can probably use AJAX for this as well (which I never used before tbh), just was trying to avoid extra overheads and keep it as basic as possible. I have the feeling AJAX might throw some CORS problem or some SSL-related hiccups.

Will look into it!

af

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 11, 2018, 9:08am UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/8 "2018-04-11T09:08:22Z")

</div>

> [@afincato](#):
>
> What about the $kirby-\>render() mode? Why is it throwing an error?
> 
> Also, I can’t find any documentation about it.

I don’t know, it doesn’t in my setting if I simply add the two lines I posted above into another template.

---

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [April 11, 2018, 9:14am UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/9 "2018-04-11T09:14:52Z")

</div>

does it change the fact that i am calling `$kirby->render()` on a controller?

OK, working perfectly on a template, the dumb error is that in a controller there is no `$kirby` variable — and feeding that into the mPDF plugin directly was hiding the kirby error.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 11, 2018, 9:21am UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/10 "2018-04-11T09:21:41Z")

</div>

In your controller, $kirby doesn’t seem to be defined:

```auto
$html = kirby()->render(page('projects'));

```

---

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [April 11, 2018, 9:32am UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/11 "2018-04-11T09:32:38Z")

</div>

> [@texnixe](#):
>
> $html = kirby()-\>render(page(‘projects’));

Thank you! It works now.

---

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [April 11, 2018, 10:30am UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/12 "2018-04-11T10:30:35Z")

</div>

hey sorry, bringing this up again.

so the `kirby()->render()` works perfectly in certain pages, and not in others. i debugged a bit, changing bits here and there and see if it was making a difference, but it seems it doesn’t and i have no clear idea of why it is like this.

the error i get is

```auto
Call to a member function price() on boolean

```

this is the snippet of code:

```auto
<?php
  $price_t = 0;
  foreach($o_desc as $item) {
    // this line ↓ //
    $item_price = page('shop/' . str::slug($item[0]))->price();
    $price_s = str::replace($item_price, ['€', ','], '');
    $price_t += $price_s * $item[1];
  };
 
  $vat = round((6 / 100) * $price_t, 2);
  $price_vat = round($price_t + $vat, 2);
?>

```

it sounds a bit similar to this other error i was having when using the `tpl::` method

```auto
Call to a member function title() on null

```

but in this case I don’t get why in certain pages it works and not in others…

let me know if you need more context for this!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 11, 2018, 10:40am UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/13 "2018-04-11T10:40:36Z")

</div>

The error message means that you don’t have a page object but a boolean here, so obviously this bit

```auto
page('shop/' . str::slug($item[0]))

```

doesn’t give you a page. Since I don’t know what the $o\_desc array or each $item contains, I can’t tell. But I can’t repeat it often enough:

NEVER call an object’s method without making sure you really have an object! Then at least you don’t get an error message (even if it means you don’t get the expected result either).

---

<div class="post-metadata">

**Author:** ![stinkstiefel](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/stinkstiefel/32/6766_2.png) [@stinkstiefel](https://forum.getkirby.com/u/stinkstiefel)\
**Post date:** [May 16, 2019, 7:01pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/14 "2019-05-16T19:01:41Z")

</div>

Hi, just in case someone needs it. In Kirby3 you can use the following solution to get the html of a page based on its ID.

```
$single_page = $pages->findById('test');
$html = $single_page->render([
        'name' => 'John Doe'
]);

```

You can have access to this variables in the template just by using ‘$name’ and in the controller you have to pass it through.

```
return function ($page, $name) {
    $headline = $page->headline()->kt();
    $subheadline = $page->subheadline()->kt();
    return [
        'header' => $headline . ' - ' . $subheadline . ' : ' . $name
    ];
};
```

---

<div class="post-metadata">

**Author:** ![stinkstiefel](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/stinkstiefel/32/6766_2.png) [@stinkstiefel](https://forum.getkirby.com/u/stinkstiefel)\
**Post date:** [May 16, 2019, 7:30pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/15 "2019-05-16T19:30:02Z")

</div>

… and for rendering content representations you can use the option contentType -\> [https://getkirby.com/docs/reference/objects/page/render](https://getkirby.com/docs/reference/objects/page/render) e.g.

```
$html = $page->render([
    'variables' => $variables
], 'txt');
```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:23pm UTC](https://forum.getkirby.com/t/return-html-content-of-another-page-in-controller/9898/16 "2025-02-06T17:23:14Z")

</div>


