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

Template Hooks question

Discussion in 'XenForo Development Discussions' started by Superfluous, Jan 19, 2015.

  1. Superfluous

    Superfluous Member

    I'm just starting to try get my head around developing add ons. I've created an Event Listener which hooks a new template onto the existing 'message' template. In this existing template it inherits the variable $post from a loop in the parent 'thread_view' template (structure is 'thread_view' -> 'post' -> 'message').

    Whenever I append my new template via my PHP file the $post variable is no longer accessible (presumably '$template->create' processes the template separately from the template I'm trying to hook on to). Whenever I manually include the new template in the 'message' template it works fine.

    What am I doing wrong? :whistle:
     
  2. Lawrence

    Lawrence Well-Known Member

    Template hooks will be deprecated in the future. I haven't used them since that was announced. What you should use is a template modification to include your custom template in the message template. This way the $message variable will be available to your template.

    For example:
    Create a new template modification, and type in message for the template name, after the template loads, set:
    Search Type: Simple replacement
    Find: {xen:raw $messageAfterTemplate}
    Replace: $0
    <xen:include template="name_of_your_template" />

    The $0 (zero), adds what you found back into the replacement data. Your custom template will show above the likes summary template. You can place your template elsewhere by finding another area to look for, maybe: <xen:if is="{$message.last_edit_date}">
     
    Xon likes this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    The template hook callback receives $template object which has access to the template that called the hook. With that in mind, it also has access to that template's params.

    It's been a while... But something like this should work:
    PHP:
    return $template->create('your_template'$template->getParams());
    As stated above, template hooks are deprecated. However, I do feel they are a good introduction to getting messy with the XF code. I wouldn't suggest building an add-on for release using template hooks, but as a way of experimenting with listeners and other concepts, certainly worth doing.

    But yeah, you will find template modifications much easier to work with.
     
  4. Superfluous

    Superfluous Member

    Thank you both. I have just achieved what I needed with the Template Modifications (didn't even know they existed!). I'm coming from a C# development background so I just have to get my head around the structure of Xenforo, and the syntax of PHP itself...

    One further question, in the guide which I learned from it taught me how to access 'Options' from the PHP file. Is it possible to access all my 'Options' and related variables from within templates directly - if so is there a post on here with decent examples?

    I'm thinking that I might be able to do away with PHP files if so, as all the logic I need is already present in XF conditionals.
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    Options can be accessed like this in templates:

    Code:
    {$xenOptions.boardUrl}
    Where boardUrl is your option ID.
     
    Superfluous likes this.
  6. Superfluous

    Superfluous Member

    Sorry to change topic, I don't want to go spamming threads everywhere while I keep asking silly questions.

    I'm not sure if it's possible, but I'd like a conditional way to disable 'includeTitleInUrls' option, but only for particular users who are logged in. I'm building an 'Add On' for a work safe mode, where I try to disguise the content employees are looking at while in the office (many of my users complain that my site is blocked in work, or they are afraid to browse it etc) - and having the thread titles in the URLs may give content away to any administrator browsing URL history. Any idea if it's doable?
     
  7. Milano

    Milano Well-Known Member

    Not sure if this works but you can try visitor_setup Event Listener

    PHP:
    public static function visitorSetup(XenForo_Visitor &$visitor)
    {
        if (
    $visitor->get('user_id')) // User is logged in
        
    {        
            if (...) 
    // your conditions
            
    {
                
    XenForo_Application::getOptions()->set('includeTitleInUrls'false);
            }
        }
    }
     
    Superfluous likes this.
  8. Superfluous

    Superfluous Member

    That worked, thank you very much (y)
     
  9. Superfluous

    Superfluous Member

    Ok, here goes another one... I've spent hours trying to figure this out and I can't. I am currently redirecting users who are not logged in to a fake page which hides the fact that logged in members are using a forum - when using Fast-CGI as my PHP module header('Location: blah.php') works fine for the redirect. When using Apache it doesn't work however. I only noticed because my test server and production server were using different modules. I'm assuming that it is sending some headers before I am calling the header redirect (with Apache being more sensitive than Fast-CGI) - and after testing in blank PHP files it's something specific to Xenforo, or more likely my implementation of it. Anyone know of a cleaner method of server side redirection to a URL which is not native to Xenforo, or any other suggestions? (it's a PHP file hosted in the Xenforo root directory, I can embed Xenforo architecture if needs be).
     

Share This Page