<p>So I've been playing around with this and got stuck a few times, but I think I have a solution that could work for you.</p>

<p>I think the problem is actually because <code>craft()-&gt;templates-&gt;render()</code> simply just gives you the compiled template back, it doesn't try and render it in the browser. So your plugins <code>init</code> method is being hit, everything is working just fine, then it just goes past it and just keeps going until the end.</p>

<p>There is a controller method for rendering a template which is called like <code>$this-&gt;renderTemplate</code> which according to the docs:</p>

<blockquote>
  <p>This is a shortcut for TemplatesService::render(), but rather than returning the rendered template, it will output it to the browser and end the request. It will also include any CSS and JS that is queued up via includeCssFile(), includeJsFile(), et al., and it will set the appropriate MIME type header based on the template file’s extension.</p>
</blockquote>

<p><a href="http://buildwithcraft.com/classreference/controllers/BaseController#renderTemplate-detail" rel="nofollow">You can see the method from the Class reference here</a></p>

<p>Now I don't think you can or should try and call controller methods from within your plugin's <code>init</code>, so I think you need to do something inspired by this method i.e just echo the content and end the request.</p>

<p>So this is what I have used and it worked for me just fine, it might need some tweaking and more inspiration from the <code>renderTemplate</code> method, but it works as a start.</p>

<pre><code>if (craft()-&gt;request-&gt;isSiteRequest()) {

  // Get your entry however you wish
  $entry = craft()-&gt;entries-&gt;getEntryById(264);

  // Render the template with $entry and set it to output
  $output = craft()-&gt;templates-&gt;render('arena', ["entry" =&gt; $entry]);

  // ** From renderTemplate **

  // Output it into a buffer, in case TasksService wants to close the connection prematurely
  ob_start();

  echo $output;

  // End the request
  craft()-&gt;end();
}
</code></pre>

<p>From this i got a rendered template in the browser, output the entries title using <code>{{ entry.title }}</code> and also all the <code>{% extends %}</code> <code>{% block %}</code> tags and everything else seemed to work.</p>

<p>If there is a better solution then i'm all ears, but this is what I could come up with from looking around the source/docs/stackexchange.</p>

<p>Hope that helps :)</p>

CC BY-SA 3.0
2015-07-29T15:31:34.893Z
hacky but it's one way to do it. In the end we decided to go around domains. Thanks for help!
user2733233
CC BY-SA 3.0
2015-07-16T08:57:21.980Z
2015-07-16T08:57:21.980Z
Alec Ritson
<p>I need to serve different home pages (structure entries) depending on the subdomain. For example:</p>

<pre><code>foo.example.com -&gt; this will render an entry with slug "foo" 
bar.example.com -&gt; this will render an entry with slug "bar"
</code></pre>

<p>So far I created a plugin and fetched the proper entry based on the subdomain:</p>

<pre><code>class LocationsPlugin extends BasePlugin
{
    (...)
    public function init()
    {
        if (craft()-&gt;request-&gt;isSiteRequest()) {
            (...)
            // at this point $entry is the entry I want to render 
            // the problem is the following doesn't work:
            craft()-&gt;templates-&gt;render("subdomains/_entry", ["entry" =&gt; $entry]);
        }
    }
}
</code></pre>

<p>I get the layout rednered but without the content. Twig's blocks aren't rendered at all and I suspect init() hook is called before some initializations required by render().</p>

<p>The question: How can I, having $entry in init(), just render it instead of the main page?</p>

<p>Thanks!
Jan</p>

CC BY-SA 3.0
2015-07-15T12:34:45.437Z
Wouldn't it be easier to select the correct entry in the template instead? Something like

`{% if siteUrl == 'foo.example-com' %}{%set entry = craft.entries.section('my-section').slug('foo') %}{% endif %}`
megatrond
CC BY-SA 3.0
2015-07-15T13:05:03.727Z
@megatrond I thought about this but I plan doing some additional steps in the init() hook that can't be made easily in Twig.
user2733233
<routing><frontend>
Custom subdomain routing with a pluign
# Custom subdomain routing with a pluign

(Asked by: username_0 on 2015-07-15)

I need to serve different home pages (structure entries) depending on the subdomain. For example:
foo.example.com -> this will render an entry with slug "foo" 
bar.example.com -> this will render an entry with slug "bar"

So far I created a plugin and fetched the proper entry based on the subdomain:
class LocationsPlugin extends BasePlugin
{
    (...)
    public function init()
    {
        if (craft()->request->isSiteRequest()) {
            (...)
            // at this point $entry is the entry I want to render 
            // the problem is the following doesn't work:
            craft()->templates->render("subdomains/_entry", ["entry" => $entry]);
        }
    }
}

I get the layout rednered but without the content. Twig's blocks aren't rendered at all and I suspect init() hook is called before some initializations required by render().
The question: How can I, having $entry in init(), just render it instead of the main page?
Thanks!
Jan

**Tags:** routing, frontend

> **Comments:**
> **username_1**: Wouldn't it be easier to select the correct entry in the template instead? Something like  `{% if siteUrl == 'foo.example-com' %}{%set entry = craft.entries.section('my-section').slug('foo') %}{% endif %}`  
> **username_0**: @username_1 I thought about this but I plan doing some additional steps in the init() hook that can't be made easily in Twig.  

---

## 1 answers

### Answer by username_2 on 2015-07-16. Score: 3 | Accepted answer

So I've been playing around with this and got stuck a few times, but I think I have a solution that could work for you.
I think the problem is actually because craft()->templates->render() simply just gives you the compiled template back, it doesn't try and render it in the browser. So your plugins init method is being hit, everything is working just fine, then it just goes past it and just keeps going until the end.
There is a controller method for rendering a template which is called like $this->renderTemplate which according to the docs:

This is a shortcut for TemplatesService::render(), but rather than returning the rendered template, it will output it to the browser and end the request. It will also include any CSS and JS that is queued up via includeCssFile(), includeJsFile(), et al., and it will set the appropriate MIME type header based on the template file’s extension.

You can see the method from the Class reference here
Now I don't think you can or should try and call controller methods from within your plugin's init, so I think you need to do something inspired by this method i.e just echo the content and end the request.
So this is what I have used and it worked for me just fine, it might need some tweaking and more inspiration from the renderTemplate method, but it works as a start.
if (craft()->request->isSiteRequest()) {

  // Get your entry however you wish
  $entry = craft()->entries->getEntryById(264);

  // Render the template with $entry and set it to output
  $output = craft()->templates->render('arena', ["entry" => $entry]);

  // ** From renderTemplate **

  // Output it into a buffer, in case TasksService wants to close the connection prematurely
  ob_start();

  echo $output;

  // End the request
  craft()->end();
}

From this i got a rendered template in the browser, output the entries title using {{ entry.title }} and also all the {% extends %} {% block %} tags and everything else seemed to work.
If there is a better solution then i'm all ears, but this is what I could come up with from looking around the source/docs/stackexchange.
Hope that helps :)

> **Comments:**
> **username_0**: hacky but it's one way to do it. In the end we decided to go around domains. Thanks for help!  

---

