1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trouble Understanding Custom Template-Controller Relationship

Discussion in 'XenForo Development Discussions' started by DeltaHF, Apr 14, 2014.

  1. DeltaHF

    DeltaHF Well-Known Member

    I'm creating a custom search page for my Resource Manager. I have built a custom controller to fetch the results, and currently outputs them using the resource_featured template, like so:
    PHP:
    return $this->responseView('XenResource_ViewPublic_Resource_Featured''resource_featured'$viewParams);
    I want to customize that output using my own template, but I don't understand exactly what this responseView() method is doing. There is no file located at /library/XenResource/ViewPublic/Resource/Featured.php as I had expected from the first parameter there.

    While experimenting, I have also tried copy-pasting the contents of resource_featured into my own new custom template and just swapping out the names for the second parameter, but none of the dynamic elements in the template appear to be processed or output, which makes it clear to me I don't really understand what is going on here.

    What does responseView() do, exactly? How can I wire up my own custom template to work with my new controller?
     
    Last edited: Apr 15, 2014
  2. Brogan

    Brogan XenForo Moderator Staff Member

    The first parameter is the view class and doesn't actually have to exist.
     
    sheel likes this.
  3. Adrian Schneider

    Adrian Schneider Active Member

    I really don't understand the choice for allowing a class not to exist, as I had the same WTF moment when I started writing public controllers.

    It seems the rule of thumb is to write it in as if you are going to use a custom one. Then, at some point in the future when you want to add one, you don't need to change your code.

    Just add a new class, and define a renderHtml() method that manipulates $this->_params for whatever view logic you want to to enhance.
     
    DeltaHF likes this.
  4. Jeremy P

    Jeremy P Well-Known Member

    I think the choice was probably made to reduce boilerplate so you don't have to write empty view classes, and as you say it makes it easier should you want to add one in the future.
     
  5. DeltaHF

    DeltaHF Well-Known Member

    Hmm... OK. Some extensive documentation sure would be handy right now. I still feel pretty lost.

    So what view class is rendering the "Featured Resources" page now?
     
  6. Adrian Schneider

    Adrian Schneider Active Member

    It seems that writing

    Code:
    $this->render('template_name', $params);
    is much less boilerplate code, and much more clear than having to (being able to) specify something that doesn't exist.

    Anyway.

    Your template is just being rendered as-is. All the custom view classes do is let you modify the $params without having crazy templates, or lots of template-related logic added to your controller. It's nice for adding secondary data, or doing some last minute transformations that may be a little too advanced for template helpers.

    The important thing is that it's optional.
     
    DeltaHF likes this.

Share This Page