Forum

Thread tagged as: Question

How to use php script in .html (navigation template) page

Hi I want to use my perch syntax (<?php perch_content('Fonts color');?>) in the html page. I have already included ".htaccess" ( AddType application/x-httpd-php .php .htm .html ) file at the root of the folder but when I run the project and inspect the code, it shows as it is, as written php script in the html page.

Damian McCracken

Damian McCracken 0 points

  • 4 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

One of the fundamental ideas behind Perch is that you can't use PHP in your Perch templates. https://docs.grabaperch.com/perch/getting-started/fundamentals/

In this instance, I'm assuming "navigation template" means a .html file that is being passed into a perch_pages_navigation function (or similar)?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can pass the value into your template https://docs.grabaperch.com/templates/passing-variables-into-templates/

PerchSystem::set_var('font_color', perch_content('Fonts color', true));

What I am trying to do is to pass the color code value for in-line css in the template so that the color of the font can be controlled from perch admin panel.

<li>
<a class="hvr-underline-SUB-leftDro" href="<perch:pages id="pagePath" />" style="color:<?php perch_content('Fonts color');?>"><perch:pages id="pageNavText" /> </a>
</li>
Rachel Andrew

Rachel Andrew 394 points
Perch Support

Drew's suggestion should work for that, or take a look at Page Attributes in the docs. https://docs.grabaperch.com/functions/page-attributes/

I am using the below syntax

PerchSystem::set_var('font_color', perch_content('Fonts color', true));
 perch_pages_navigation(array(                       
                         'from-path' => '/',
                         'levels'    => 3,
                         'template' => array('level1.html', 'level2.html'), )); ?>

and using the below syntax in navigation template

<li><a class="hvr-underline-SUB-leftDro" href="<perch:pages id="pagePath" />" style="color:<perch:content id="font_color" />"><perch:pages id="pageNavText" /></a></li>

But this is not reflecting the color functionality on my site. On inspecting the code in developer mode I am getting the following html tag

<li><a class="hvr-underline-SUB-leftDro" href="https://xyz.com/" style="color:">Track Infrastructure</a></li>
Duncan Revell

Duncan Revell 78 points
Registered Developer

Where you have perch:content in your template, try changing that to perch:pages

<perch:pages id="font_color" />

Thanks for your valuable support. Now I am getting this html code in inspect window (developer mode). Here you can see "" string is getting appended before the color code value.

<ul class="dropdown-menu" style="background-color:&#65279;#ffff00
"> 

Duncan Revell

Duncan Revell 78 points
Registered Developer

I would think you would now need to check the template that is being used by the "Fonts colour" region - make sure there's nothing there that is adding &#65279;

Otherwise, check the value stored in the region - is there a possibility of a copy/paste mistake?