Ginho Posted August 8, 2012 Share Posted August 8, 2012 Ciao a tutti, I'm new and I've got a question: I made small template engine in PHP and works very well with a HTML file. But when I wanna include a PHP file, in the part I wanted, it shows "1" and the file is included at the top of the page. Here's a part of the code (maybe the affected): foreach($this->values as $key => $value) { $Tags_To_Replace = "[$key]"; $this->output = str_replace($Tags_To_Replace, $value, $this->output); } return $this->output; "values" is an array $this->output = file_get_contents($this->file); the code for include the file set("Left", require_once('pages/left.php')); Can someone help me? If you need more information, ask Thanks Thanks so much!! P.S = Sorry for my bad english, i'm italian Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/ Share on other sites More sharing options...
trq Posted August 8, 2012 Share Posted August 8, 2012 Firstly, you have't shown us enough code. Secondly, there are a few easily noticeable errors. require_once does not return a string, so unless you are using return to return a value within your left.php file, that is why you are receiving 1. The other part that is confusing. You say "values is an array", you then show us some seemingly unrelated code. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1367744 Share on other sites More sharing options...
Ginho Posted August 8, 2012 Author Share Posted August 8, 2012 First of all thank you Firstly, you have't shown us enough code. function set: function set($Key, $Value){ $this->Values[$Key] = $Value; } function: __construct: function __construct($file) { $this->File = $File; $this->output = file_get_contents($this->File); } function output: function output() { foreach($this->Values as $Key => $Value) { $Tags_To_Replace = "[$Key]"; $this->output = str_replace($Tags_To_Replace, $Value, $this->output); } return $this->output; } declaration of the variables: public $output; public $File; public $Values = array(); The other part that is confusing. You say "values is an array", you then show us some seemingly unrelated code. Sorry I was confused Thanks again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1367843 Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 Why don't you post the entire class? Doing so will ensure that the people trying to help you can be certain that they know the entire scope, and as such not only be more inclined towards helping you but also find it easier to give you accurate and speedy assistance. Providing some example data would also be extremely appreciated, as it allows us to observe ourselves what the problem is firsthand. The more we know about a problem, the easier it is to spot the solution, after all. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1367886 Share on other sites More sharing options...
Ginho Posted August 8, 2012 Author Share Posted August 8, 2012 I posted the whole class, I've broken it to better understand. class Template{ public $output; public $file; public $values = array(); function __construct($file) { $this->file = $file; $this->output = file_get_contents($this->file); } function set($key, $value){ $this->values[$key] = $value; } function output() { foreach($this->values as $key => $value) { $Tags_To_Replace = "[$key]"; $this->output = str_replace($Tags_To_Replace, $value, $this->output); } return $this->output; } } Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1367901 Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 Do you have some example data as well, that produces the erroneous result? PS: Breaking up a class like that doesn't make it easier to understand, but harder as it breaks the logical flow of the code. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1367911 Share on other sites More sharing options...
Ginho Posted August 9, 2012 Author Share Posted August 9, 2012 Here's the index.php, which uses the template engine error_reporting(E_ALL); ini_set("display_errors", 1); //Includo il template engine require_once("template.php"); //Includo i files di configurazione //Configurations file require_once("include/config.php"); require_once("include/connect.php"); require_once("include/funzioni.php"); $Template = new Template('default.tpl'); $Template -> set("Titolo_Pagina", "Benvenuti in Ginho! - Il sito che parla di tutto ci? che ruota attorno all'informatica"); $Template -> set("Descrizione", "Il sito che parla di tutto ci? che ruota attorno all'informatica"); $Template -> set("Left", require_once('pages/left.php')); echo $Template->output(); it isn't show any error, and it isn't include correctly.(look the attachement in the first post). config.php contains the costant for the DB connection, connect.php connect to the DB, funzioni.php contains general function (in this case doesn't matter with the current problem) I haven't got any idea to solve it. i've tried the impossible, but.. nothing. It's a vital issue xD Thanks again Secondly, there are a few easily noticeable errors. require_once does not return a string, so unless you are using return to return a value within your left.php file, that is why you are receiving 1. I use return only to output the render template with this: return $this->output; PS: Breaking up a class like that doesn't make it easier to understand, but harder as it breaks the logical flow of the code. Sorry, I will adjust it for next time Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368030 Share on other sites More sharing options...
Christian F. Posted August 9, 2012 Share Posted August 9, 2012 The problem is on this line: $Template -> set("Left", require_once('pages/left.php')); The cause of the problem is quite well explained in the PHP manual for include (). In fact, it's stated in the very first sentence. What you need to do is to rethink your logic, because what you're trying to do here won't work. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368082 Share on other sites More sharing options...
Ginho Posted August 9, 2012 Author Share Posted August 9, 2012 I see this tutorial on Youtube: http://www.youtube.com/watch?v=QjRHjVgnT0o and honestly I tried to act on various parts of the code, but I haven't got more ideas on how to modify... Thanks you for your interest. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368090 Share on other sites More sharing options...
PFMaBiSmAd Posted August 9, 2012 Share Posted August 9, 2012 The second parameter of the ->set() method expects a string, i.e. like the first two uses set("Titolo_Pagina", ... If your 'pages/left.php' file contains the string your want to supply as the second parameter, use file_get_contents instead of require_once. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368099 Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 Why use a foreach instead of using preg_replace? Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368118 Share on other sites More sharing options...
Ginho Posted August 9, 2012 Author Share Posted August 9, 2012 If your 'pages/left.php' file contains the string your want to supply as the second parameter, use file_get_contents instead of require_once. Yeah, it works. I've tried this but the php code it isn't been elaborate. Why use a foreach instead of using preg_replace? ? foreach is a cycle, preg_replace is something completely different... explain better Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368157 Share on other sites More sharing options...
Christian F. Posted August 9, 2012 Share Posted August 9, 2012 With preg_replace () you can use a Regular Expression to capture and replace all of the template variables in one call, skipping the loop entirely. Something which you can do with "str_replace ()" as well, coincidentally, seeing as it supports arrays as the first two arguments. Latter one just requires a bit more work, to get the square brackets added to the keys. In any case, example of a preg_replace () to insert the template data: return preg_replace ('/\[([\w\d_-]+)\]/e', '$this->values[$1]', $this->output); Whether this or the existing method is better, is something you'll have to decide. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368210 Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 With preg_replace () you can use a Regular Expression to capture and replace all of the template variables in one call, skipping the loop entirely. Something which you can do with "str_replace ()" as well, coincidentally, seeing as it supports arrays as the first two arguments. Latter one just requires a bit more work, to get the square brackets added to the keys. In any case, example of a preg_replace () to insert the template data: return preg_replace ('/\[([\w\d_-]+)\]/e', '$this->values[$1]', $this->output); Whether this or the existing method is better, is something you'll have to decide. IIRC, PCRE regex allows you to replace values of any variable in your template. Let's say you use {{var_name}} to signify variables, you can be almost certain that it won't come up randomly in a sentence. You can replace anything that has not been defined with [var_name not defined], or whatnot. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368226 Share on other sites More sharing options...
Ginho Posted August 10, 2012 Author Share Posted August 10, 2012 Thank you! I did not know. Wow with your solution, 5 lines have been optimized in 1! awesome! but how can i include php files correctly? Thanks for your patience Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368304 Share on other sites More sharing options...
Christian F. Posted August 10, 2012 Share Posted August 10, 2012 You're welcome, but as for optimized... Well, that you won't know until you've benchmarked the two methods against each other. When it comes to PHP files you have to keep in mind that when you include them, you execute the code inside of them at the same time. What you need to do, is to separate the PHP and HTML code in your mind (and code). All PHP is parsed on the server, before any output is sent to the browser. Prior to that time the output is simply PHP strings, the fact that the browser interprets it as HTML (JS, CSS, or whatever) is purely coincidental and not important at this time. That means you have to reorganize your code, so that you can either store the output in a variable until it's needed, or use the included code to set the name of the HTML template file to be included. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368347 Share on other sites More sharing options...
Ginho Posted August 10, 2012 Author Share Posted August 10, 2012 You're welcome, but as for optimized... Well, that you won't know until you've benchmarked the two methods against each other. xD c'mon rough guess it's better xD Thanks again... i've got an idea! is it right create a tag , where the php code will process? can i do this with a simple if? Cheers P.S = are correct the periods i wrote? xD Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368449 Share on other sites More sharing options...
maxudaskin Posted August 10, 2012 Share Posted August 10, 2012 Even if you could, that would present major security issues. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368452 Share on other sites More sharing options...
Ginho Posted August 10, 2012 Author Share Posted August 10, 2012 Even if you could, that would present major security issues. Why? I can parse it with some filters... so how can I di this? Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368462 Share on other sites More sharing options...
Adam Posted August 10, 2012 Share Posted August 10, 2012 Your templates should be logic-less. There's no reason to have to include PHP within a template. Take a look {{mustache}} as an example; simple tags allow for echoing variables, including partials and looping through data sets. You shouldn't need to do anything more in them. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368477 Share on other sites More sharing options...
Ginho Posted August 12, 2012 Author Share Posted August 12, 2012 OK. perfect. one question: how do you manage the pages? Currently I have two files, called header.php and footer.php, which I include on the interested page. And you? What's the better method to manage pages in a CMS? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368753 Share on other sites More sharing options...
Adam Posted August 12, 2012 Share Posted August 12, 2012 Well blocks of content are traditionally entered using a WYSIWYG editor. The header and footer though would need to be more controlled, given that anyway who doesn't know what they're doing could completely destroy the website. You don't really need to worry about proving a way to edit the templates, no one that isn't a dev should be able to go in and change the HTML, and the dev can do that in the file as usual. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368764 Share on other sites More sharing options...
Ginho Posted August 12, 2012 Author Share Posted August 12, 2012 You don't really need to worry about proving a way to edit the templates, no one that isn't a dev should be able to go in and change the HTML I would use my cms to another site and create a new template with ease, thanks to the separation of design and code. that's all... for example, i tried to see the joomla code but i'm getting crazy, and i don't know how can do this. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/266802-my-template-engine-doesnt-include-correctly-files/#findComment-1368765 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.