per1os Posted April 20, 2007 Share Posted April 20, 2007 Hey all, Designing a new system I am looking into a "template" system as to say that is relatively easy to modify etc. What I came up with was to use a constant variable to house the template information for a specific area. I am curious if this is frowned upon or not. Currently here is what it would look like: <?php define('TEMPLATE_EXTRA', 'This is a template %REPLACE_VAR% !!!'); $replace['REPLACE_VAR'] = "awesome"; $template = replaceTemplateVar(TEMPLATE_EXTRA, $replace); print $template; function replaceTemplateVar($template, $replace) { foreach ($replace as $what => $with) $template = str_replace("%" . $what . "%", $with, $template); return $template; } ?> Pretty laid back and basic. The issue that I am not sure about is there could potentially be 25-50 different templates defined as a constant every time the page is loaded. I am not sure if that is a problem or not. Looking for some insight to the situation of having alot of constants defined with some having potentially a lot of text. Thanks. Note: There is no issue with the code. Link to comment https://forums.phpfreaks.com/topic/47935-excessive-constants/ Share on other sites More sharing options...
utexas_pjm Posted April 20, 2007 Share Posted April 20, 2007 I don't like the idea of template engines like smarty where you ask PHP to parse template code, using str_replace or regex or whatever. In my opinion that is inefficient and you're building a scripting language within a scripting language. I prefer an approach like this, where you use PHP as the template language. <?php class TemplatePage { function process() {} function registerVar($name, $value) { $this->$name = $value; } function renderTemplate($tpl) { require($tpl); } } class SomeTemplatePage extends TemplatePage { function process() { $this->registerVar('replace_var', 'awesome'); $this->registerVar('some_array', array(1,2,3,4,5)); $this->renderTemplate('test.tpl.php'); } } $c = new SomeTemplatePage(); $c->process(); ?> test.tpl.php <html> <body> This is a template <?php echo $this->replace_var ?> !!! <?php foreach($this->some_array as $num) { echo $num . '<br />'; }?> </body> </html> Best, Patrick Link to comment https://forums.phpfreaks.com/topic/47935-excessive-constants/#findComment-234322 Share on other sites More sharing options...
per1os Posted April 20, 2007 Author Share Posted April 20, 2007 Yea, thats not a bad idea, but I do not like the file portion. I like to keep everything in a database. I know I could do it this way and use eval, but the reason I am coding it in such a way is for my designer, he does not know php but can work with a scheme I create. But I may use that code in other scripts that I create. I think I will try and test the constant issue I am describing, and see if the server load changes at all, but what I will probably end up doing is store these in the DB and make a function that grabs the template and than parses it. I will post any new information I find out regarding constants. Link to comment https://forums.phpfreaks.com/topic/47935-excessive-constants/#findComment-234326 Share on other sites More sharing options...
per1os Posted April 20, 2007 Author Share Posted April 20, 2007 Well I created a function to test the use of 500 constants that were created "on the fly". My results were as follows: It took 0.004493 seconds to create 500 constants and print 3 out. The code used is here: <?php $start = microtime(true); for ($i=0;$i<500;$i++) { define('CONSTANT' . $i, 'This is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testThis is a relatively long string testively long string testThis is a relatively long string testThis is a relatively long string test'); } echo CONSTANT255 . "<br />"; echo CONSTANT450 . "<br />"; echo CONSTANT0 . "<br />"; $end = microtime(true); $total = ($end - $start); print '<br /><br />It took ' . $total . ' seconds to create 500 constants and print 3 out.'; ?> As far as I can tell the processing time will not be a huge difference, but what about server memory? Link to comment https://forums.phpfreaks.com/topic/47935-excessive-constants/#findComment-234339 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.