ComGuar Posted April 3, 2013 Share Posted April 3, 2013 I want to separate the presentation from the business logic, and i wrote a class. Template engine working like any other. Example: page.tpl <html><body> <div id="content"> {{content}} </div> </body></html> index.php $content_i_want_to_add = 'yeah, this is my page content'; $content->set('content', $content_i_want_to_add); This part is working great. Problem is when i need to include file on index.php with some data that can be turned on or off. Example: index.php $content->set('content', $content_i_want_to_add); if ($show_time == "on") { include"time.php"; } // how to "set" this? time.php if ($show_time == "on") { echo '<div align="center">' . date("d.m.Y.") . '<br />' . date("H:i") . '</div>'; } This is just example, i have a lot of other, more complex, situations when i need to include a file (echo data that i include). How to solve this? Did i must to put all data in variable or there is more "elegant" way? time.php if ($show_time == "on") { $time = '<div align="center">' . date("d.m.Y.") . '<br />' . date("H:i") . '</div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/ Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 (edited) Typically the template does that logic, along the lines of {{if show_time eq "on"}} {{time}} {{/if}} Edited April 3, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422753 Share on other sites More sharing options...
ComGuar Posted April 3, 2013 Author Share Posted April 3, 2013 (edited) Thanks requinix, but i'll try to find other solution. I have a lot more situations when i need to include file, not only to "show time". Edited April 3, 2013 by ComGuar Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422772 Share on other sites More sharing options...
trq Posted April 3, 2013 Share Posted April 3, 2013 Is there a particular reason your reinventing this wheel? If you really want to use a templating engine there are some awesome engines around these days. Twig is very feature full yet light weight. I personally don't see the point in templating engines, separation of presentation logic from the business logic is more in your app design. Templating engines do little to resolve this issue besides adding another layer between the two. Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422773 Share on other sites More sharing options...
ComGuar Posted April 3, 2013 Author Share Posted April 3, 2013 Now i do not use any template engine.Whole site is on one template, and all pages have same design, only content is changable.With template engine, there will be option to have completely different design for some pages + there will be much easier to apply new design when needed. Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422780 Share on other sites More sharing options...
trq Posted April 4, 2013 Share Posted April 4, 2013 With template engine, there will be option to have completely different design for some pages + there will be much easier to apply new design when needed.Again, this can be achieved without a templating engine. Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422799 Share on other sites More sharing options...
ComGuar Posted April 4, 2013 Author Share Posted April 4, 2013 (edited) Thanks for helping me trq. Can you tell me how to do this without template engine? Note that html code must be in template files, separated from php code. Edited April 4, 2013 by ComGuar Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422841 Share on other sites More sharing options...
trq Posted April 4, 2013 Share Posted April 4, 2013 Note that html code must be in template files, separated from php code.I think your confused about what business logic is. Just because code is written in php does not mean it is business logic. Take this simple template example to list usernames and email addresses. <html> <head> <title>Users</title> </head> <body> <ul> <? foreach ($users as $user): ?> <li><?= $user->name; > - <?= $user->email; ></li> <? endforeach; ?> </ul> </body> </html> While this html has php within it, theres is no business logic here, and it wouldn't really be written any differently using a template engine. For example, in Twig: <html> <head> <title>Users</title> </head> <body> <ul> {% for user in users %} <li>{{ user.name }} - {{ user.email }}</li> {% endfor %} </ul> </body> </html> It might be a little cleaner, but really, that's about it. Of course there is no data escaping in either example and once you start down that path template engines really start to show there benefit, but still even that can be easily accommodated in php alone of you know what your doing. eg; In Twig you would use: <li>{{ user.name|e }} - {{ user.email|e }}</li> while in php you might use something like: <li><?= e($user->name); > - <?= e($user->email); ></li> Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422853 Share on other sites More sharing options...
ComGuar Posted April 4, 2013 Author Share Posted April 4, 2013 (edited) No, i do this this way: foreach ($users as $user) { $row = new Template("listusers.tpl"); foreach ($user as $key => $value) { $row->set($key, $value); } $usersTemplates[] = $row; } // merge in variable $users_list = Template::merge($usersTemplates); $usersList = new Template("listusers.tpl"); $usersList->set("users", $users_list); And this is working great. Problem is when i have situation to include file with some data. This included file have sometimes more then 400 lines of code. Edited April 4, 2013 by ComGuar Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422856 Share on other sites More sharing options...
trq Posted April 4, 2013 Share Posted April 4, 2013 I'm not sure you even understood my last reply. I'll leave you to it. Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422857 Share on other sites More sharing options...
ComGuar Posted April 4, 2013 Author Share Posted April 4, 2013 (edited) Hehe we do not understand each other. Maybe i need to write function like $data = get_data('file/i_need_to_include.php'); or something like this. and $usersList->set("data", $data); Edited April 4, 2013 by ComGuar Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422858 Share on other sites More sharing options...
ComGuar Posted April 5, 2013 Author Share Posted April 5, 2013 Problem solved, i wrote function to echo included file. function echo_inc() { global $data; include "data_file.php"; echo $mydata; } $mydata = $echo_inc(); Thanks everyone for helping! Quote Link to comment https://forums.phpfreaks.com/topic/276505-template-and-include/#findComment-1422994 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.