Peter Falkenberg Brown Posted May 11, 2013 Share Posted May 11, 2013 Hi All, I've set up a website with three or four main templates: - one for the home page - one for "list" pages (i.e. section type pages) - one for article type pages - and some other ones. All the templates point to a custom template file that acts as a controller, and checks a custom template file field for the actual template file to include from the controller template, defined when the various pages are created. For example a "multi-section" page would use the list type template structure, but would have a field that defined the final output template file as "multi-section.html". A sub-section page would use the "section.html" template. (the multi-section pages are not paginated, and the section pages are paginated). An article page might use a regular "article.html" template or an "article_plain.html" template, to create a different type of page, depending on whether it was a simple contact us page (for the plain one), or a page that pulled in author information, etc. The Question: In the admin section, I can define each basic template field set to be cachable, but I'm wondering if that automatically caches the include files loaded by the controller template file. Here's the content of my controller file that each of the template field sets use: <?php#################################################################################################### custom_template_controller.php###################################################################################################include("./inc_functions.php");block_future_publish_date();$custom_template_file_id = $page->custom_template_file;$custom_template_file = $pages->get("$custom_template_file_id")->select_value;include("./$custom_template_file");################################################################################################### So far, everything is working fine. I just need to confirm if the final output, assembled by the $custom_template_file, will be cached, assuming that the template field set has caching turned on. My second question relates to the MarkupCache module. Based on the concept that the custom_template file has PHP statements and include function, etc, etc, will all of that output be cached by the template setting, or do I have to wrap the whole thing in a MarkupCache statement? Thanks for any clarifications... Peter Link to comment Share on other sites More sharing options...
teppo Posted May 11, 2013 Share Posted May 11, 2013 (edited) Umm.. sorry, but you almost lost me with "I can define each basic template field set to be cachable". You are talking about template cache, right -- not Fieldtype Cache? If it's template level cache you're talking about, that will cache the final, rendered output of each page, naturally including any and all include files. Take a look at PageRenderer for more details. You should also take a look at Page Render at modules page after turning cache on and loading a few pages. It'll tell you just how many files are cached. Another thing you might want to do (especially during testing) is to take a look at the contents of /site/assets/cache/Page/ to see which templates are getting cached and what is being cached for them. That should provide a definitive answer to your question MarkupCache is useful if you have specific, smaller parts of pages that need to be cached, individually (and probably instead of) whole templates. I've used this, for an example, in the case of relatively resource-consuming queries to external (or internal..) API's that also need to parse fetched data to display it etc. Edit: just wanted to add that I've been working on a personal site that does similar things you've described above, such as using a front controller that all requests are routed through and various methods of selecting which view script (sorry, Zend Framework tends to stick) should be used to render that particular page / part of page. One problem I had was that once a page had first been rendered elsewhere (a list view) with very stripped view script and then rendered on it's proper URL as (supposedly) a full version, stripped version got pulled from cache and things looked very wrong. Long story short, $session->PageRenderNoCachePage can be quite useful at times -- it allows you to skip cache for pages rendered on the fly. Then again, this is mostly an issue if you're a) using page properties to determine which file should be rendered (URL segments and GET/POST variables can already be set to override cache via template settings) and b) rendering pages inside other pages with $page->render(). Might not be an issue for you at all, just saying.. Edited May 11, 2013 by teppo Link to comment Share on other sites More sharing options...
Wanze Posted May 11, 2013 Share Posted May 11, 2013 Hi peter. Like teppo said: Doesn't matter what you do in your templates, the output that is returned by $page->render() is cached if you enable caching for the templates. Just wanted to say that there's nothing beeing cached if you're logged in. Make sure you use another browser or log out to test if the caching works. Link to comment Share on other sites More sharing options...
Soma Posted May 11, 2013 Share Posted May 11, 2013 Template cache, caches the page as it's being sent to the browser, same as if you would save the page in the browser to static html, not matter what and how many includes you have to render the page. Link to comment Share on other sites More sharing options...
Peter Falkenberg Brown Posted May 11, 2013 Author Share Posted May 11, 2013 Dear Teppo, Wanze and Soma, Thanks for all your responses. I appreciate it. Teppo, yes, I was talking about templates. I still find it a bit prone to error when I talk about an internal template (which to me looks like a definition of a set of fields), versus a template file that gets parsed from the disk. It's a fine line unless one assumes that the critical word is "file" (and doesn't forget to mention that critical word). About the $page->render() comments above: Most of my template files use a find command (except for article pages), and then they grab the data through the $page object. I only use the $page->render() function in my code on the home page, for blocks of lists, in this function: function render_block($inc_block_template_file, $page_ids_array){if( count($page_ids_array) and !empty($inc_block_template_file) ) { foreach ( $page_ids_array as $page_id ) { $page_id->template->filename = wire(config)->paths->templates . "$inc_block_template_file"; echo $page_id->render(); } }else { echo "No Content Found"; }} Is $page->render() a necessary call for things to be cached, or does it also happen when one just uses the echo $page->field method? Also, Teppo, I haven't run into the conflict you describe above. I use different templates for list pages and article pages. I wonder if that might be an issue. Thanks! Peter Link to comment Share on other sites More sharing options...
teppo Posted May 12, 2013 Share Posted May 12, 2013 About the $page->render() comments above: Most of my template files use a find command (except for article pages), and then they grab the data through the $page object. I only use the $page->render() function in my code on the home page, for blocks of lists, in this function: function render_block($inc_block_template_file, $page_ids_array) { if( count($page_ids_array) and !empty($inc_block_template_file) ) { foreach ( $page_ids_array as $page_id ) { $page_id->template->filename = wire(config)->paths->templates . "$inc_block_template_file"; echo $page_id->render(); } } else { echo "No Content Found"; } } Is $page->render() a necessary call for things to be cached, or does it also happen when one just uses the echo $page->field method? The fact that any of your template files (controller or something else) get rendered means that ProcessWire has already triggered $page->render() behind the scenes. This is done by Process Page View. So, essentially, the code you've posted above is perfectly valid.. it just means that $page->render() get's called once for your controller and then once for each $page_id->render(). You can quite easily debug this by adding an echo as the first thing in /wire/modules/PageRender.module. So the answer is two-fold: yes, it is necessary for things to get cached -- but no, you don't necessarily have to call it yourself Also, Teppo, I haven't run into the conflict you describe above. I use different templates for list pages and article pages. I wonder if that might be an issue. Not really sure about this one. If a page is rendered and it has caching on, it should get cached exactly the way it's rendered there. I may be confusing something here, but you should definitely make sure that you're not just viewing things while logged in -- in case that you have edit access and are logged in, cache gets automatically overridden and things may look a bit different. Like @Wanze pointed out above, you'll probably want to log out every now and then to make sure that everything is fine or use another browser at the same time while testing stuff related to cache. Link to comment Share on other sites More sharing options...
Peter Falkenberg Brown Posted May 12, 2013 Author Share Posted May 12, 2013 Dear Teppo, Thanks for your excellent feedback. And yes, as soon as I turn on caching in the templates, I'll start testing with a non-logged in browser. I already ran into an issue with that, when I had some permissions incorrectly set. I could view some things as the admin that normal users couldn't. Thanks to you, and everyone, for such excellent help. Hey, maybe one day, I'll be able to help someone with ProcessWire, too! Best regards, Peter 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