Forum
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.head
has 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?
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 fromcontent/about_us/landing.tpl.php
- thats basically how our front controller works.So in our our
landing.tpl.php
we have our regions defined withperch_content();
.I'm not sure, if this answers your question.
Ok, so where is the above
index.php
file used?in the root directory.
Is it part of the same request?
yes.
site/index.php?content=page
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.
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:The Bootstrap-code includes then a certain page based on the url.
https://www.site.com?content=contact
the code looks for a folder namedcontact
and includes thelanding.tpl.php
within it. For example, the contact page:The code I have now in my index.php, looks like this:
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:
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 thelanding.tpl.php
does.My question is now, how can I make usage of layout variables and page attributes in this pattern?
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.