How do I insert php code into my markdown files

Hello Hugoers

I am struggling to get my php code rendered in a markdown file. Did anyone try to be naughty this way :smiley: ? 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 !!

File - content/_index.md

---
title: ""
date: 2018-11-12T16:58:05Z
draft: false
outputs:
- PHP
---

{{ define "php" }}`
    {{ "<?php" | safeHTML }}
        echo "Hello World";
    {{ "?>" }}
{{ end }}

File - layouts/_default/baseof.php

{{ block "php_code" . }}

{{ end }}
<!doctype html>

ouput

<p>{{ &ldquo;&lt;?php&rdquo; | safeHTML }}

echo &ldquo;Hello World&rdquo;;

{{ &ldquo;?&gt;&rdquo; }}</p>

<!doctype html>

<html lang=ā€œenā€>

Where am I going wrong ?

1 Like

Since you want to use PHP in your markdown (as opposed to your template), you need to use a shortcode.

Say you named it layouts/shortcodes/php.html.

Define it as:

{{ .Inner | safeHTML }}

Then use it like:

{{< php >}}
<?php echo "<p>Hello World</p>"; ?> 
{{< /php >}}

Note, wrote this from my phone, so itā€™s untested.


Update: Tested this from my laptop. It works.

3 Likes

Thanks @zwbetz

Works perfectly fine as you explained.

Is there any way I can inject that php into {{ define ā€œphp_codeā€ }} block ?
Basically I want that to come before the <!doctype html>

You would need to edit one of your templates for this.

Do you want it to show on all pages or only one/some?

Also are you using a theme or a custom layout?

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 }}

Something like is what I am trying to achieve.

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>

For a deeper dive checkout the docs on base templates.

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

baseof.php

{{$file := .Get .Params.php_file }}
{{ $file  | readFile | safeHTML }}

_index.md

---
title: ""
php_file: "/static/class/test.php"
---

Will it work this wayā€¦ ? Havenā€™t tested it out thoughā€¦

Just tried that codeā€¦

Getting this error

execute of template failed: template: index.php:2:12: executing "php_code" at &lt;.Get&gt;: can't evaluate field Get in type *hugolib.PageOutput

Can hugo access files from static directory ?

@zwbetz

Thanks for the help buddyā€¦ I was able to inject the php code using this code block

{{ print .Dir .Params.php_file | readFile | safeHTML }}

<doctype html>

Sadly I couldnā€™t figure why I canā€™t run go template code in markdown files i,e

{{- block "php_code" . -}}

{{- end -}}

This functionality is supported by middleman. For ex

In markdown files

<% content_for :php do %>
 // anything
<% end %>

I wish hugo supports thisā€¦ Or may be I couldnā€™t figure it out yet.

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.

Make sense?

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.

As far as I know, it does not support that.

But why do you need it? You can do the equivalent thing using a shortcode.

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 :slight_smile:

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.

Thanks @zwbetz

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ā€¦

Best Regards