SitePoint Sponsor

User Tag List

Page 5 of 5 FirstFirst 12345
Results 101 to 106 of 106
  1. #101
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dan7
    Yes, but I'm not sure how to do it properly. Considering, the template examples above, html rendering looks like this:
    PHP Code:
        protected function render($template) {
            
    ob_start();
            include(
    $template);
            return 
    ob_get_clean();
        } 
    So, considering that my json template consists only of $response = array(...);, then how would rendering look like, something like this:?
    PHP Code:
        protected function renderJSON($template) {
            include(
    $template);
            return 
    json_encode($response);
        } 
    This way I'm sticked to some name convention. Have you got a better idea?
    I wouldn't change the render() function of the controller. Just keep it as is - then make a template (view) for your ajax response:
    PHP Code:
    <?php
    // file : json.tpl.php
    return Array (
        Array (
            
    'href'=>'/1',
            
    'title'=>'1',
            
    'value'=>'1',
        ),
    );
    ?>
    Quote Originally Posted by dan7
    Also, this way, if I want to add something to the view, I'm forced to update both templates. It would be probably easier, if I have a links array predefined and iterated over in the templates, instead of hardcoding.
    You're mixing concepts now - The templates are the views. You could deal with the problem by putting your data in a model component. This is where data belongs in a full MVC separation.

    Quote Originally Posted by dan7
    What if such links are dependent on some permissions? So, user can see links he is allowed to.
    That's a whole topic in itself. You can make a model that is aware of user permissions. Or you can use the controller as facade for the model and make the filtering in there.

  2. #102
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the answers. Yeah, I thought about those links in the model.

    I think I didn't understand you about json and concatenation. You can concatenate json encoded strings, but they need to be sent as a one object. So I was talking about this issue (1 object/request) and thought you wanted to send more ('glued as html'). But that's a whole different story, not important in this topic.

    Thanks a lot for help!

  3. #103
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     protected function render($template) {
            
    ob_start();
            include(
    $template);
            return 
    ob_get_clean();
        } 
    Quote Originally Posted by kyberfabrikken
    I wouldn't change the render() function of the controller. Just keep it as is - then make a template (view) for your ajax response:
    PHP Code:
    <?php
    // file : json.tpl.php
    return Array (
        Array (
            
    'href'=>'/1',
            
    'title'=>'1',
            
    'value'=>'1',
        ),
    );
    ?>
    But it won't work with output buffering, will it? The render function would have to "return include($template);" to make it work as you suggest above. Or am I missing something??

    Thanks

  4. #104
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I believe it would. But I still think it would be better to stick with one response type (string).

  5. #105
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Actually I believe it would. But I still think it would be better to stick with one response type (string).
    Well, it does not work, for me at least.
    If I consider string as a response from each child controller, than I would have to concatenate those strings into 1 object and as they are already encoded, I would have to attach additional parent object, that would hold this concatenated string. That's because json data should be sent as 1 object. So, that's why I wanted to encode data once at the end and avoid any further manipulation of data. I will have to think about it a bit more.

  6. #106
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    The solution I'm using in konstrukt is a bit different. I basically exploit exceptions to break the normal execution-stack. If a deep controller wants to prevent outer rendering, it'll create an instance of k_http_Response, assign contents to it and throw it (k_http_Response extends Exception). The top level dispatcher then catches it and calls it's out() method. Purists would call it exception-abuse, and they'd be right too, None the less, it works very well, and I haven't really encountered any backsides to it yet. The important issue for me, is that the scope of each controller is contained, and since exceptions can be caught, that holds true.
    I wanted to ask something about it. It's generally interesting idea. I know how to handle redirects this way, but I don't know how to handle those breaks (considering my current design)
    How do you handle grabbing the content from the childcontrollers after you threw the exception? You write, that you pass the content to k_http_Response, but if you throw this exception from childcontroller, how do you know anything about the rest of the content from already executed childcontrollers?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •