Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template engine #3

Open
Ma27 opened this issue Aug 26, 2014 · 10 comments
Open

Template engine #3

Ma27 opened this issue Aug 26, 2014 · 10 comments

Comments

@Ma27
Copy link

Ma27 commented Aug 26, 2014

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Member

"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
Copy link
Author

Ma27 commented Aug 27, 2014

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
Copy link
Member

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
Copy link

tector commented Aug 27, 2014

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
Copy link
Contributor

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
Copy link
Member

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
Copy link
Contributor

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
Copy link

tector commented Sep 7, 2014

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants