A template that pulls in sub templates to create a page

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
blackrain
Forum Members
Forum Members
Posts: 98
Joined: Wed Feb 20, 2008 4:33 pm

A template that pulls in sub templates to create a page

Post by blackrain »

I have been looking for a way to create a page that iterates through sub pages and dynamically pull in these sub pages in order to create a page of sub pages, like a single page website. Each sub page holds a section of the parent page and can be reordered as per the CMSMS re-ordering function.

I remember seeing a post about it some time ago for CMSMS 1.x and there is a similar post here https://www.i-do-this.com/blog/one-page ... mplates/57 but, this post only deals with block elements and does not include the sub page template.

Any thoughts or ideas on how to achieve this, or even if you know where I can find the post concerning pulling sub pages into a parent page, including templates.

many thanks

Mark
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1606
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: A template that pulls in sub templates to create a page

Post by DIGI3 »

I've done something similar (I think) but I can imagine there would be a lot of different ways to go about it depending upon your requirements, site size, etc.

I used Navigator to make a list of the pages to load:

Code: Select all

{if isset($nodes)}
  {foreach $nodes as $node}
    <div id="{$node->alias}" class="lazy" data-loader="ajax" data-src="{$node->url}"></div>
  {/foreach}
{/if}
Then I used the jQuery lazy plugin to load each page. It's not exactly the same as what you're doing, but might give you some inspiration. You could also just do straight ajax and specify the </__body> or another element to grab.
Not getting the answer you need? CMSMS support options
blackrain
Forum Members
Forum Members
Posts: 98
Joined: Wed Feb 20, 2008 4:33 pm

Re: A template that pulls in sub templates to create a page

Post by blackrain »

I have tried this, the only issue I have is that I really wanted to pre compile the pages with php rather than ajax, this would help SEO and I can cache the pages also.

The i-do-this.com post offered a great solution but only rendered the block elements and not the template a a whole.

your thoughts are appreciated, thanks
User avatar
brambaud
New Member
New Member
Posts: 7
Joined: Mon Apr 18, 2011 8:38 am

Re: A template that pulls in sub templates to create a page

Post by brambaud »

Hi,

I did something similar to your need by loading subpages content from a main page.
The content of the main page will be the concatenation of sub-pages contents.
To do this, I took and customized the 'cache_remote_file' plugin written by Rolf Tjassens which allows you to call and cache the url content.
I also used in the main page template the SmartyEx extension to get the list of the main page sub-pages.

In the main page i do :

Code: Select all

{$children=smx::get_children()}
{if count($children)}
    {foreach $children as $child}
        {cache_content_file alias=$child.alias}
    {/foreach}
 {/if}
Here is my 'cache_content_file' plugin code (to be put in assets/plugins/function.cache_content_file.php') :

Code: Select all

<?php
#--------------------------------------------------------------------------------
# Plugin: cache_content_file based on cache_remote_file plugin by Rolf Tjassens
# Author: Benoit Rambaud (brambaud@gmail.com)
#--------------------------------------------------------------------------------
# Original Plugin: cache_content_file
# Author: Rolf Tjassens (cmscanbesimple.org)
#--------------------------------------------------------------------------------
# CMS Made Simple - Power for the professional, Simplicity for the end user.
# (c) 2004 - 2011 by Ted Kulp (wishy@cmsmadesimple.org)
# (c) 2011 - 2018 by The CMS Made Simple Development Team
# (c) 2018 - 2019 by The CMS Made Simple Foundation
# This project's homepage is: http://www.cmsmadesimple.org
# The module's homepage is: http://dev.cmsmadesimple.org/projects/cacheremotefile
#--------------------------------------------------------------------------------
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#--------------------------------------------------------------------------------

function smarty_nocache_function_cache_content_file($params, &$smarty) {

	/* Default parameter settings */
	$alias = isset($params['alias']) ? $params['alias'] : '';
  
  if ($alias == '') return;
  
  $source_url = "index.php?page={$alias}&bloc=1";
  
	$cache_directory = 'tmp/cache_content_file';
	$cache_file = $cache_directory . '/' . $alias . '.htm';

	/* Language strings */
	$lang_cantcreatedir = '<p style="color:red">ERROR - cache_content_file tag - Cannot create the required cache directory</p>';
	$lang_urlparammissing = '<p style="color:red">ERROR - cache_content_file tag - The required URL parameter is missing in your tag</p>';
	$lang_sourceoffline = '<p style="color:red">ERROR - cache_content_file tag - The source URL is off line or incorrect, can not create cache file</p>';

	/* Check if the cache directory is available. If not, create it */
	if (! is_dir($cache_directory) ) {
		@mkdir($cache_directory);
		
		if  ( isset($cache_directory) && !file_exists($cache_directory.'/index.html') ) {
			file_put_contents($cache_directory.'/index.html', '<!- Cache directory for cache_content_file tag -->');
		}
		
		if ( isset($cache_directory) && is_dir($cache_directory) && file_exists($cache_directory.'/index.html')) {
			/* Put mention into the Admin log */
			audit('','cache_content_file tag','Created cache directory: '. $cache_directory);
		} else {
			echo $lang_cantcreatedir;
			return false;
		}
	}
	
	/* Check for the URL parameter in the cache_content_file tag */	
	if ( isset($source_url) && $source_url == '' ) {
		echo $lang_urlparammissing;
		return false;
	}
	
	/* Depending on the presence of the cache file do some actions... */
	if ( !file_exists($cache_file) ) {
		/* Cache file is not present */
		
		$file_content  = @file_get_contents($source_url);
		
		if ($file_content === false) {
			echo $lang_sourceoffline;
			return false;
		} else { 
			file_put_contents( $cache_file, $file_content );
		}
	
	} else {
		/* Cache file is present */
		
		$time_left = ( (@filemtime($cache_file)) - (time() - 3600 * $time) );
		
		if ( $time_left < 0 ) {
		
			$file_content  = @file_get_contents($source_url);
			
			if ($file_content === false) { 
				touch($cache_file);
			} else { 
				file_put_contents( $cache_file, $file_content );
			}
		}
	}

return @file_get_contents($cache_file);

} // End Function


/**
 * Help text
 */
function smarty_cms_help_function_cache_content_file() {
	?>
	<h3>What does this do?</h3>
	<p>This plugin is an extended - but more reliable - version of the PHP file_get_contents function.<br />
	Change the content at one site and the others will follow when the buffer time has elapsed.</p>
	<br />
	<p>At first the tag will automatically create a new folder named (by default): /tmp/cache_content_file.<br />
	Because the tag caches the content for one hour, it won't waste any bandwidth. The tag has several reliability checks, so even when the source website is off line for a long period the content in the client website remains available.<br />
	The clear cache function of CMSMS will not delete these cached files! The files can only be removed manually in the CMSMS File Manager or using FTP!</p>
	<br />
	<p>Some examples where to use this tag:<br />
		<ul>
			<li>Copyright or disclaimer texts</li>
			<li>Analytics code</li>
			<li>Navigation</li>
			<li>News headlines</li>
			<li>etc.</li>
		</ul>
	</p>


	<h3>Dependencies</h3>
	<p>This plugin requires write permission in the CMSMS /tmp folder. If necessary you can change the directory path in the top of the code of the file function.cache_content_file.php at the default parameter settings. Change the value for the parameter <i>$cache_directory</i>.<br />
	In the server settings "allow_url_fopen" should be allowed.</p>


	<h3>How do I use it?</h3>
	<p>Create at the source website in the Design Manager module a Core::Page template (named i.e. blank) with <b>only</b> the <code>&#123;content&#125;</code> tag in it.<br />
	Put the text or code you want to share between websites in a hidden page (this means not in the menu) with the blank template attached.<br />
	Perhaps it is necessary to switch off the WYSIWYG editor in the page options tab.<br />
	Opening the page will only show the text or code with no styling.</p>
	<p>You can retrieve and cache this text or code from the source website just by adding in your page or template:<br />
	<code>&#123;cache_content_file url='//www.website.com/index.php?page=page_alias'&#125;</code> Don't use a <i>pretty url</i> in the url parameter!</p>
	<br />
	<p><b>Note:</b> All image or page paths in the content need to have the masters root url in it! Otherwise you will get an Error 404.</p>


	<h3>What parameters does it take?</h3>
	<p><b>url=''</b> (Required)<br />
	The url of the source file/page at the source website.</p>
	<p><b>time=''</b><br />
	Number of <b>hours</b> the remote content will be stored in a local cache file. When this period has elapsed the tag will try to renew the cache file with the content present in the source file. Default value is 1.</p>
	<br />

	<?php
} // End Function


/**
 * About text
 */
function smarty_cms_about_function_cache_content_file() {
	?>
	<p><b>Plugin author: Rolf Tjassens</b> &lt;info [at] cmscanbesimple [dot] org&gt;</p>

	<p><b>Version:</b> 1.2</p>
	<br />
	<p><b>Change History:</b></p>
	<ul>
		<li><b>Version 1.2</b> (17-10-2019)<br />
		- Change cache file extension.<br />
		- Change smarty_cms_ to smarty_nocache_ function.</li>

		<li><b>Version 1.1</b> (14-02-2016)<br />
	 	- Improved source file not available function.</li>

		<li><b>Version 1.0</b> (03-08-2012)<br />
		- Initial release.</li>
	</ul>
	<?php
} // End Function

#
# EOF
#
French CMSMS websites maker
Post Reply

Return to “Developers Discussion”