Results 1 to 14 of 14

Thread: Subfolder Support in the Templates Folder!

  1. #1
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Subfolder Support in the Templates Folder!

    Woah! When did the core templates module start supporting subfolders? I just discovered it by accident.

    Just wanted to say thank you -- you just made me very happy!

    Brian

    EDIT: Nevermind -- they show up in the module, but don't actually work unless they are in the root. I got really excited there for a second.
    Brian

  2. #2
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Subfolder Support in the Templates Folder!

    Booyah!!!

    http://dev.typolight.org/issues/show/1348

    The code is there, if anyone else wants to try it out -- it's only tested in 2.8RC2.
    Brian

  3. #3
    User
    Join Date
    06-29-09.
    Posts
    271

    Default Re: Subfolder Support in the Templates Folder!

    I do understand the need for logic structures, but when does this become so very useful? Perhaps I have never run into the problem you are having, usually the default templates work perfectly and worst case up until now I used 12 custom templates for one website. Which given logical names is not really messy.

    Could you explain to me when this feature becomes a necessity? ops:

  4. #4
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Subfolder Support in the Templates Folder!

    Hi Vera,

    Some scenarios --

    1. A large site with many custom templates (including multiple catalogs, news formats, etc.), or a multi-domain setup where sub-sites need different layouts. Obviously if you have no more than 10, it is less useful, but once you get to about 30+ it can become a bit cumbersome.

    2. In a team environment where multiple people need to touch the template files, it may be helpful to have them organized into logical folders for clarity -- as a solo developer you have a pretty good idea what templates do what, but for other team members in such a scenario it may be less clear.

    3. If you have your own set of custom templates with a preferred HTML format that you want to reuse across multiple projects, again it can be helpful since in this scenario you would probably have a lot of them, and you can organize them into subfolders based on functionality. So if a site I'm deploying does not use events, I can leave out the "events" folder instead of sifting through a flattened list of templates.

    The request was rejected anyway, and once I got Leo to explain it made perfect sense (I wasn't that surprised considering I have about a month of PHP under my belt and figured there was much more to this that I didn't know).
    Brian

  5. #5
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Subfolder Support in the Templates Folder!

    So anyway, just in case anyone else is interested in this feature, here is what I did. This is -- I think -- a much better version than the one I put in the feature request because it parses the templates folder once upon page load and stores as $GLOBALS['TL_CONFIG']['customTemplates'], then every time after that references this array instead.

    First I added this function:

    Code:
    function getCustomTemplates($strBaseFolder)
    {
    	$strBaseFolder = rtrim($strBaseFolder, '/');
    	$arrTemplates  = array();
    	$arrFilters    = array();
    		$arrFilters[] = '.';
    		$arrFilters[] = '..';
    		$arrFilters[] = '.htaccess';
    		
    	foreach (array_diff(scandir($strBaseFolder), $arrFilters) as $strFileName)
    	{
    		if (!is_bool(strripos($strFileName, '.tpl')))
    		{
    			$arrTemplates[basename($strFileName, '.tpl')] = $strBaseFolder;
    		}
    		
    		if (is_dir($strBaseFolder . '/' . $strFileName))
    		{
    			$arrTemplates = array_merge($arrTemplates, getCustomTemplates($strBaseFolder . '/' . $strFileName));
    		}
    	}
    
    	return $arrTemplates;
    }
    This into localconfig.php:
    Code:
    $GLOBALS['TL_CONFIG']['customTemplates'] = getCustomTemplates(TL_ROOT . '/templates/');
    And finally made the following two changes to the functions in Controller.php:
    Code:
    protected function getTemplate($strTemplate)
    {
    	$strTemplate = basename($strTemplate);
    	$arrCustomTemplates = $GLOBALS['TL_CONFIG']['customTemplates'];
    	
    	foreach($arrCustomTemplates as $strCustomTemplate => $strCustomFolder)
    	{
    		if ($strCustomTemplate == $strTemplate)
    		{
    			$strFile = sprintf('%s/%s.tpl', $strCustomFolder, $strCustomTemplate);
    		}			
    	}
    
    	if (!isset($strFile))
    	{
    		foreach ($this->Config->getActiveModules() as $strModule)
    		{
    			$strPath = sprintf('%s/system/modules/%s/templates/%s.tpl', TL_ROOT, $strModule, $strTemplate);
    
    			if (file_exists($strPath))
    			{
    				$strFile = $strPath;
    				break;
    			}
    		}
    	}
    
    	if (!file_exists($strFile))
    	{
    		throw new Exception(sprintf('Could not find template file "%s"', $strTemplate));
    	}
    
    	return $strFile;
    }
    Code:
    protected function getTemplateGroup($strPrefix)
    {
    	$arrTemplates = array();
    	$arrFolders =  array();
    	$arrCustomTemplates = $GLOBALS['TL_CONFIG']['customTemplates'];		
    
    	foreach ($this->Config->getActiveModules() as $strModule)
    	{
    		$strFolder = sprintf('%s/system/modules/%s/templates', TL_ROOT, $strModule);
    
    		if (is_dir($strFolder))
    		{
    			$arrFolders[] = $strFolder;
    		}
    	}
    
    	foreach ($arrFolders as $strFolder)
    	{
    		$arrFiles = preg_grep('/^' . preg_quote($strPrefix, '/') . '.*\.tpl$/i',  scan($strFolder));
    
    		foreach ($arrFiles as $strTemplate)
    		{
    			$arrTemplates[] = basename($strTemplate, '.tpl');
    		}
    	}
    
    	foreach ($arrCustomTemplates as $strCustomTemplate => $strCustomFolder)
    	{
    		if(!is_bool(strripos($strCustomTemplate, $strPrefix)))
    		{ 
    			$arrTemplates[] = $strCustomTemplate;
    		}
    	}
    	
    	natcasesort($arrTemplates);
    	return array_values(array_unique($arrTemplates));
    }
    I have no idea how to make this update safe, or if such a thing is even possible.

    Brian
    Brian

  6. #6
    User
    Join Date
    05-02-10.
    Posts
    121

    Default Re: Subfolder Support in the Templates Folder!

    Quote Originally Posted by Medianomaly
    First I added this function:

    Code:
    function getCustomTemplates($strBaseFolder)
    {
    	$strBaseFolder = rtrim($strBaseFolder, '/');
    	$arrTemplates  = array();
    	$arrFilters    = array();
    		$arrFilters[] = '.';
    		$arrFilters[] = '..';
    		$arrFilters[] = '.htaccess';
    		
    	foreach (array_diff(scandir($strBaseFolder), $arrFilters) as $strFileName)
    	{
    		if (!is_bool(strripos($strFileName, '.tpl')))
    		{
    			$arrTemplates[basename($strFileName, '.tpl')] = $strBaseFolder;
    		}
    		
    		if (is_dir($strBaseFolder . '/' . $strFileName))
    		{
    			$arrTemplates = array_merge($arrTemplates, getCustomTemplates($strBaseFolder . '/' . $strFileName));
    		}
    	}
    
    	return $arrTemplates;
    }
    where exactly?
    sorry for my english

  7. #7
    User
    Join Date
    05-02-10.
    Posts
    121

    Default Re: Subfolder Support in the Templates Folder!

    cool!

    initconfig.php
    sorry for my english

  8. #8
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Subfolder Support in the Templates Folder!

    Just keep in mind that is very old. It probably doesn't work in new versions (at least without breaking theme support).

    It's also ill-advised because it requires hacking the core.

    And it's also one of the first things I ever wrote in PHP. So it's probably terrible.

    It would still be nice to have, but I've since given up and gotten used to stuffing everything into one template folder. It's really not that bad, actually.
    Brian

  9. #9
    User
    Join Date
    05-02-10.
    Posts
    121

    Default Re: Subfolder Support in the Templates Folder!

    Hi, Medianomaly.
    I use 2.9.5, with my fixes.

    your code working

    but
    I don't understand why not supported subfolders
    still
    2.11 too
    sorry for my english

  10. #10
    Community-Moderator xchs's Avatar
    Join Date
    06-19-09.
    Posts
    1,287

    Default Re: Subfolder Support in the Templates Folder!

    Which kind of templates you want to put in a template subdirectory?
    Contao Community Moderator
    → Support options

  11. #11
    User
    Join Date
    05-02-10.
    Posts
    121

    Default Re: Subfolder Support in the Templates Folder!

    Quote Originally Posted by xchs
    Which kind of templates you want to put in a template subdirectory?
    for
    dma_elementgenerator

    it BE module

    subfolders not work for BE modules say Leo :shock:
    why?

    i have 12 sites and 12 subfolders
    sorry for my english

  12. #12
    Community-Moderator xchs's Avatar
    Join Date
    06-19-09.
    Posts
    1,287

    Default Re: Subfolder Support in the Templates Folder!

    Quote Originally Posted by Energetik
    subfolders not work for BE modules
    Yes, that's true, since back end templates are normally not theme-relevant.
    Contao Community Moderator
    → Support options

  13. #13
    User
    Join Date
    05-02-10.
    Posts
    121

    Default Re: Subfolder Support in the Templates Folder!

    Yes, that's true, since back end templates are normally not theme-relevant.
    mm.. but if i have 30-50 back end templates?
    this very uncomfortable

    how fix this?)) for 2.9


    Medianomaly code is old... though the working
    sorry for my english

  14. #14
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Subfolder Support in the Templates Folder!

    Quote Originally Posted by Energetik
    I don't understand why not supported subfolders still 2.11 too
    I think it has to do with performance. It's less intensive to search a single folder than to crawl an entire folder tree. Especially when you need to do it on every page load (although a caching system can help).

    The way Contao looks for templates is it scans you theme/template folders first, then scans all the module template folders, always searching the "backend" and "frontend" template folders last. The first template it finds wins -- it stops searching at that point.

    I think recently (somewhere around 2.10 or 2.11) it searches in reverse order. So precedence looks something like this:
    1. Your theme template folders
    2. The default templates folder
    3. /system/modules/*/templates/ folders (in reverse alphabetical order).
    4. /system/modules/frontend/templates/ folder
    5. /system/modules/backend/templates/ folder

    So you can use this to your advantage by making a folder like /system/modules/{templates}/templates/ and putting custom templates in there too (the { character is alphabetized after z on Linux, and maybe Windows too, so it will get searched before the other modules). If you want to put custom BE templates there, that might not be a bad place.
    Brian

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •