XF 2.3 Can I call template contents from a controller?

mjda

Well-known member
I'm trying to auto-create a new thread when I add a new entity in my add-on. However, I can't use a phrase for the thread message because some variables may not exist so I need to be able to use <xf:if /> code. So, I was thinking how about using a template, such as something like this:

PHP:
protected function getThreadMessage()
    {
        $category = $this->category;
        $entity = $this->entity;
        
        $message = \XF::someWayToGetTemplateContents($entity);
        
        return $message;
    }

The question is, is this even possible? If so, how?

...or, is there a better way to do what I'm wanting to do?
 
Solution
I'd still strongly recommend using phrases for any prose, though you can technically render any template anywhere:

PHP:
$contents = \XF::app()->templater()->renderTemplate('public:some_template', $params);
However, I can't use a phrase for the thread message because some variables may not exist so I need to be able to use <xf:if /> code.
You should be able to just use regular PHP if-statements. It's technically possible to render a template but but that shouldn't be necessary here.
 
You should be able to just use regular PHP if-statements. It's technically possible to render a template but but that shouldn't be necessary here.

I was trying to avoid hard-coding any of the language. For example, the message will say "{$username} created a new entity in {$category}". Then after that it will go through checking if certain parts are there and, if so, add a line that says "Some title: Content\nSome other title: More content", etc...

If I do that in PHP, there's no way folks could change the wording later on without altering the code. I suppose I could just create a new phrase for each line and only include it if needed. Maybe I'll try that. Sorry, just thinking in text here...

Thanks!
 
I'd still strongly recommend using phrases for any prose, though you can technically render any template anywhere:

PHP:
$contents = \XF::app()->templater()->renderTemplate('public:some_template', $params);
 
Solution
I'd still strongly recommend using phrases for any prose, though you can technically render any template anywhere:

PHP:
$contents = \XF::app()->templater()->renderTemplate('public:some_template', $params);

Yeah, I think I'm going to just take the approach of just calling multiple phrases, where needed. I just didn't think of doing that before. Maybe I've been sitting here too long today. 🤦‍♂️
 
Back
Top Bottom