Mixing PHP into Hugo

Hugo’s Homepage

Hugo is nice because of its blog template system, static site generation abilities and generic framework structure, but sometimes you may want to add extra functionality that requires dynamic The unholy union of PHP and Hugo.

If you are also combining static elements with dynamic functions you may want to DRY your views to make everything consistent. It could be that you only want the blog of your website to be static and everything else to operate dynamically.

We can generate files ending in .php using the media types option in Hugo, however because the template is in the layouts/_default directory, we can simply inject something like this directly within the .html This is not recommended if you do not have complete control of your stack.

go-html-template
{{ safeHTML "<?php require $_SERVER['DOCUMENT_ROOT'] . '/..' . '/views/partials/navigator.php'; ?>" }}

This tells Hugo when compiling to insert the above PHP code as safe HTML into the statically generated When in the context of an HTML attribute you can use safeHTMLAttr. If your web server is set up to parse .html files as PHP then you are good to go.

cfg
# nginx let fastcgi parse .php, .html, and .htm
location ~ \.(php|html|htm)$ {
    include default.d/php_fastcgi.conf;
}

More tricks can be had by carefully tiptoeing around the Go This is blasphemy … is what you are probably thinking. You get the best (or worst) of both This is a partial snippet of an image shortcode which automatically sets height and width using PHP to prevent image reflow. This can also be done with plain imageConfig.

go-html-template
{{ safeHTML "<?php" }}
$width = getimagesize($_SERVER['DOCUMENT_ROOT'] . '{{.Get `source`}}')[0];
$height = getimagesize($_SERVER['DOCUMENT_ROOT'] . '{{.Get `source`}}')[1];
$ratio = ((($height / $width) * 100) > 100) ? $height . 'px' : $height / $width * 100 . '%';
{{ safeHTML "?>" }}

<img data-image-zoom src="{{.Get `source`}}" alt="{{.Get `title`}}" title="{{.Get `title`}}"
{{ safeHTMLAttr `<?php echo 'width=' . '"' . $width . '"'; ?>` }}
{{ safeHTMLAttr `<?php echo 'height=' . '"' . $height . '"'; ?>` }} />

If you are interested in producing .php pages with Hugo, take a look at using custom output formats. Note that since Hugo version 0.44 the MediaType.Suffix is deprecated and replaced with a plural version, MediaType.Suffixes

1 August 2018 — Written
18 May 2019 — Updated
Thedro Neely — Creator
mixing-php-into-hugo.md — Article

More Content

Openring

Web Ring

Comments

References

  1. https://thedroneely.com/git/
  2. https://thedroneely.com/
  3. https://thedroneely.com/posts/
  4. https://thedroneely.com/projects/
  5. https://thedroneely.com/about/
  6. https://thedroneely.com/contact/
  7. https://thedroneely.com/abstracts/
  8. https://ko-fi.com/thedroneely
  9. https://thedroneely.com/tags/hugo/
  10. https://thedroneely.com/tags/php/
  11. https://thedroneely.com/tags/webdev/
  12. https://thedroneely.com/posts/mixing-php-into-hugo/#isso-thread
  13. https://thedroneely.com/posts/rss.xml
  14. https://thedroneely.com/images/mixing-php-into-hugo.png
  15. https://gohugo.io/
  16. https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
  17. https://gohugo.io/templates/output-formats#media-types
  18. https://thedroneely.com/posts/mixing-php-into-hugo/#code-block-2a35169
  19. https://gohugo.io/functions/safehtml/
  20. https://gohugo.io/functions/safehtmlattr/
  21. https://thedroneely.com/posts/mixing-php-into-hugo/#code-block-d8527ef
  22. https://www.thedroneely.com/git/thedroneely/thedroneely.com/tree/generators/hugo/themes/tdro/layouts/shortcodes/sideimage.html?id=3c0f9c3df4063413ee2ea96c237e990073d85e91
  23. https://gohugo.io/functions/images#imageconfig
  24. https://thedroneely.com/posts/mixing-php-into-hugo/#code-block-89133fa
  25. https://gohugo.io/templates/output-formats/
  26. https://gohugo.io/news/0.44-relnotes/
  27. https://www.thedroneely.com/posts/mixing-php-into-hugo.md
  28. https://thedroneely.com/posts/writing-nixos-modules-and-switching-to-cgit/
  29. https://thedroneely.com/posts/nixos-in-the-wild/
  30. https://thedroneely.com/posts/tailwind-css-and-beyond/
  31. https://git.sr.ht/~sircmpwn/openring
  32. https://drewdevault.com/2022/11/12/In-praise-of-Plan-9.html
  33. https://drewdevault.com/
  34. https://mxb.dev/blog/the-indieweb-for-everyone/
  35. https://mxb.dev/
  36. https://www.taniarascia.com/simplifying-drag-and-drop/
  37. https://www.taniarascia.com/
  38. https://thedroneely.com/posts/mixing-php-into-hugo#isso-thread
  39. https://thedroneely.com/posts/mixing-php-into-hugo#code-block-2a35169
  40. https://thedroneely.com/posts/mixing-php-into-hugo#code-block-d8527ef
  41. https://thedroneely.com/posts/mixing-php-into-hugo#code-block-89133fa
  42. https://thedroneely.com/posts/a-few-abstracts/
  43. https://thedroneely.com/posts/a-few-linux-distributions/
  44. https://thedroneely.com/posts/headless-gpu-remote-acceleration/
  45. https://thedroneely.com/projects/micro-blog/
  46. https://thedroneely.com/posts/improving-paperless-interface/
  47. https://drewdevault.com/2022/09/16/Open-source-matters.html
  48. https://mxb.dev/blog/make-free-stuff/
  49. https://thedroneely.com/sitemap.xml
  50. https://thedroneely.com/index.json
  51. https://thedroneely.com/resume/
  52. https://gitlab.com/tdro
  53. https://github.com/tdro
  54. https://codeberg.org/tdro
  55. https://thedroneely.com/analytics
  56. https://thedroneely.com/posts/mixing-php-into-hugo#
  57. https://creativecommons.org/licenses/by-sa/2.0/
  58. https://thedroneely.com/git/thedroneely/thedroneely.com
  59. https://opensource.org/licenses/GPL-3.0
  60. https://www.thedroneely.com/
  61. https://thedroneely.com/posts/mixing-php-into-hugo/#