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

Render a template in Deferred

#1
I have a Deferred, i want render template alert_post_like (html) and send to a API
I try:
PHP:
$template = new XenForo_Template_Public('alert_post_like');
$template->setParam('user', $user);
$template->setParam('content', $content);
$html = $template->render();
var_dump($html);
But $html is empty.

@Chris D Please help me solve this problem, thankyou very much !!!
 

tenants

Well-known member
#3
Just going from what the core does, it does it by extending the XenForo_ViewRenderer_Abstract and using createTemplateObject
(see code below, from XenForo_ViewRenderer_HtmlPublic)

Code:
        $templateName = (!empty($params['containerTemplate']) ? $params['containerTemplate'] : 'PAGE_CONTAINER');
        $template = $this->createTemplateObject($templateName, $params);

        if ($contents instanceof XenForo_Template_Abstract)
        {
            $contents = $contents->render();
        }

        $template->setParams($this->_dependencies->getExtraContainerData());
        $template->setParam('contents', $contents);

        $rendered = $template->render();
        return $rendered;
it doesn't create a new XenForo_Template_Public (the only time I've seen it do that is cssInternal.php), here it passes the params in at object creation time (I don't think that is necessary, but worth a try)
cssInternal (when creating a new object):

Code:
$templates[$cssTemplate] = new XenForo_Template_Public(substr($cssTemplate, strlen('public:')), $templateParams);
I haven't actually figured out what is wrong with your code yet, but I know looking at core examples always helps me out when I'm stuck
 
Last edited:
#4
Just going from what the core does, it does it by extending the XenForo_ViewRenderer_Abstract and using createTemplateObject
(see code below)

Code:
        $templateName = (!empty($params['containerTemplate']) ? $params['containerTemplate'] : 'PAGE_CONTAINER');
        $template = $this->createTemplateObject($templateName, $params);

        if ($contents instanceof XenForo_Template_Abstract)
        {
            $contents = $contents->render();
        }

        $template->setParams($this->_dependencies->getExtraContainerData());
        $template->setParam('contents', $contents);

        $rendered = $template->render();
        return $rendered;
it doesn't create a new XenForo_Template_Public (the only time I've seen it do that is cssInternal.php), here it passes the params in at object creation time (I don't think that is necessary, but worth a try)
cssInternal (when creating a new object):

Code:
$templates[$cssTemplate] = new XenForo_Template_Public(substr($cssTemplate, strlen('public:')), $templateParams);
I haven't actually figured out what is wrong with your code yet, but I know looking at core examples always helps me out when I'm stuck
Thank @tenants, but i not understand how use it in a Deferred :(
 

tenants

Well-known member
#5
Why do you want to do it in Deferred, I'm trying to dig information out of the core to see when deferred it actually run

How are you testing this, are you going to the deferred link, does you code some how extend deferred
if you just

var_export('test') ;

in your code, does this show up for the page you are looking at?
 
#6
Why do you want to do it in Deferred, I'm trying to dig information out of the core to see when deferred it actually run
I have a Addon, when create new thread, comment or like a post it will create new deferred, this deferred send content HTML alert of that action to API.
My English is not good, sorry because you can not understand what me say...
 

tenants

Well-known member
#7
I understand you, your English is very good
deffered.php it's self isn't actually part of xenforo class constrruct (has no classname), what is the name of the class you have written and what is it you are actually extending

does

var_export('test') ;

even show up when you go to the link you are testing it with
what is the link you are testing it with

mysite/deferred.php ???

Sorry, I haven't done work with deferred before, but I can usually figure things out (as long as it's not regex :) )
 
Last edited:
#8
deffered.php it's self isn't actually part of xenforo class constrruct, what is the name of the class you have written and what is it you are actually extending

does

var_export('test') ;

even show up when you go to the link you are testing it with
what is the link you are testing it with

mysite/defferred.php ???
I have class MyAddon_Model_Alert and function alertUser
I create a deferred in that function

PHP:
XenForo_Application::defer('MyAddon_Deferred', $alertData, $hash_key, 0, XenForo_Application::$time);
Model Alert can call in Deferred, think so can render a template in Model -> call it in Deferred ^^
 

tenants

Well-known member
#10
These deferred jobs are not actually necessarily run by you, they may be run by the next person/page, so trying to render anything there (if it were possible), doesn't seem to make sense.

I'm not sure you really want to be trying to render anything in deferred (where would the api even look, at deferred.php ?)
 
#11
These deferred jobs are not actually necessarily run by you, they may be run by the next person/page, so trying to render anything there (if it were possible), doesn't seem to make sense.

I'm not sure you really want to be trying to render anything in deferred (where would the api even look, at deferred.php ?)
I think deferred jobs is protected, it run follow each queue saved in database, so every body can do it is safe way.
I used deferred to when creating new thread not delayed by API
I think so. Do you have ideas? :( I'm congested :((
 
#13
Its working!
But do not know it's performance :(
PHP:
$dependencies = new XenForo_Dependencies_Public();
$dependencies->preRenderView();
$template = $dependencies->createTemplateObject('my_template');
var_dump($template->render());