Help with Template Mess
Moderator: General Moderators
Help with Template Mess
On the server of the site I'm currently working on I see a bunch of .tpl files, which inside look like regular html files except they also have tags like "{$navi}", "{$menu}" etc.
The problem is I need to add some php code in a couple of those file, but they just show up as plain text. I've heard of template engines like smarty, and gathered online that you can include php code by using curly brackets. Well, I tried that syntax but it didn't work.
I guess the first question is, how can I learn what template engine is being in use here? (Original developer is long gone). I don't see any template-engine name anywhere. So far, all I see on the server are the.tpl files, .php files, img files and css files. I have a deadline to get this done by. I just need to know what php template engine is being used so I can check its documentation, and figure out how to add php statements to these tpl files.
Thanks in advance
The problem is I need to add some php code in a couple of those file, but they just show up as plain text. I've heard of template engines like smarty, and gathered online that you can include php code by using curly brackets. Well, I tried that syntax but it didn't work.
I guess the first question is, how can I learn what template engine is being in use here? (Original developer is long gone). I don't see any template-engine name anywhere. So far, all I see on the server are the.tpl files, .php files, img files and css files. I have a deadline to get this done by. I just need to know what php template engine is being used so I can check its documentation, and figure out how to add php statements to these tpl files.
Thanks in advance
-
mikeashfield
- Forum Contributor
- Posts: 159
- Joined: Sat Oct 22, 2011 10:50 am
Re: Help with Template Mess
If the folder structure is something a little like this: http://www.maheshchari.com/wp-content/u ... ucture.jpg with a models, views, and controllers folder (amongst others) then it's probably CodeIgniter.
Re: Help with Template Mess
Thanks for trying to help, unfortunately I couldn't access the file.
I've scanned through pretty much the whole website directory, but can't find a hint of what template engine is used, other than.tpl files which many on the web say is common for the smarty engine. But I tried the smarty php syntax ({php}code{/php} as ell as as something similar. And also the phpBB <!---code----> I think it was. I don't know what to do!~ There are like 30 different template engines, I can't check the syntax for all of them just to be able to include a snippet of php code.
Perhaps they all use .tpl files, I dunno. The website directory contains the following top folders, if it helps, in this order: css, db_log, img, includes, modules, templates.
It is the templates folder that contains the.tpl files, and the modules folder contains.php files. But shouldn't there be a folder for the template engine somewhere?? Otherwise, how can it be used, unless it's there. Where would it be , normally.
I've scanned through pretty much the whole website directory, but can't find a hint of what template engine is used, other than.tpl files which many on the web say is common for the smarty engine. But I tried the smarty php syntax ({php}code{/php} as ell as as something similar. And also the phpBB <!---code----> I think it was. I don't know what to do!~ There are like 30 different template engines, I can't check the syntax for all of them just to be able to include a snippet of php code.
Perhaps they all use .tpl files, I dunno. The website directory contains the following top folders, if it helps, in this order: css, db_log, img, includes, modules, templates.
It is the templates folder that contains the.tpl files, and the modules folder contains.php files. But shouldn't there be a folder for the template engine somewhere?? Otherwise, how can it be used, unless it's there. Where would it be , normally.
Re: Help with Template Mess
Maybe posting some sample code will help someone recognize it? We currently have very little to go on.
Re: Help with Template Mess
To determine if it is Smarty, drop the following into one of the .tpl files:
{debug}
This will popup the debug window that shows what all data the template is working with....
If it is smarty, well there could be a reason your adding code didn't work, caching could be turned on and programmed in such a way, that it didn't detect the template changed, so it was using a cached copy of it, not what you changed. (What a headache until you realize this and how to clear the cached data when you first start working with Smarty)
As mentioned, post some code for what gets called when you visit the site (ie. root index.php)
-Greg
{debug}
This will popup the debug window that shows what all data the template is working with....
If it is smarty, well there could be a reason your adding code didn't work, caching could be turned on and programmed in such a way, that it didn't detect the template changed, so it was using a cached copy of it, not what you changed. (What a headache until you realize this and how to clear the cached data when you first start working with Smarty)
As mentioned, post some code for what gets called when you visit the site (ie. root index.php)
-Greg
Re: Help with Template Mess
Ok, here are some samples, if it helps;
This is a sample from the modules folder, which are all php files, one for each webpage. They basically contain a bunch of preg_replace()es, classes and custom functions like to get webpage menu code etc. This is a sample of one from the modules folder.
Ok, I notice here that it calls the .tpl file for the index page above and then replaces some text in it. But still don't know what template engine is used.
Here's a sample from the ix.tpl called in the above code, found the templates folder.
Like I said, I've tried smarty php tags, but that didn't work. The {php} php code {/php}. So now there are only some 30 more template engines to check out, to h with that. I should mention that the root directory on the server also contains folders for other websites, but I haven't seen any template engine name so far by looking through files in some of them.
I just saw your comment twinedev. I'm going to try the {debug} and see.
This is a sample from the modules folder, which are all php files, one for each webpage. They basically contain a bunch of preg_replace()es, classes and custom functions like to get webpage menu code etc. This is a sample of one from the modules folder.
Code: Select all
/**
* @desc : this returns index (index.php) page which is modified by an array of XML data
* @acce : public
* @param : none
* @return : string of html file (index.php)
*/
function getHtml()
{
//-- gets template
$tpl = file_get_contents("./templates/ix.tpl");
//-- replace specific tags for specific contents
$tpl = preg_replace("/\{\\\$lang_def\}/", STR_LANGUAGE_DEFINITION, $tpl);
$tpl = preg_replace("/\{\\\$meta_for_lng\}/", STR_META_FOR_LNG, $tpl);
$tpl = preg_replace("/\{\\\$meta_for_description\}/", STR_META_FOR_DESCRIPTION, $tpl);
$tpl = preg_replace("/\{\\\$meta_for_css_def\}/", STR_META_FOR_CSS_DEFINITION, $tpl);
$tpl = preg_replace("/\{\\\$meta_for_script_def\}/", STR_META_FOR_SCRIPT_DEFINITION, $tpl);
$tpl = preg_replace("/\{\\\$title_in_body\}/", STR_TITLE_FO_INDEX_IN_BODY, $tpl);
Ok, I notice here that it calls the .tpl file for the index page above and then replaces some text in it. But still don't know what template engine is used.
Here's a sample from the ix.tpl called in the above code, found the templates folder.
Code: Select all
<meta name="verify-v1" content="zuKp24Q/3E9UeGn85ZLo+xnpu46QC0G9aACg4dNFPiA=" />
<title>{$title}</title>
{$meta_for_description}
{$meta_for_css_def}
{$meta_for_script_def}
</head>
<body id ="home">
<div id="wrapper">
<div id="container">
<div id="intro">
<div id="pageHeader">
<h1><span>{$title_in_body}</span></h1>
</div>
<div class="menu">{$menu} </d5iv>
</div>
<div id="menu2">
<p class="nav"><span>{$navi}</span></p>
<p class="desc"><span>{$desc}</span>
</p>
</div>Like I said, I've tried smarty php tags, but that didn't work. The {php} php code {/php}. So now there are only some 30 more template engines to check out, to h with that. I should mention that the root directory on the server also contains folders for other websites, but I haven't seen any template engine name so far by looking through files in some of them.
I just saw your comment twinedev. I'm going to try the {debug} and see.
Re: Help with Template Mess
No debugging, the page just displays the {debug} tag as text. That at least further confirms that maybe it's not smarty.
You two are heroes for taking your time where no one else has. Out of thousands of users, no-one has any better clue than I do? Or, is this mostly the kind of 'meh' forum?
You two are heroes for taking your time where no one else has. Out of thousands of users, no-one has any better clue than I do? Or, is this mostly the kind of 'meh' forum?
Re: Help with Template Mess
It looks like there might not be any "template engine" per se - the original developer just used {} characters for his/her string replacement. If this is the case, then injecting PHP code into the template file itself will be difficult/impossible because that getHtml() function reads it in as text, not as PHP code.
Since it's unlikely you'll be able to port this to a proper template engine in time, just follow what the standard for this project. Insert a tag into the template, then write a function in the appropriate module that replaces that tag with the result of some PHP code you write. ob_start() and ob_get_clean() might be useful here.
Edit: And this generally isn't a "meh" forum - your original post didn't have any code & was relatively non-specific, so I'm guessing most people glanced at it & left.
Since it's unlikely you'll be able to port this to a proper template engine in time, just follow what the standard for this project. Insert a tag into the template, then write a function in the appropriate module that replaces that tag with the result of some PHP code you write. ob_start() and ob_get_clean() might be useful here.
Edit: And this generally isn't a "meh" forum - your original post didn't have any code & was relatively non-specific, so I'm guessing most people glanced at it & left.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: Help with Template Mess
Thanks for that. Actually all I want is to include a file with one call to the include() function.
Do you happen to know what subtopic of php deals with communicating between a function in one file and a bracket statement - {$statement} - in another?
Do you happen to know what subtopic of php deals with communicating between a function in one file and a bracket statement - {$statement} - in another?
Re: Help with Template Mess
"template engine tutorial" might work for search terms. Most tutorials are about how to use existing engines though - not how to build one yourself.
Really though, there isn't any "communication", you're just doing string replacement.
Really though, there isn't any "communication", you're just doing string replacement.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: Help with Template Mess
Well, it just struck me that if it's not a template file, shouldn't its extension be .php and not.tpl? Or does the php language allow.tpl extensions natively?
Also hopefully I'm getting closer. I tried creating a separate function in the same .php file as the get_html() function is in. Tried assigning the include() content to a variable after reading how to online. There are no errors, but, how I get the contents to display in the html .tpl file is another story. I don't know why the original developer could write functions and then simply display their return variable contents within { } brackets in the.tpl, file and have their contents display, but when I try to do it with my function, only the name of the variable in text format shows up. Wonder what the magic is that he used.
My function returns the variable with the return statement just like his does.
Also hopefully I'm getting closer. I tried creating a separate function in the same .php file as the get_html() function is in. Tried assigning the include() content to a variable after reading how to online. There are no errors, but, how I get the contents to display in the html .tpl file is another story. I don't know why the original developer could write functions and then simply display their return variable contents within { } brackets in the.tpl, file and have their contents display, but when I try to do it with my function, only the name of the variable in text format shows up. Wonder what the magic is that he used.
My function returns the variable with the return statement just like his does.
Re: Help with Template Mess
From the code you posed, the .tpl is just being read into the main script that runs. When this is done, it makes no difference what the extension is (also the same for when a file is included/required). The extension only matters for the actual script being called from the server, which then needs to be something that the server is configured to handle as php (.php .php5 .phtml are the most common)
The code you gave (at least the little bit you gave), looks for the {$variable} style code in the template, and replaces it with the appropriate value. So unless you put code in the specific way it replaces things, you will just get what you put in the .tpl.
We can't advise here on how to properly do it without the code for at least the function you gave the beginning part of.
-Greg
The code you gave (at least the little bit you gave), looks for the {$variable} style code in the template, and replaces it with the appropriate value. So unless you put code in the specific way it replaces things, you will just get what you put in the .tpl.
We can't advise here on how to properly do it without the code for at least the function you gave the beginning part of.
-Greg
Re: Help with Template Mess
Thanks Greg and the others. I'm thinking of posting the whole code page, it's just that I'm a bit concerned about posting someone else's work publicly. Maybe I can modify it a bit first.
The more I look at the code the more I see a pattern, but still, not sure exactly what he was replacing or what-for. I need to include a file via php include(), but it doesn't need to have any of its contents replaced, so I'm trying to figure out how it would fit in with the rest of this code.
I'll see what I do, thanks.
The more I look at the code the more I see a pattern, but still, not sure exactly what he was replacing or what-for. I need to include a file via php include(), but it doesn't need to have any of its contents replaced, so I'm trying to figure out how it would fit in with the rest of this code.
I'll see what I do, thanks.
Re: Help with Template Mess
Ok, I've decided to post the php file modified (tried not to introduce code errors in the process, but bear in mind potential duplicates or extra brackets, it'd be from my editing). Also, the reason I didn't post all the code at first is that I wonder why someone would spend their time read through all my posted code. But then, I realize that it can be a fun challenge to spot problems in others code, especially if someone is experienced in coding.
This file I think replaces the bracket strings in the .tpl files, well many will know better than me. My question is again how would I introduce the contents of an include() file that's from a separate server, into this code? Is it technically possible? Alright, not enough code they said, bet this will be too much
.
This file I think replaces the bracket strings in the .tpl files, well many will know better than me. My question is again how would I introduce the contents of an include() file that's from a separate server, into this code? Is it technically possible? Alright, not enough code they said, bet this will be too much
Code: Select all
<?php
/**
*
* @Author:
* @File Name:
* @Description: this module returns a string for index (target_city.php) html file
* @Begining date: 11/02/08
* @Update History:
* @Things to do:
*
*/
class IxCreator
{
/**
* @for debugging
*/
var $debug1 = true;
/**
* @array of data from XML
*/
var $aryData;
/**
* @regular expression for title
*/
var $re_title = "/\{\\\$title\}/";
/**
* @regular expression for description
*/
var $re_description = "/\{\\\$desc\}/";
/**
* @regular expression for menu
*/
var $re_menu = "/\{\\\$menu\}/";
/**
* @regular expression for navigator
*/
var $re_navigator = "/\{\\\$navi\}/";
/**
* @regular expression for city list
*/
var $re_city_list = "/\{\\\$city_list\}/";
/**
* @regular expression for country name
*/
var $re_country_name = "/\{\\\$country_name\}/";
/**
* @regular expression for city name
*/
var $re_city_name = "/\{\\\$city_name\}/";
/**
* @regular expression for city
*/
var $re_city = "/\{\\\$city\}/";
/**
* @desc : constructor of this class
* @acce : public
* @param: obj $_ary instance of array object which contains XML data
* @return: none
*/
function IxCreator($_ary)
{
$this->aryData = $_ary->createArray();
}
/**
* @desc : this returns index (target_city.php) page which is modifyed by array of XML data
* @acce : public
* @param : none
* @return : string of html file (index (target_city.php))
*/
function getHtml()
{
//-- gets template
$tpl = file_get_contents("./templates/ix.tpl");
//-- replace specific tags for specific contents
$tpl = preg_replace("/\{\\\$lang_def\}/", STR_LANGUAGE_DEFINITION, $tpl);
$tpl = preg_replace("/\{\\\$meta_for_lng\}/", STR_META_FOR_LNG, $tpl);
$tpl = preg_replace("/\{\\\$meta_for_description\}/", STR_META_FOR_DESCRIPTION, $tpl);
$tpl = preg_replace("/\{\\\$meta_for_css_def\}/", STR_META_FOR_CSS_DEFINITION, $tpl);
$tpl = preg_replace("/\{\\\$meta_for_script_def\}/", STR_META_FOR_SCRIPT_DEFINITION, $tpl);
$tpl = preg_replace("/\{\\\$title_in_body\}/", STR_TITLE_FO_INDEX_IN_BODY, $tpl);
$tpl = preg_replace("/\{\\\$quick_serch_panel\}/", STR_QUICK_SEARCH_PANEL, $tpl);
$tpl = preg_replace("/\{\\\$title_of_region_links\}/", STR_TITLE_OF_REGION_LINKS, $tpl);
$tpl = preg_replace("/\{\\\$str_africa\}/", STR_AFRICA, $tpl);
$tpl = preg_replace("/\{\\\$str_asia\}/", STR_ASIA, $tpl);
$tpl = preg_replace("/\{\\\$str_europe\}/", STR_EUROPE, $tpl);
$tpl = preg_replace("/\{\\\$str_north_america\}/", STR_NORTH_AMERICA, $tpl);
$tpl = preg_replace("/\{\\\$str_south_america\}/", STR_SOUTH_AMERICA, $tpl);
$tpl = preg_replace("/\{\\\$str_oceania\}/", STR_OCEANIA, $tpl);
$tpl = preg_replace("/\{\\\$title_of_top_cities\}/", STR_TITLE_OF_TOP_CITIES, $tpl);
$tpl = preg_replace("/\{\\\$top_cities\}/", STR_TOP_CITIES_CODE, $tpl);
$home_url = URL_OF_THIS_SITE . MY_PROGRAM_NAME;
$tpl = preg_replace("/\{\\\$home_url\}/", $home_url, $tpl);
$tpl = preg_replace("/\\\http:/", "http:", $tpl);
$tpl = preg_replace($this->re_title, $this->getTitleCode(), $tpl);
$tpl = preg_replace($this->re_description, $this->getDescriptionCode(), $tpl);
$tpl = preg_replace($this->re_menu, $this->getMenuCode(), $tpl);
$tpl = preg_replace($this->re_navigator, $this->getNaviCode(), $tpl);
$tpl = preg_replace($this->re_city_list, $this->getcityListCode(), $tpl);
return $tpl;
}
/**
* @desc : makes code for title
* @acce : private
* @param : none
* @return : string - page title
*/
function getTitleCode()
{
return STR_TITLE_OF_INDEX;
}
/**
* @desc : makes code for description
* @acce : private
* @param : none
* @return : string - page description
*/
function getDescriptionCode()
{
return STR_DESCRIPTION_FOR_INDEX;
}
/**
* @desc : makes html code of menu
* @acce : private
* @param : none
* @return : string - html code of menu
*/
function getMenuCode()
{
$rtnvalue = file_get_contents("./templates/menu.tpl");
$rtnvalue = preg_replace("/\{\\\$url_of_this_site\}/", URL_OF_THIS_SITE, $rtnvalue);
$rtnvalue = preg_replace("/\{\\\$home\}/", STR_HOME, $rtnvalue);
$rtnvalue = preg_replace("/\{\\\$link\}/", STR_LINK, $rtnvalue);
$rtnvalue = preg_replace("/\{\\\$menu\}/", STR_MENU, $rtnvalue);
$rtnvalue = preg_replace("/\{\\\$quick_serch\}/", STR_QUICK_SEARCH, $rtnvalue);
return $rtnvalue;
}
/**
* @desc : makes code for navigation part
* @acce : private
* @param : none
* @return : string - html code of navigation
*/
function getNaviCode()
{
$rtnvalue = "";
$rtnvalue .= "<a href=\"" . URL_OF_THIS_SITE . "\">" ;
$rtnvalue .= STR_HOME;
$rtnvalue .= "</a>";
return $rtnvalue;
}
/**
* @desc : generates the list of citys for index page
* @acce : private
* @param : none
* @return : string - list of citys for index page
*/
/**
* @desc : generates name of country or city name in
* appropriate form for url
* @acces: private
* @param : string - name of country or city
* @return : string - name of country or city for url
*/
function getNameInAppropriateFormForUrl($_str)
{
$rtnvalue = str_replace("'","", $_str);
$rtnvalue = str_replace("&","and", $rtnvalue );
$rtnvalue = str_replace(" ","_", $rtnvalue );
$rtnvalue = str_replace("-","_", $rtnvalue );
$rtnvalue = str_replace("/","_", $rtnvalue );
$rtnvalue = strtolower($rtnvalue);
$rtnvalue = eregi_replace("\|[a-zA-Z][a-zA-Z]", "", $rtnvalue);
return $rtnvalue;
}
}
?>
Re: Help with Template Mess
First off, your regexes can be made a bit simpler by wrapping them in single quotes so the $ isn't evaluated:
ex: [text]/\{\$city\}/[/text]
Second, if you're getting a file from another server, all you're going to get is the output from running the file. I assume that's what you want?
Finally, to get your remote content you will need to:
ex: [text]/\{\$city\}/[/text]
Second, if you're getting a file from another server, all you're going to get is the output from running the file. I assume that's what you want?
Finally, to get your remote content you will need to:
- Create a new class variable that will define your regular expression for finding the tag to replace. If this is content is injected only per file, it's much more efficient to use str_replace(), but in the interests of following the existing code flow, you should set up a regex
- Modify getHTML(). Somewhere in there you need to retrieve the contents of the remote file. file_get_contents() works well, but you'll need to make sure HTTP wrappers are turned on (I thing they generally are).
- After the file contents have been retrieved, mimic the existing lines that call preg_replace(), using your regex and the remote file content.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.