3

Okay so I am developing an advanced framework in PHP using MVC and want to know the best way to format the Views with dynamic data to be parsed by the PHP.

Currently I am using this format:

Format of Controller loading a View

$vars = array();
$vars['EXAMPLE_TEXT'] = "Hello World!";
$this->load->View("view_name", $vars);

Where vars is an array of strings to be passed into the view using the key to identify it in the View.

Format of the "view_name" view.

<b>{EXAMPLE_TEXT}</b>

would just show the text: Hello World!

The problem

But as I am starting to use complex elements from databases (using foreach loops) I can not find a suitable syntax for the view, while keeping it nice for developers and keeping the speed of the parsing engine in PHP.

I was thinking of using a DTD or Schema style but I would really like to hear any input.

Community
  • 1
  • 1
  • 2
    Most sensible frameworks use straight-up PHP code in views. No placeholders or anything. That way you don't add any overhead *and* don't lose any flexibility. Remember that PHP itself is a templating engine, there's no need to any another on top of it. – NullUserException Oct 10 '12 at 21:55
  • Just out of curiosity, what frameworks (PHP or otherwise) have you used in the past? – hafichuk Oct 10 '12 at 21:55
  • I have used CodeIgnighter before, and since then have been creating my own. The overhead is not an issue as the views are generated into straight-up PHP code and cached. When a change is made when developing the cache is updated. The HTML developer never sees the messy PHP code, and the system is not affected. – Stephen Fraser Oct 10 '12 at 21:59
  • eg: [CodeIgniter](http://codeigniter.com/user_guide/general/views.html), [CakePHP](http://book.cakephp.org/2.0/en/views.html), [Kohana](http://kohanaframework.org/3.0/guide/kohana/mvc/views). Your option has the added disadvantage of creating a probably unstandardized syntax that suddenly everyone has to learn. And as you can see already, you're finding yourself adding more and more functionality that is already provided for by PHP. Not to mention the template and the "cached" version of it could easily get out of sync. Why bother? – NullUserException Oct 10 '12 at 22:00
  • Why on earth would you be developing another framework when so many excellent ones exist? most popular ones even have massive communities and support, even. That and the nature of this question alone makes one question just how "advanced" it could possible be. Not to be discouraging or anything. If you have an explanation it might be interesting to know. – Kai Qing Oct 10 '12 at 22:02
  • 1
    This question may be closed as too discursive - see the FAQ. However [this article](http://fabien.potencier.org/article/34/templating-engines-in-php) may be of interest. It comes from a core developer of Symfony, who has moved from a use-PHP perspective, to a we-should-be-using-an-engine-after-all perspective. – halfer Oct 10 '12 at 22:03
  • To make it a little clearer the final aim of the framework is to be able to compile the script from a easy to use Object Orientated Development Structure into fast and optimised sequential scripts hidden from the developer. (So overhead is not an issue) – Stephen Fraser Oct 10 '12 at 22:03
  • @NullUserException That is the question I am asking, is there a standard, or a budding standard that can provide HTML developers an understanding of what they are creating without having PHP splatted everywhere. – Stephen Fraser Oct 10 '12 at 22:11
  • @KaiQing: Here's a great article about reinventing the wheel: http://blog.ircmaxell.com/2012/08/reinvent-wheel.html – Madara's Ghost Oct 10 '12 at 22:12
  • possible duplicate of [What are your templating strategies?](http://stackoverflow.com/questions/1948686/what-are-your-templating-strategies) – Gordon Oct 10 '12 at 22:19
  • and pretty much related: http://stackoverflow.com/questions/62605/php-as-a-template-language-or-some-other-php-templating-script/, http://stackoverflow.com/questions/4667629/separating-code-from-layout-in-template and http://stackoverflow.com/questions/2235179/lightweight-php5-based-template-class-system - and of course you can also XSLT which would need no special syntax whatsoever. – Gordon Oct 10 '12 at 22:20
  • @halfer very interesting article will look into Twig. – Stephen Fraser Oct 10 '12 at 22:21
  • 1
    @Madara: Not saying don't do it. If you see flaws or shortcomings in the current options then go ahead. But do you see why one might question the task at hand when it is coupled with a question like this? Perhaps all I'm saying is making a framework is a massive task, and one that's much harder alone or with a team that couldn't come up with an answer to what appears to be a relatively basic problem. A problem that depends heavily on preference and assumed ease of use. I can think of how I might do it, but that doesn't mean it would be convenient for others. – Kai Qing Oct 10 '12 at 22:23
  • @KaiQing Its a task I have been working on for around 9 months on and off. It is also the basis for a lot of client work and may not reach public release. Asking a community how they would do it and hearing feedback is all part of the process. – Stephen Fraser Oct 10 '12 at 22:28
  • That's a fair answer since it is client based. However I would answer, I think teresko's answer is the most sane. Couple that with writing to flat text and you achieve what you were asking for without the hassle of parsing custom markup. – Kai Qing Oct 10 '12 at 22:33
  • If you're looking for more programming language choice and your templates are lightweight, checkout http://mustache.github.com/mustache.5.html – hakre Oct 10 '12 at 22:38
  • I tend to agree with @KaiQing - writing ones own PHP framework these days duplicates a great deal of work that has been written, and tested, better by someone else. They say, tongue in cheek, that good programmers write software and great programmers reuse it - that's what we should all be aiming for! Also, if you use an off-the-shelf product, you'll make your client projects more maintainable if they go elsewhere - a good selling point if you wish to take on new clients. – halfer Oct 11 '12 at 07:40
  • Lastly - and ignoring the FAQ politely for a moment - if you plan to investigate the above framework recommendations, add Symfony2 to the list. Not used it yet, but the 1.x series was excellent, and the docs make the newer version look well worth trying. – halfer Oct 11 '12 at 07:41

1 Answers1

5

The best option us to use PHP itself for templating, kinda like described in this article.

As for views. I think you are suffering from classical Rails delusion. Views are meant to be classes/objects/instances, which contain presentation logic and are responsible for producing the response to user. It might mean arranging HTML output from multiple templates or just sending a HTTP header as only response. Or maybe some JSON file, or XML.

hakre
  • 193,403
  • 52
  • 435
  • 836
tereško
  • 58,060
  • 25
  • 98
  • 150