<p>I am writing a plugin which has a controller, service and a model. The model represents some fields which will be POST'd and then emailed via the service.</p>

<p>The controller uses an action to populate the model via the $_POST vars, which is exposed to the template for displaying validation errors:</p>

<pre><code>$fields = BusinessLogic_EventBookingFormModel::populateModel($_POST);

if ($fields-&gt;validate()) {
    // send the email...
} else {
    craft()-&gt;urlManager-&gt;setRouteVariables(array(
        'errors' =&gt; $fields-&gt;getErrors()
    ));
}
</code></pre>

<p>I'd like to expose that model to the template before it's populated, I assume via a template variable. What is standard-practise for instantiating and then managing a reference to that model? Where would the instance live? In the service perhaps?</p>

CC BY-SA 3.0
2015-03-23T19:05:40.927Z
"I'd like to expose that model to the template before it's populated" - I'm confused by that. You want to expose the model to the template before you populate it?
Brad Bell
CC BY-SA 3.0
2015-03-24T10:27:24.967Z
Yes. The model is defined but has no data in it.
hamishtaplin
CC BY-SA 3.0
2015-03-24T14:51:48.337Z
Where exactly do you want this to happen?  From your controller? If so, where in the code?
Brad Bell
<plugin-development>
Expose a model to a template?
# Expose a model to a template?

(Asked by: username_0 on 2015-03-23)

I am writing a plugin which has a controller, service and a model. The model represents some fields which will be POST'd and then emailed via the service.
The controller uses an action to populate the model via the $_POST vars, which is exposed to the template for displaying validation errors:
$fields = BusinessLogic_EventBookingFormModel::populateModel($_POST);

if ($fields->validate()) {
    // send the email...
} else {
    craft()->urlManager->setRouteVariables(array(
        'errors' => $fields->getErrors()
    ));
}

I'd like to expose that model to the template before it's populated, I assume via a template variable. What is standard-practise for instantiating and then managing a reference to that model? Where would the instance live? In the service perhaps?

**Tags:** plugin-development
> **Comments:**
> **username_1**: "I'd like to expose that model to the template before it's populated" - I'm confused by that. You want to expose the model to the template before you populate it?  
> **username_0**: Yes. The model is defined but has no data in it.  
> **username_1**: Where exactly do you want this to happen?  From your controller? If so, where in the code?  

---

## 0 answers

