Build Templates in Code?

Jaxel

Well-known member
This is what I've been doing to build my pages:
Code:
return $this->responseView('EWRporta_ViewPublic_Portal', 'EWRporta_Portal', $viewParams);


However, the problem with this is that it doesn't just build the template, it builds the template into a page. What I'm trying to do now is a bit different. I am trying to put a module system in place for my portal mod, and I need to be able to store templates into variables. For instance, lets say I have the template below:
Code:
<xen:if hascontent="true">
<div class="section wikiNav" id="Navigation">
	<div class="secondaryContent">
		<h3><a href="{xen:link 'full:portal/list'}" class="OverlayTrigger">{xen:phrase wiki_navigation}</a></h3>
		<ul>
			<xen:contentcheck>
			<xen:foreach loop="$pageList" value="$page">
				<li>
					{xen:raw $page.page_indent}<a href="{xen:link 'full:portal/{$page.page_slug}'}">{xen:raw $page.page_name}</a>
				</li>
			</xen:foreach>
			</xen:contentcheck>
		</ul>
		<div style="clear: both;"></div>
	</div>
</div>
</xen:if>


I want to build this template, and ONLY this template (with the appropriate $viewParams), and then store the HTML into a variable. This way I can send the variable as a parameter to another template, and then have it built there. Is this at all possible right now?
 
Inside of a custom view class (EWRporta_ViewPublic_Portal), try:

PHP:
$module = $this->createTemplateObject('template_name', $viewParams);

This is not a string, however, but a template that will be converted to a string during the render process. I suppose you could get its string value by casting it to a string, or using $module->render(), but I don't know if there'll be any side effects when not rendering everything all at once.
 
That does work, but I dont think its a good option... I plan on doing this on multiple pages, and I dont want to have to emulate the code 12 times, once for each page. The idea is to reduce redundancy.
 
This is not a string, however, but a template that will be converted to a string during the render process. I suppose you could get its string value by casting it to a string, or using $module->render(), but I don't know if there'll be any side effects when not rendering everything all at once.
Lower performance would be the the only side effect.

That does work, but I dont think its a good option... I plan on doing this on multiple pages, and I dont want to have to emulate the code 12 times, once for each page. The idea is to reduce redundancy.
I'm not sure what you mean by "emulating". Indigo's post is correct. If you explicitly need a template to be rendered separately (because you don't know where it's going), you have to prepare it in the view.
 
Well I am trying to put a module system into the sidebar for my portal. The sidebar is on more than one page. If I have to put code into the view, then that means I need to have something like 12 points of redundancy and reuse the same code over and over.
 
You can just create a function that does all the work for you. Obviously you'll still have to call it in all the necessary views (strictly speaking, there's nothing stopping you from pointing them all at the same view).
 
Is there any way to do this in Models? Here is what I have so far:

EWRporta_ViewPublic_Portal
Code:
<?php

class EWRporta_ViewPublic_Portal extends XenForo_ViewPublic_Base
{
	public function renderHtml()
	{
		$modulesModel = new EWRporta_Model_Modules;
		$this->_params['sidebar'] = $modulesModel->getSidebar();
	}
}

EWRporta_Model_Modules
Code:
<?php

class EWRporta_Model_Modules extends XenForo_Model
{
	public function getSidebar()
	{
		$modules = $this->_getDb()->fetchAll("
			SELECT *
				FROM EWRporta_modules
			WHERE module_order > ?
			ORDER BY module_order ASC
		", 0);

		$sidebar = "";
		foreach ($modules AS $module)
		{
			$sidebar .= $this->createTemplateObject($module['module_template'], array('mod' => $this->getModelFromCache($module['module_name'])->get()));
		}

		return $sidebar;
	}
}

The problem is that I can't use createTemplateObject inside of a model. Is there a better way of doing this?
 
Templating is part of the view layer. It doesn't have any relation to the Model layer. So no, you can't do that in a model.
 
Top Bottom