Is there a way to add php code to template?

Hello,

I want to add php code to my search.html.

I just put <?php to my template file. But Hugo generate a &lt;?php.

So I need make another program to change this.

Is there a way to add php code to template?

Thanks for reply.

Try surrounding your PHP code with double quotes and piping to safeHTML

{{ "<?php" | safeHTML }}
2 Likes

Thank you very much. It works.

Am curious about your workflow. So you are building your hugo site then copying the generated files to a PHP web server?

Yes. I just migarated my wordpress site to Hugo a few days ago.
I made git prod master on my nginx web server and update everything smoothly with just one deploy script.
But only search function need php to connect my manticoresearch database.

Oh interesting

Just found that it was a security hole if web server allow html to run php code.

And hugo theme makers can include php code inside the html files.

Please make sure that web server just allow php file to run php code if anybody need the same solution as me.

For my case, I will make a html file with php function with powerful Hugo but I will also make a bash script to rename the html file to php file before I upload the whole site to my web server.