Get list of included template (.tpl) file names

Sudaraka Wijesinghe
Sudaraka Wijesinghe used Ask the Experts™
on
Hi all,

This is a question about Smarty Template Engine that runs on PHP, because I couldn't find a section for Smarty I post this under PHP.

Does any one know if there's a way to get a names for all the template files used to create the final HTML output?

Like for example let's say I have mypage.tpl and it includes top.tpl and bottom.tpl and in top.tpl it includes head.tpl and menu.tpl
So after display or fetch, I need to get the list containing top.tpl, head.tpl, menu.tpl and bottom.tpl

Any ideas?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
you can get this information while running Smarty under debug mode.
Sudaraka WijesingheWeb Application Programmer

Author

Commented:
I would like to get them in the production mode rather than the debug mode, is that possible?
Most Valuable Expert 2011
Top Expert 2016

Commented:
Have you tried this?
http://us.php.net/manual/en/function.get-included-files.php



<?php
$included_files = get_included_files();
var_dump($included_files);

Open in new window

Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Sudaraka WijesingheWeb Application Programmer

Author

Commented:
yes, I tried that. get_included_files return the list of included php file. What I need is the list of included template (.tpl) files while using the Smarty engine.

Thanks.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Show us how they are included with a code sample, please.
Sudaraka WijesingheWeb Application Programmer

Author

Commented:
I'm speaking in general, not addressing a particular code. But I have created sample set of file for you if it would help to understand what I'm talking about.

Please not that you need to rename all the .txt files in templates folder to .tpl (I added tham as .txt because EE doesn't allow me to upload .tpl files)

Thanks.
sample.zip
Most Valuable Expert 2011
Top Expert 2016
Commented:
Sorry, not much that is helpful there - I do not have the smarty scripts so I cannot give you the exact code, but I can still give you the strategy I would suggest.

Go into the smarty class and find the code that deals with things like this{include} statement and find the place where it processes the include statement.

<h1>Test Home Page</h1>
<hr />
{include file="menu.tpl"}

Take a position out of $GLOBALS - maybe $GLOBALS["smarty_includes"] - and make it an array of these included files.  

The kind of thing you would do in your changes to the smarty template would be something like this:

$GLOBALS["smarty_includes"][] = [DETECTED NAME OF TEMPLATE];

Then you could find the included smarty .tpl files with this statement:

var_dump($GLOBALS["smarty_includes"]);

Best regards, ~Ray
<?
include("./libs/Smarty.class.php");

$smarty=new Smarty();

$smarty->display("index.tpl");
?>

Open in new window

Sudaraka WijesingheWeb Application Programmer

Author

Commented:
Thanks Ray.
I was hoping Smarty would already have something like that built-in. I update Smarty libraries of my web projects separately so I wouldn't modify the Smarty compiler unless it's really necessary.

But I will consider you guidelines if I come to modify the Smarty.
Most Valuable Expert 2011
Top Expert 2016

Commented:
I understand.  It would seem that it only needs to be a one-line modification, and the results would be available in all PHP variable scopes and namespaces if you use the $GLOBALS array.  Best of luck with it, ~Ray
Web Application Programmer
Commented:
Finally found the solution, it was staring at my face all the time, imagine what good night of sleep can accomplish :)

In Smarty class there's a method called "_smarty_include", which is called for every include .tpl we use in out template (excluding the main template we display) and parameter passed to that method (is an array) has the .tpl name (full path/file name of the .tpl in $params["smarty_include_tpl_file"]). All I had to do is override this method in my wrapper class and do my stuff there.

Again, my thanks to Ray_Paseur and 04it040 for their participation.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial