Soma Posted December 10, 2013 Share Posted December 10, 2013 I'm using the _init.php as per the config.php. I'm not sure I understand why some things just don't seem to work in there. 1. I have a code to check for access on parents, though I can't get it to work in the _init.php $access_page = $page->parents("access_select_roles.count>0")->first; if($access_page->id){ // #29 if($access_page->access_select_roles->has("$user->roles")){ echo "access granted"; } else { $session->redirect("/login/"); } } It always throws notice: Notice: Trying to get property of non-object in /site/templates/_init.php on line 29 And I can't get it to work, as it looks like the page is found but can't check for properties. But when I add this code to a template file it works fine. Could this be due to some multiple calls of the _init.php, maybe when using $page->render() in some templates? 2. I'm trying to use the $config->scripts, $config->styles arrays and when I set them to be empty in the _init.php When I do $config->scripts->removeAll(); And add script from within template files, $config->scripts->add($config->urls->templates . "js/somescript.js") ... the filename array is just empty when I try to output them at the end in the output with <?php foreach($config->scripts->unique() as $file) echo "\n\t<script src='$file'></script>"; ?> Link to comment Share on other sites More sharing options...
ryan Posted December 24, 2013 Share Posted December 24, 2013 Could this be due to some multiple calls of the _init.php, maybe when using $page->render() in some templates? I think this is most likely the case, assuming you do have multiple render() calls. Your _init.php may not be the right place to do some of this stuff, or if it is, then you may need to add additional check so that you don't have the same things being run twice. For instance, you have a $config->scripts->removeAll(). If you add some scripts to $config->scripts, and later have another $page->render() call, then the files you previously added to it would again be removed by your $config->scripts->removeAll(). There are a couple ways you could solve this. First would be to just move your code that shouldn't be run twice to a separate include file, and then use PHP's include_once() function on that file. For instance, your _init.php could have this: include_once("./_init_once.php"); The above is the safest bet, because if your _init.php defines any functions or classes, then you don't have to worry about them being defined twice (and resulting in a fatal error). But if you want to keep everything in your _init.php, you could do this: if(!defined("LOADED")) { define("LOADED", true); // your code here // ... } Lastly, I wanted to mention that your files will have access to an $options variable, which has a 'pageStack' property containing a stack (array) of pages that initiated the current render. It will be empty the current render() is not recursive. So you could accomplish the same thing as above like this: if(empty($options['pageStack'])) { // your code here // ... } One more thing I just remembered is that you could also tell your render() call to skip the prependTemplateFile: echo $somePage->render(array('prependFile' => '')); 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now