Forum

Thread tagged as: Question, Problem

HTML Template with PHP code

Hello, is it possible to integrate a line of PHP code in my HTML Content Template ?

in the content Template, i have a Perch tag for a Persons Initials <perch:if exists="initials"> <a class="anker" name="<perch:content id="initials" type="text" label="Initialen" order="4" />"></a> </perch:if>

in the same template, i want to reuse the "initials" for a line of PHP to display some content from a database: <? lehrerPlan('INITIALS'); ?>

how can I do this ?

thank you, katja

~~~

Summary information

Perch: 2.8.24, PHP: 5.6.30, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $, with PDO
Server OS: Linux, cgi-fcgi
Installed apps: content (2.8.24), assets (2.8.24), categories (2.8.24), perch_mailchimp (2.0.1)
App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_blog', 'perch_mailchimp', );
PERCH_LOGINPATH: /perch
PERCH_PATH: /homepages/12/d125864790/htdocs/studios/perch
PERCH_CORE: /homepages/12/d125864790/htdocs/studios/perch/core
PERCH_RESFILEPATH: /homepages/12/d125864790/htdocs/studios/perch/resources
Image manipulation: GD
PHP limits: Max upload 64M, Max POST 64M, Memory: 256M, Total max file upload: 64M
F1: dc1fef2ad0fcd9f943c02ebb43d85dbc
Resource folder writeable: Yes
HTTP_HOST: patrickbroome.de
DOCUMENT_ROOT: /kunden/homepages/12/d125864790/htdocs/studios
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php

is there any way to

Katja Schwarz

Katja Schwarz 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

The best you can do in Perch 2 is call a layout from the template and put your code in that.

In Perch 3 you could turn it into a template filter.

thanks, i am glad there is a way where would i find this in the documentation ?

best regards ! katja

i found out how to refer to a layout PHP page

but i can't work out how to include the perch Tag into the layout page

<? lehrerPlan('XX'); ?>

when i put <perch:content id="initials" type="text" label="Initialen" /> in place of XX, it doesn't work ...

Drew McLellan

Drew McLellan 2638 points
Perch Support

Thank you Drew, i found this

but now i can't work out how to include the perch Tag into the PHP layout page

<? lehrerPlan('XX'); ?>

when i put <perch:content id="initials" type="text" label="Initialen" /> in place of XX, it doesn't work ...

Katja,

In order to send variable to layout from template

<perch:layout path="path_to_template.php" variable1="this_is_var1" variable2="this_is_var_2" />

in layout template use

<?php
    if (perch_layout_has('variable1')) {
        perch_layout_var('variable1');
    }else{
        echo "Nope, not here!";
    }
?>

to pass back into template use

<?php PerchSystem::set_var('variable1', 'send_into_template'); ?>

thank you for your explanation, but i don't quite understand, as i am not a PHP programmer, sorry !

my perch Tag is <perch:content id="initialen" type="text" label="Initialen" order="4" />

so the users input from "initialen" would be my variable, right ?

when i send <perch:layout path="path_to_template.php" variable1="initialen" /> to layout PHP, it does not work

Drew McLellan

Drew McLellan 2638 points
Perch Support

What happens?

it is printed "nope not there" - apparently, the value of the variable does not get there ?

it just doesnt work out ... how would i transfer the value from the content Tag, which is in the HTML template, to the layout PHP ?

  1. in the HTML template, i ask the user for a value
<perch:content id="initials" type="text" label="Initialen"  />
  1. in the PHP template, there is just one line
<? lehrerPlan('xx'); ?>

i need to repleace xx with the value of "initials"

  1. to finish, i would call the layout PHP from the template HTML with <perch:layout>
Drew McLellan

Drew McLellan 2638 points
Perch Support

To use the layout, you'd have:

<perch:layout path="mylayout" />

which would load in the file perch/templates/layouts/mylayout.php

All your content variables will be passed to that layout as layout variables. So then you'd do:

<?php
    lehrerPlan(perch_layout_var('initials', true));
?>

Thank you Drew, for looking at this again,

but it still does not show up on the page, i am getting desperate

i did as you described, the path to the layout is correct, but the result is "nope"

that is the code from mylayout.php ~~~ <?php

lehrerPlan(perch_layout_var('initials', true));

if (perch_layout_has('initials')) { perch_layout_var('initials'); }else{ echo "Nope, not here!"; }
?>

You need to pass the variable to the layout

<perch:layout path="mylayout" initials="<perch:content id="initials" type="text" label="Initialen" />" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to pass the variable to the layout

You don't, it's automatic.

Thank you very much Robert,

now it works

thanks for your patience and your great support

best regards ! katja

Drew McLellan said:

You need to pass the variable to the layout

You don't, it's automatic.

Drew, I don't want to be pain, I use layouts a lot and there is also this documentation on the perch site: Passing Variables into Layouts https://docs.grabaperch.com/perch/pages-and-nav/layouts/variables/

Without passing the variable as an attribute on the layout tag: Including Layouts in Templates https://docs.grabaperch.com/templates/includes/layouts/ "Any attributes set on the perch:layout tag are passed through as layout variables." I've not been able to discover them automatically there.

I have never seen documentation to the effect of this happening automatically, so I use the above documented approach.