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

Build Templates in Code?

Discussion in 'XenForo Development Discussions' started by Jaxel, Oct 12, 2010.

  1. Jaxel

    Jaxel Well-Known Member

    This is what I've been doing to build my pages:
    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:
    <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>
    			<xen:foreach loop="$pageList" value="$page">
    					{xen:raw $page.page_indent}<a href="{xen:link 'full:portal/{$page.page_slug}'}">{xen:raw $page.page_name}</a>
    		<div style="clear: both;"></div>

    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?
  2. Indigo

    Indigo Active Member

    Inside of a custom view class (EWRporta_ViewPublic_Portal), try:

    $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.
  3. Jaxel

    Jaxel Well-Known Member

    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.
  4. Mike

    Mike XenForo Developer Staff Member

    Lower performance would be the the only side effect.

    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.
  5. Jaxel

    Jaxel Well-Known Member

    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.
  6. Mike

    Mike XenForo Developer Staff Member

    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).
  7. Jaxel

    Jaxel Well-Known Member

    Is there any way to do this in Models? Here is what I have so far:

    class EWRporta_ViewPublic_Portal extends XenForo_ViewPublic_Base
    	public function renderHtml()
    		$modulesModel = new EWRporta_Model_Modules;
    		$this->_params['sidebar'] = $modulesModel->getSidebar();
    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?
  8. Mike

    Mike XenForo Developer Staff Member

    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.

Share This Page