Destramic Posted December 20, 2010 Share Posted December 20, 2010 hey guys im making a template class which is working and im trying to impliment a loop section for MySQL results... but i need to be able to find a match for a string such as: {section name=1 loop=$2} content {/section} but also extract the values of name, loop and the section content if anyone could advise me on the best way this can be done please...thank you Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/ Share on other sites More sharing options...
johnny86 Posted December 20, 2010 Share Posted December 20, 2010 Why dont you just use PHP for looping your SQL results? There is really no need for templates. PHP itself is a templating language. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1149483 Share on other sites More sharing options...
Destramic Posted December 20, 2010 Author Share Posted December 20, 2010 well ive come to the conclusion that i think a template engine would be good for my website for i can edit the .tpl file when things need to be changed without the php side of things being toutched by others Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1149485 Share on other sites More sharing options...
johnny86 Posted December 20, 2010 Share Posted December 20, 2010 Well that's a common misunderstanding of templates. Your .tpl files migh just as well be .php files. What's the reason for creating your own difficult templating syntax like {loop name=something loop=$2} do something {/loop} When you can just do this in your template file: <? foreach $data as $row ?> <div> <p><?= $row->topic ?></p> <?= $row->content ?> </div> <? end foreach ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1149487 Share on other sites More sharing options...
Destramic Posted December 20, 2010 Author Share Posted December 20, 2010 but wont template files load quicker? and whats the point in smarty then? Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1149488 Share on other sites More sharing options...
johnny86 Posted December 20, 2010 Share Posted December 20, 2010 There is no possible way that a template would load faster. When you use just PHP the code is parsed just by PHP where as when you use template the code is parsed by PHP and then your own template parser which makes no sense at all. Smarty has no point at all what so ever in my opinion. Here is an article about templates: http://www.phppatterns.com/docs/design/templates_and_template_engines You should read something about the MVC pattern (Model View Controller) You don't even need to implement it fully. You could only use the view for your purposes. When you want to show something, you just create a "view". Fill it with data and then extract the variables to global scope and include a template file containing all the processing of the variables. And there you have it a basic template system. No large engines like smarty that will just create more complexity to your programs and slow them down. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1149491 Share on other sites More sharing options...
johnny86 Posted December 20, 2010 Share Posted December 20, 2010 Further reading: http://www.massassi.com/php/articles/template_engines/ There is a short example also. And thats how simple it really is. And as that article points out. The point of templates is not to seperate PHP from your HTML code. It is to seperate business logic from the presentation or "view". There are as much opinions as there are coders. But I'm on the opinion of just using PHP in my templates. Since it doesn't matter what syntax is used. As long as the job of seperating of business logic and presentation achieved. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1149495 Share on other sites More sharing options...
Destramic Posted December 20, 2010 Author Share Posted December 20, 2010 thank you...do you have a good article about mvc please? Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1149496 Share on other sites More sharing options...
johnny86 Posted December 20, 2010 Share Posted December 20, 2010 Well I must point out that the MVC pattern is very widely used and there is a tremendous amount of different implementations of it. You are going to have your head stuck for a while before understanding it. Google might be a good starting point and examining different frameworks that use MVC. To get back to where we started, you building your own template engine. Try to replace it with just a simple View implementation. You'll still have your template files. But you dont have to parse through your own syntax. Just use PHP instead. And a way to do it is just create your presentation class. In the last article I posted here: http://www.massassi.com/php/articles/template_engines/ there is basicly a "view" implemented but it uses a name template. See how that works. It actually works just as I said before; It takes in data and extracts it to local scope and then includes the template file. And captures the output using output buffering. This article scratches some surface of MVC and it also has some aspects of frameworks. I'm quessing this article encourages you to use frameworks because they are tested and has an active community behind them. I mean instead of creating your own. But this is also a matter of "Do I need framework for this project?". If it's a big project, a framework would be more than welcome to keep things simple and by simple I mean that framework forces all coders to do certain things in a same way. But anyway, this is a conversation which could last for ages so here's the article: http://net.tutsplus.com/tutorials/other/mvc-for-noobs/ Hope this helps you on the right track. Just consider what's the best solution for your needs. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1149498 Share on other sites More sharing options...
Destramic Posted December 20, 2010 Author Share Posted December 20, 2010 thank you i'll have a read and let you know how i get on...maybe i will scrap the template engine Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1149585 Share on other sites More sharing options...
Destramic Posted December 22, 2010 Author Share Posted December 22, 2010 hey johnny i found a good article i think http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/comment-page-2/#comment-8619 is this a good example of mvc? Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150412 Share on other sites More sharing options...
laffin Posted December 22, 2010 Share Posted December 22, 2010 Templating systems are only really useful when you have a group, and dont want them to have access to php functionality. But if its a project for yourself, than yeah, php will be a better choice as a templating system. A nice example would be here: Template Engines its pretty dated, as the template they use as an example uses: <?=$var;?> since than php has gone to the use full tags, as common practice: <?php echo $var; ?> Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150452 Share on other sites More sharing options...
lastkarrde Posted December 22, 2010 Share Posted December 22, 2010 Twig is the best example of a proper PHP templating engine, you would learn alot from it's source code. It has it's own lexer and parser that goes through the template and extracts all information. It then compiles and caches the template into straight PHP (so it doesn't need to lex and parser every request). Leaving you with super fast templates :-) Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150457 Share on other sites More sharing options...
johnny86 Posted December 23, 2010 Share Posted December 23, 2010 I just don't get the idea of twig either. I mean talking about overcomplicating things? When you need 150 files of PHP code to get your templates working. I really can't see the benefits of that. I looked trough Twigs documentation and source code, and yes it's well written and very nice project. The thing is tough, Twig offers a lot of different features, you can do all that in a whole lot less of code just by using simple PHP in your templates. The reason for template is not to complitely remove PHP code from it. Templates are just to seperate your business logic from your presentation. And that way you can change the logic of your application without touching the view part of your code and you can change how you show things without touching your logic. Further more, you'll have to learn Twig syntax too. And instead of a little bit of PHP you now have Twig all over your templates. What's the point in that? It's more likely that a person knows PHP syntax than Twig syntax. And all it's going to do is annoy the person after you, trying to change the way things should show up. Caching is not that difficult to implement in your own PHP application. When request arrives, check cache and act according to that and if needed save the product before response. You can also make a big difference just by sending proper caching headers in the response. That is what browsers caches are for. Laffin: I know what you mean. But I can't understand why would you want to deny access? If you are creating a website in one big group. Are your employees untrustworthy? What is exactly the problem of letting them do <?php foreach ?> instead of {% foreach %} ? It's not like they are going to brake your site with that? You might say that they could create errors, well they can create them just as much with a bad usage of templating syntax. I really don't see the point. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150640 Share on other sites More sharing options...
johnny86 Posted December 23, 2010 Share Posted December 23, 2010 hey johnny i found a good article i think http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/comment-page-2/#comment-8619 is this a good example of mvc? Well that's one way of implementing MVC. But the thing that bothers me there is that the application redirects every request to index.php. Then it tries to resolve what kind of request it was and what do I have to load. That seems pretty annoying to me. Also there is a lot of stuff that doesn't belong to the MVC pattern which blurs out the MVC pattern a little bit. MVC is really a quite simple thing. There are only the three things: Model, View and Controller. Model: Handles all your data. Commonly database transactions, what a surprise =) View: Just shows the final product to the user Controller: Is inbetween Model and View. This is where your real "application" is. It is the logic of your application. Controller gets it's data from model by asking the model. And then Controller does things with the data. After that Controller gives the data to View and View renders it the way desired. Everything else is built around those three things. But to simplify a little bit. You propably have already used "controllers". You can consider your index.php to be a Controller. It is Controller where your logic for your frontpage is located. In MVC you would break the index.php to three bits and leave all the logic there. And take out data handling complitely to another file, lets say "Model.php". Then you would create "View.php" where you have your HTML and a little bit of PHP. After that your index.php could be like this: include your Model.php. Ask the mdel for example: $userinfo = Model_Get_User_Info("johnny"); Now you would do something for the data that needs to be done. Assign the data to your variables inside index.php like this: $username = $userinfo["name"]; Now you could start output buffering if you want to save the result. include("View.php"); Close buffering if its on. And there you have it, final product. Where View.php could contain: Hello there <?php echo $username; ?> <-- that is assigned in the controller, and controller got it from model That's really all there is to MVC. Everything else is built around it. =) Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150643 Share on other sites More sharing options...
johnny86 Posted December 23, 2010 Share Posted December 23, 2010 So what are your toughts? I really want to talk about this because this interests me. In general application design is intreating. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150909 Share on other sites More sharing options...
laffin Posted December 23, 2010 Share Posted December 23, 2010 I know what you mean. But I can't understand why would you want to deny access? If you are creating a website in one big group. Are your employees untrustworthy? What is exactly the problem of letting them do <?php foreach ?> instead of {% foreach %} ? It's not like they are going to brake your site with that? You might say that they could create errors, well they can create them just as much with a bad usage of templating syntax. I really don't see the point. Well I made some template systems before, and the major reason to make these systems is to allow users access to some variables and functions. So it allows any user to be able to design content without knowledge of php or coding. I think my first one was for a image tag generator, which allowed users to change background images, choose a font, and write text with some of their user infomation as variables. But I totally agree with your statements. template systems should make display easier for the end user. So providing a completely new scripting language is rather pointless, as the end user would have to learn a new language. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150917 Share on other sites More sharing options...
johnny86 Posted December 23, 2010 Share Posted December 23, 2010 Sounds interesting. So did you allow the end users to change their sites background to whatever they preferred or what was going on there? And what is the reason for letting users access their variables? Can't they just type it in if they want to tell their e-mail or homepage? I can only see one benefit from that. If they get it from variable, it would update to all their previous posts also. On a pretty small forum that would work I guess. But as the site gets bigger and gets thousands of hits per hour. Those things are the ones that will take a lot of resources. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150920 Share on other sites More sharing options...
laffin Posted December 23, 2010 Share Posted December 23, 2010 No the tags were like XBox Gamer tags. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150940 Share on other sites More sharing options...
johnny86 Posted December 23, 2010 Share Posted December 23, 2010 Okay well that sounds very reasonable. And that is only one tag to replace. What was the image tag generator? In general I think that end users should not have any kind of control over the templates. They shouldn't be able to make loops or anything like that. And if end user needs to do some decoration, that's what HTML is for. I think templating is about how you want to show your data. It's not the dynamic content that the template will be filled with. So giving users the opportunity to show some statistics of themselves isn't really templating. It's dynamic content. You talked about Twig earlier on. It's funny that it creates a PHP template from it's own syntax =) And why? I think because PHP is just so powerful and fast. I'd gladly just skip using Twig and writing the templates with PHP from the begining. Twig might be fast but on a large scale project, it would only add another rather useless layer which would slow down things quite a lot in my opinion. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150949 Share on other sites More sharing options...
laffin Posted December 24, 2010 Share Posted December 24, 2010 The image tag generator only had a few commands image - Select a background image font - Select font color - Set Background foreground color coord - Set X,Y Coordinates text - write on image with text u had opportunity to have custom variables: username, url, email, posts, rank, etc..... it was more of a system to allow users a unique sig in the forums. but it was found useful in other sections of the site to display statistics and such. But it still the same concept of a template system. Since I didnt want users direct access to GD/PHP functions, and for many programming php code to display a simple tag like this was way over their heads, and many which have no coding experience. It's been a number of years since i wrote that code.... (Just for nostalgia i pulled out the code to look at the script). U can find a lot of similarities of systems when it comes to templating. Templating just sets forth a standard of doing something. U set the page layout, and tags for content. allow the core engine to the rest. Twig, sumone else brought up. Even tho that the concept is nice, I dont see a need for it. Your OP was on how to build one, Thats the reason I posted a link to a templating system that was more PHP specific, being somthing like 50 lines of code, makes it easy to understand. And gives you some idea about using cache systems. The article is more of a guide. Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150993 Share on other sites More sharing options...
johnny86 Posted December 24, 2010 Share Posted December 24, 2010 Yeah offcourse if an end user is allowed to do something specific you need some kind of an engine to sort it out. But acttually what was rather amusing. We both seem to be mixing up who said what I didn't start this topic and you didn't bring up twig. I actually was on the opinion that generally templating isn't reasonable if only developers or designers work on the project. I just found you to be someone who I could have a good conversation in a wider view and aspects of the matter here. =) Quote Link to comment https://forums.phpfreaks.com/topic/222203-template-help/#findComment-1150996 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.