TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
View Context - None or Controller?
  • I've recently moved over to the 3.x branch of Kohana from 2.3.X - and i'm very pleased so far - much nicer to work with the zend like naming conventions etc.

    This isn't really a problem or issue, but I was wondering peoples thoughts on the matter, anyway here's the 'issue'.

    In the previous Kohana branch (and other frameworks) views were executed in the context of the controller instance which created them, allowing you to use the controllers methods inside your views.

    With the new branch the views are executed in an empty context, meaning you have to pass through everything you reqire. As I said this isn't really a problem and very easy to get around - I was just trying to think of any advantages of this way forward, and peoples thoughts on the matter.
  • Views should be dumb. In fact many are arguing that views should not have any accessible scope whatsoever and certainly shouldn't be able to invoke PHP functions/methods. I agree with this principle, it is finding an elegant solution that is always the challenge. Mustache seems a very good compromise.
  • ouch that looks like a terrible solution to me and certainly nothing i'd ever use.

    Why on earth we'd want to start parsing text based views and inserting content when we're using a templating language made for this kind of thing?
    PHP is a templating language surely this is one of it's strengths - actually using PHP variables and control structures etc inside the templates/views - this is why we have the alternative syntax after all?

    I agree in keeping your business/control logic out of any views/templates, but having text only based views and using regex/string replacement just absolute madness to me, especially when php is perfect for templating, saving on any re-parsing overhead too :)
  • PHP is a templating language surely this is one of it's strengths

    But it's not. PHP templating is actually rather poor.

    actually using PHP variables and control structures etc inside the templates/views - this is why we have the alternative syntax after all?

    Except it makes your templates completely un-reusable. Ever want to use your views from a jquery ajax call returning json, instead of returning enourmous html strings from your ajax calls?

    I agree in keeping your business/control logic out of any views/templates

    This is where view classes come into play. Views should have logic in them (view logic doesn't belong in the controller), just not in the template file.

    but having text only based views and using regex/string replacement just absolute madness to me, especially when php is perfect for templating, saving on any re-parsing overhead too :)

    Except pure php views provide no xss filtering, no ability to extend views and no (or very poor) code reuse.

    If your specific beef was with mustache specifically, at least consider looking at my Kohana-View project, which uses the mustache style class/template structure, but adapts to use php templates. It still gives you auto escaping for xss protection and class based views, but gives you php templates for those that think php templating is better (imo it's absolutely not).

    btw, I used to think "well php is a templating language, why have something else" too. I still think that way for things like smarty, but mustache is a special case.

  • But it's not. PHP templating is actually rather poor.

    For example, I develop quite a lot using Magento, the mixture of PHP/Html in views is better than using some text replacement system to replace {{heading}} with a line of text when you can just use a variable and be done with it. Ending up with a file full of text and no PHP, and then using PHP to parse the text just seems a little backward to me that's all :)

    This is where view classes come into play. Views should have logic in them (view logic doesn't belong in the controller), just not in the template file.

    I usually end up extending view classes this way, the only thing I was saying I don't agree with is trying to shift all PHP out of the view files and replace it with some crazy string replacements. PHP is excellent for mixing with HTML in view files IMO.

    And using eval with string replacements inside the view class seems nasty aswell - using eval on potentially hundreds of variables accross many views is hacky to me ;)

    An example from the mustache docs:

    • {{default_tags}} {{=<% %>=}}
    • <% erb_style_tags %> <%={{ }}=%>
    • {{ default_tags_again }}

    Parsing something this in PHP seems totally absurd to me when you could achieve the same goal with the same amount of code using mixed PHP/HTML, passing values in to the view etc. I really don't see any benifit of this approach what so ever, it reduces readability and will certainly have an additional performance overhead. :)

  • I generally disagree with additional parsing of PHP within the view. My point is that the view should just be a placeholder for pre-defined variables. I share the view that many templating languages implemented in PHP are additional overhead. But, I feel there is a happy medium which can deliver the correct separation without a significant performance hit. Caching also makes the performance argument null and void.

    However, I think mixing PHP and HTML is very dirty and it's about time the MVC separation is actually obeyed rather than bastardised.
  • I really don't see any benifit of this approach what so ever, it reduces readability and will certainly have an additional performance overhead. :)

    Right, that's what Kohana-View is for. For people like you. :)

    You still get to use <?=?> (it's real php, except auto-escaped for xss [you can even turn that off if you want to]), and you can still bastardize your template files with view logic if you really want to.

    The whole point is: View logic should live in a class, and all your template should be doing is echo, foreach, and if(isset())

    The specific implinetation of this is a moot point. I prefer mustache because it forces me to put my logic in the class. I can't mess it up.

  • The whole point is: View logic should live in a class, and all your template should be doing is echo, foreach, and if(isset())

    I agree with this just, but surely that's all you need to do in view files anyway? therefore there shouldn't be a need for any added templating system on top? My views only every have foreach, issets, echo, maybe the odd useage of a helper class in a simple echo

    It's good to hear other peoples point of views on these matters, thanks fellas.

  • My views only every have foreach, issets, echo, maybe the odd useage of a helper class in a simple echo

    Except if you aren't using class based views, your view logic is in the wrong place (probably the controller) ;)

  • Posted By: zomborExcept it makes your templates completely un-reusable. Ever want to use your views from a jquery ajax call returning json, instead of returning enourmous html strings from your ajax calls?


    Hi, zombor. You previously emphasized on the reusability of templates. But for your example, how is a mere HTML + PHP placeholders template reused for xml or json? Can you explain it?



    I generally agree to keep view logic away from template for the sake of readability and maintainability, while I'm still not sure how to strictly follow that priciple under these situation:


    • I want to use some helpers to do small conversion stuff. is it a bit overkill to move them all to view class?
    • I need branching to control whether to display some small snippets, use if or move them to view class?
    • I need to render complex tables, or forms, or other complex stuff, and tracing their html outputs makes me finally lost in the inheritance and abstraction of view classes. How can I do?

    How do you think of them?

  • Hi, zombor. You previously emphasized on the reusability of templates. But for your example, how is a mere HTML + PHP placeholders template reused for xml or json? Can you explain it?

    Well, say you have a html view of a news feed. You want to do a RSS version of it. All the variables are the same, including the logic to create the variables. You can have a View_News_RSS class that extends your View_News class, and simply changes the template in View_News_RSS, but uses the same variables that are inherited from View_News.

    I want to use some helpers to do small conversion stuff. is it a bit overkill to move them all to view class?

    The rule is to put every kind of non echo in the class. This includes helper calls.

    I need branching to control whether to display some small snippets, use if or move them to view class?

    It's OK to do things like if (isset()) or if (empty()). This is done frequently in mustache to control whats shown in the template.

    I need to render complex tables, or forms, or other complex stuff, and tracing their html outputs makes me finally lost in the inheritance and abstraction of view classes. How can I do?

    Here's a simple example of rending zebra stripes, it may help:

    http://github.com/zombor/KOstache/blob/master/classes/view/striping.php#L14 http://github.com/zombor/KOstache/blob/master/templates/striping.mustache#L13

    If you have a more concrete example of what you would exactly do in your template, I can help "move" it to the class. It sometimes takes a different way of thinking to do it.

  • Posted By: zomborWell, say you have a html view of a news feed. You want to do a RSS version of it. All the variables are the same, including the logic to create the variables. You can have a View_News_RSS class that extends your View_News class, and simply changes the template in View_News_RSS, but uses the same variables that are inherited from View_News.

    Understood. It's the reuse of view logic, not template.

  • I wasn't saying have load of logic inside a template, but are you telling me that:

    <?php foreach($blas as $bla): ?> Something <?php endofreach ?>

    is including more logic than (what ever the syntax is):

    {{#bla}}something{{/bla}}

    It is the same but with an added layer of micky mouse regex/str replace parser added on top.

    If you add lots of PHP code into your templates, indeed you are a fool. This should be in a different layer I agree that (not controller, neither the template - a view class would do nicely).

    But simple regexing PHP to replace PHP tags with something is a joke. You even most liley lose your auto complete / highlight / indent while writing your HTML inside the template too.

  • but are you telling me that: <?php foreach($blas as $bla): ?> Something <?php endofreach ?> is including more logic than (what ever the syntax is): {{#bla}}something{{/bla}}

    I don't think anyone said that.