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

Pass Xenforo rendered template html to a custom View class

Sadik B

Well-known member
#1
Hi

I have an API which creates PDF files from html. The way the API class works is that it has a function which accepts html code and outputs the pdf from the html. So far so good.

Now I want to use this API in xenforo. Under normal circumstances, in the controller of my addon I pass my viewparams to a template which I create in the Master Style. Now what I want is that I pass my viewparams to the template which has the html I need and then pass the rendered html (after all the template conditionals etc are processed) to my API class function so that I can get the pdf output.

So any pointers about how I may go about this...

Thanks...
 

Jake Bunce

XenForo moderator
Staff member
#2
I think this might be helpful:

http://xenforo.com/community/threads/return-content-without-pagecontainer.33902/

The 'raw' response type displays only what is returned by your view class. No container is used. This allows you total control over your output. And you can send custom headers as need by calling $this->_response->setHeader() inside of your view. See this view class as a code example for a raw render with headers:

XenForo_ViewPublic_Attachment_View

So basically set a raw response type in your route or controller and do all the rendering and headers in your view.
 

Sadik B

Well-known member
#3
I think this might be helpful:

http://xenforo.com/community/threads/return-content-without-pagecontainer.33902/

The 'raw' response type displays only what is returned by your view class. No container is used. This allows you total control over your output. And you can send custom headers as need by calling $this->_response->setHeader() inside of your view. See this view class as a code example for a raw render with headers:

XenForo_ViewPublic_Attachment_View

So basically set a raw response type in your route or controller and do all the rendering and headers in your view.

Thanks Jake.

I discovered after posting this thread that I might be able to use renderRaw()

However I am confused about one thing. Is it a good idea, writing html within the renderRaw method of the View class? My original understanding was that html under all circumstances should remain within templates. Hence I was trying to create a template with the html, render it and pass it to my API class. Also is there any way possible to get rendered template output within PHP? either in the controller or in the View class?
 

Jake Bunce

XenForo moderator
Staff member
#4
Do it in your view class:

Code:
<?php

class YourAddon_ViewPublic_You extends XenForo_ViewPublic_Base
{
	public function renderRaw()
	{
		// RENDER TEMPLATE WITH PARAMS PASSED FROM CONTROLLER
		$html = $this->createTemplateObject($this->_templateName, $this->_params)->render();

		// NOW YOU HAVE THE RENDERED CONTENT TEMPLATE IN $html
		// YOU CAN THEN OPERATE ON THAT WITH YOUR PDF FUNCTION
		$pdf = Your_Class::pdf($html);

		// SET APPROPRIATE HEADERS FOR PDF
		$this->_response->setHeader('Content-Type', 'application/pdf', true);

		// AND RETURN YOUR PDF DATA
		return $pdf;
	}
}
That's the idea. Just be sure to set the "raw" response type in your route or controller as shown in this thread:

http://xenforo.com/community/threads/return-content-without-pagecontainer.33902/