Webgamers / ProjectReling

Our second community project
http://webgamers.de/index.php?page=Board&boardID=81
1 stars 1 forks source link

Template engine #3

Open Ma27 opened 10 years ago

Ma27 commented 10 years ago

A controller should never generate the html of the view. I recommend a template engine to render templates and use the result as view.

If we don't use Twig, we could use PHP as engine. I've implemented a simple template php templating engine which we may use: http://pastebin.com/j9vw9guD

LinuxDoku commented 10 years ago

I vote for a real template engine, cause PHP code in templates will lead to templates which contain too much logic.

Twig would be a good choice it's simple and easy to understand - also for non programmers. And it covers the most common control structures like if, foreach, etc..

chrischi1989 commented 10 years ago

I've implemented a simple template php templating engine [...]

You should take a look at the extract() function because I think this function is just perfect to pass multiple types of data to a template/view. CodeIgniter, Laravel and AFAIK Yii are also using this function for their template mechanisms.

For other functionalities like replacing text, formating/converting dates, timestamps, etc. I'd suggest a helpers.php with plain old functions inside. Just like some nice shorthands which can be used everywhere in the code.

[...] cause PHP code in templates will lead to templates which contain too much logic.

Please define "too much logic". The only things we might need inside a template would be if-else, foreach, a include function for sub-templates and maybe shorthand for translation.

Even I know that Twig is simple I don't think that we need a full scale templating engine for these few things.

Besides this I currently only see more or less advanced developers in the webgamers group, which know to keep logic out of the templates/views or at least as small as possible. And if there are other people contributing their stuff via pull-request we still can deny it if there is too much logic inside the templates/views.

BlackScorp commented 10 years ago

"too much logic" means that youre able to do some crazy things inside the template(e.g. call a method of an object, create new instance of a new class and so on)

you can get some informations if you google for "logic less template engine" Mustache is one for example, you cannot define there even an else or elseif or if something === anything

while in twig, you can even check for regular expressions..

Ma27 commented 10 years ago

Please define "too much logic". The only things we might need inside a template would be if-else, foreach, a include function for sub-templates and maybe shorthand for translation.

well, PHP has many libraries and features and if you would use stuff like the SPL inside of a template, it would be too much logic. That's the problem with PHP as template language.

extract()

I don't like extract. In my approach I use ArrayObject + Closure Binding to have a new scope for the template. In case of extract the scope of the render() Method and the scope of the template would be the same one which means, that you could access stuff from the template object.

For other functionalities like replacing text, formatting/converting dates, timestamps, etc. I'd suggest a helpers.php with plain old functions inside. Just like some nice short hands which can be used everywhere in the code.

Twig supports functions, too :)

while in twig, you can even check for regular expressions..

well, the big advantage of twig is, that it is extremely flexible (if you work with Symfony, you see how much we could do with it). The problem with mustache is, that we don't have features like template inheritance.

BlackScorp commented 10 years ago

well mustache is also extermly flexible and you dont need inheritance feature for templates, you can still reuse template parts with partials, but i dont want say anything against twig, iam not a part of that project anyways;) iam just against PHP templates

tector commented 10 years ago

mustache is great! +1 i am totally with @blackscorp: i am against php template engines but if you want to use one mustache is the way to go!

LinuxDoku commented 10 years ago

I am against mustache, cause this project is for beginners and beginners need some kind of logic in templates. Otherwise they will produce bad server side code (I think of storing pre rendered timestamps in database to reduce difficulty in displaying them., etc.).

I know why mustache exists and appreciate its usage in well designed professional projects where every developer understands separations of concerns. But this project is meant to be an entry level one - so I would choose Twig rather than Mustache.

BlackScorp commented 10 years ago

i think, if the project would be for beginners, why the heck there is already a DI Container? beginners dont know why the need Depdency Injection ;)

chrischi1989 commented 10 years ago

i think, if the project would be for beginners, why the heck there is already a DI Container?

Since I made the first pull-req I was wondering how long it would take if someone come up with that issue and you're absolutely right about this. But this should be discussed in a separate topic because I personally came to the clue that DI is somekind of a tipping point.

tector commented 10 years ago

So where do you draw the line between 'beginner' and 'advanced'. Does a beginner know what a template engine is? And if the beginner uses the template engine... does he use it right and does he keep the logic out of the templates? It is a strength of Mustache to completely deny logic in templates so a beginner can't make this mistake...