Display content from HTML file on a page?

I am at the stage of adding content to a site. The content, which was created in another program, consists of an HTML file and a folder of images.

Is it possible to add these as files to a page then have kirby use the content of the HTML file on the page?

Thanks

Yup. I’m doing that on my site to handle legacy content.

content/
    old-article/
        index.html
        meta.txt

In the meta.txt I have a ContentFile field like this:

----
ContentFile: index.html
----

Then with a meta.php template it’s trivial to show the contents of the HTML file:

<?php

if ($file = $page->contentfile()->toFile()) {
  $file->show();
}
1 Like

Please note that currently Kirby does not allow uploading .html files via the Panel; you would have to add them to a folder manually.

1 Like

@fvsch How do you deal with images in that scenario?

Depends what “dealing with images” means. Just showing an image when the original HTML says <img src="something.jpg" alt="">?

In my case I have a rather specific setup where I don’t use numbered page folders, and I’m serving the content folder as my web root, so /some/page/something.jpg serves the content/some/page/something.jpg file directly. I’m “only” using Kirby to render a page if the request did not match an existing file.

In a more standard setup, you could rewrite the HTML, using a list of known files:

<?php

// Output the HTML source, replacing "image.jpg" with e.g. "/content/page/image.jpg"
if ($content = $page->contentfile()->toFile()) {
  $fileMap = [];
  foreach($page->files() as $file) {
    $fileMap[ $file->filename() ] = $file->url();
  }
  $html = str_replace(
    array_keys($fileMap),
    array_values($fileMap),
    $content->read()
  );
  $content->headers();
  echo $html;
}
1 Like

Is it possible to pass the content of a md file through kirbytext() so that image links work?
Something like this?

<?php
if ($file = $page->contentfile()->toFile()) {
  $file->show()->kirbytext();
}?>

At the moment I just get the text:

Thanks

Try with the kirbtext helper (untested)

echo kirbytext($file->show());

I get the same as before unfortunately…hmmm