I am struggling to get my php code rendered in a markdown file. Did anyone try to be naughty this way ? I tried many topics from the forum but none of them seemed to work me. Is it because I am trying to include php in markdown file ?
Thanks in advance!! Hugo Rocks at Super Sonic Speeds !!
As of now my intention is to include standard php code at the begining of each php page generated by different files from the content directory (markdown files)
I am not using any themeā¦ I am trying to come up with one, which can handle php code as well.
baseof.php
{{- block "php_code" . -}}
{{- end -}}
<!doctype html>
content/_index.md
---
title: ""
date: 2018-11-12T16:58:05Z
draft: false
outputs:
- PHP
---
<!-- some php code in the body tag -->
{{< php >}}
<?php echo "Hello World"; ?>
{{< /php >}}
<!-- other php code that I want in before Doctype -->
{{ define "php_code" }}
<?php echo "Hello Before Doctype html" ?>
{{ end }}
You could do something like this in your baseof.html, which would include this PHP code on every page.
{{ "<?php echo \"Hello Before Doctype html\" ?>" | safeHTML }}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ block "title" . }}
<!-- Blocks may include default content. -->
{{ .Site.Title }}
{{ end }}</title>
</head>
<body>
<!-- Code that all your templates share, like a header -->
{{ block "main" . }}
<!-- The part of the page that begins to differ between templates -->
{{ end }}
{{ block "footer" . }}
<!-- More shared code, perhaps a footer but that can be overridden if need be in -->
{{ end }}
</body>
</html>
This should work fine for normal use cases. But I was wondering just in case if I had to inject a different php code into that section through different files like [ācontent/_index.mdā, ācontent/_page1.mdā, ācontent/_page2.mdā]
So that this way I can make that code block dynamic which can accept any php code.
I was also going through the readFile option, if it can make the functionality dynamic. For example
So, I think you may be confusing how to do this in Template files vs Content files.
As mentioned previously, you can run go template code in markdown (content) files by using a shortcode.
This code snippet below you are referencing, needs the āother piecesā to work. So letās say you had this snippet in your layouts/_default/baseof.html
{{- block "php_code" . -}}{{- end -}}
You would then need to define it. Letās say you defined it in your layouts/post/single.html. By doing this, it would be included in all pages of type āpostā.
{{- define "php_code" -}}
<!-- mysterious php things here -->
{{- end -}}
If you instead defined it in your layouts/_default/single.html, it would be included in all single pages/posts.
Yes it perfectly makes sense and it actually works. I was just wondering if Hugo supports the below code in /content/{ āpage-1.mdā, āpage-2.mdā } instead of /layouts/_default/single.html
{{ - define "php_code" - }}
<!-- mysterious php things here LOL :D -->
{{- end -}}
Because right now, when I include the above 3 lines in content/page-1.md, Markdownify seems to be escaping it / sanitizing the special characters.
I actually want page specific php code to execute / check certain conditions before the actual page loads. In order to do that I need to place different php codes at the beginning of the page.
shortcodes will the do the job but i canāt place them in the intended position. (i.e at the beginning of the page instead of including it in the body)
As of now the only thing that supports it is the āblockā and ādefineā from Hugo templates, but only in layouts.
I Think it should be fine for now as I can use shortcodes for the markdown and use Params to readFile and print its content. (This is working for now)
I think Hugo is getting better day by day and I am really amazed with its blazing fast build speeds. I am doing some leisure time testing to make use of this speed to see if I can actually avoid using bulky php frameworks and keep things simple and organized yet dynamic(PHP) using Hugo. To be honest I think Hugo does it well.
Also, Appreciate your support in helping me figure out things. Kudos to you
I see what you are asking now. So there are a few ways to do this
PHP code in front matter param
Letās say your markdown file has the literal PHP code in its front matter. Notice how the snippet has to be surrounded by quotes since itās a string, and the inner quotes are escaped.
---
phpcode: "<?php echo \"Hello World\"; ?>"
---
Then in your template you would add this:
{{ with .Params.phpcode }}
{{- . | safeHTML -}}
{{ end }}
<!doctype html>
By using with, the PHP code is only included if the phpcode param is set in the pageās front matter.
PHP file in front matter param
As you hinted to earlier, you could also reference a PHP file from a front matter param.
Say a file existed at static/file.txt, with contents:
<?php echo "Hello World"; ?>
Then in your pageās front matter:
---
phpfile: "static/file.txt"
---
Then in your template:
{{ with .Params.phpfile }}
{{- . | readFile | safeHTML -}}
{{ end }}
<!doctype html>
As mentioned, since using with, this will only run if the phpfile param is set in the pageās front matter.
Edit: If you use this way, perhaps donāt name the file with a .php extension, so that it isnāt unintentionally executed.
That sounds promising to resolve my issue. Tested it out a while ago and both option seem to work. This should get me going for couple more days while I scratch my head to test out localization. I hope it would be easierā¦