Forum

Thread tagged as: Question, Problem

Working with layout variables and front controllers

I work with a front controller, that does all the including for me in php. So my index.php file looks something like

<?php include('perch/runtime.php');?>
<!DOCTYPE html>
    <html class="no-js">
    <?php perch_layout('global.head'); ?>
    <body class="fill--color-bg" role="main">
        <?php
            // fancy include thingy
            perch_layout('global.footer');
        ?>
    </body>
</html>

The global.headhas everything it needs, including <title>Sitename - <?php perch_layout_var('title'); ?></title>. The include works fine.

My pages are organized in a folder structure. content/page/landing.tpl.php. I thought, in order to work with layout variables, I could write in my landing.tpl.php:

<?php
    PerchSystem::set_page('/content/kontakt/landing.tpl.php');

    perch_layout('global.head', array(
        'title'=>'Kontakt',
    ));

?>

But this does nothing. Am I not getting the concept of layout variables? Sorry if this is an obvious question, but I just can't find the solution here.

Edit: the same thing seems to be vaild for page attributes. So you can't use these things, if you work with a front controller?

Thomas Semmler

Thomas Semmler 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's how it works, but I don't understand where landing.tpl.php is used.

Drew McLellan said:

Yes, that's how it works, but I don't understand where landing.tpl.php is used.

The front controller takes the $_REQUEST['content'] and searches for a folder named 'CONTENT'. If there is a folder, it includes landing.tpl.php from the folder.

so for example, site/about_us will include from content/about_us/landing.tpl.php - thats basically how our front controller works.

So in our our landing.tpl.php we have our regions defined with perch_content();.

I'm not sure, if this answers your question.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so where is the above index.php file used?

in the root directory.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is it part of the same request?

yes. site/index.php?content=page

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, well I'll have to take your word for that as it makes no sense to me!

Yes, that's how it works.

Well, maybe I'm not really able to communicate my problem, sorry for that. But it is a huge problem after all, because we can't use layout variables and page attributes in any of our projects then.

Its hard to imagine, that we are the only ones, that work with this kind of front controller pattern, to be honest.

Other frameworks are also bootstrapping their pages, including the header first, including content files depending on the Url, and including the footer then. This is a very common pattern and its hard for me to accept, that page attributes and layout variables cannot be integrated in this case.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Right - that's also how our own product Perch Runway works. You're clearly experiencing a problem, but not a design limitation. If you can give me a clear example I can try to help.

Alright. I'll try to give you an example.

All requests on the site go to the index.php, which looked like this:

<?php

    // Constants, Functions & Settings
    include('inc/config.inc.php');
    include('inc/global.inc.php');

    // Perch
    include('perch/runtime.php');

?>
<!DOCTYPE html>
    <html>
    <?php

        // html head.
        include('tpl/head.tpl.php');

    ?>
    <body>
    <?php   

        // Navigation
        include('tpl/navigation.tpl.php');

        //  ---- 
        //  Bootstrap-code
        //  Here runs code, that does all the including for the site, based on $_REQUEST['content']
        //  ----

        // Footer
        include('tpl/footer.tpl.php');

    ?>
    </body>

The Bootstrap-code includes then a certain page based on the url. https://www.site.com?content=contact the code looks for a folder named contact and includes the landing.tpl.php within it. For example, the contact page:

<?php
    PerchSystem::set_page('/content/contact/landing.tpl.php');
?>
<section class="content--base section--page-content">
    <?php perch_content('Impressum'); ?>
</section>

The code I have now in my index.php, looks like this:

<?php

    // Constants, Functions & Settings
    include('inc/config.inc.php');
    include('inc/global.inc.php');

    // Perch
    include('perch/runtime.php');

?>
<!DOCTYPE html>
    <html>
    <?php

        // html head.
        //include('tpl/head.tpl.php');
        perch_layout('global.head');

    ?>
    <body>
    <?php   

        // Navigation
        include('tpl/navigation.tpl.php');

        //  ---- 
        //  Bootstrap-code
        //  Here runs code, that does all the including for the site, based on $_REQUEST['content']
        //  ----

        // Footer
        include('tpl/footer.tpl.php');

    ?>
    </body>

Instead of the php includes, I use perch layouts for my head. So, what I want to do now, is set my layout_variable in the bootstrapped page. Which can't work of course, because the head gets included before the page itself. So this doesn't work:

<?php
    PerchSystem::set_page('/content/contact/landing.tpl.php');
    perch_layout('global.head', array(
        'title'=>'This is my contact page'
    ));
?>
<section class="content--base section--page-content">
    <?php perch_content('Impressum'); ?>
</section>

I hope, this describes my problem better now. The same thing goes for page attributes, they also cannot work, because the global.head already gets included way before the landing.tpl.php does.

My question is now, how can I make usage of layout variables and page attributes in this pattern?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great. So it's just an execution order problem? Why not move your bootstrap code up to the top of the process, figure out what needs doing, set the page for Perch and then just include the content in order?

Alright, as our bootstrap-object is very complex, we'd have to expand its functionality to parse the files and look for the setPage, attributes and layout code to execute before the header gets included.

It really bugs me, that our boilerplate doesn't already have the capacity for this. But the boilerplate was never designed to be integrated with a cms.

Thank you very much for your patience and support on this subject.