Template Class: Getting the logics in – If loop

In the previous post, we have seen that the template class is not acting as a proper tag replacer. So what it does now is replaces all the variables as a tag. So if we add $tpl->put(”var1?,”SSS”);, it will replace {$var1} in the template with SSS. Now, when we think about the template class, we find that this is still a very raw system. I mean this is a very small system and would be of virtually of no use. So what we do is, we give it some brains. It has to process some logis. It doesnt mean if it can process simple loops such as ifs and fors. So we create a new filter. Our first loop is going to be if loop.

So how do we do it? Well I decided to keep {} as the standard format for my tags. Hence, I added the following system. {if:var_name} {endif:var_name}. Now here, the system checks if the variable name exists, it is going to take the chunk of code between the 2 braces. So now, we can give the following:

{if:var_name}

The variable var_name is present and hence you can see this text.

{endif:var_name}

So, in the run time, if you put up $tpl->put(”var_name”,”1?), then it will show you the text “The variable var_name is present and hence you can see this text”. But if the variable is not present, it will not show you the block of text. To use the variable in the piece of code, you can simply put {$var_name} and it will call the variable. This is a very common system. Though it doesn’t have the best of logics, it is still pretty decent in comparison to most of the template systems. The main advantage of having your own template system is, firstly, you will have a system that you have made and will be having great flexibility. You are not bound by any rules of a system built by someone. And secondly, your logics develop tremendously. Since you are making a parser, you are bound to increase your programming skills. You will find during the course of development that you miss the logics more often than not :) . But that is just the beginning man! you can enjoy this for ever and ever. Even at this point of time my template class is in its growing stage… I am planning to release it very soon. Hope I can complete the tutorial by then :) . More of the if loop very soon! Have a nice day!

About admin

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!