3

In a plugin is it possible to output the equivalent of {embed="my_template"} if a certain condition is met?

My site index file is as follows at the moment, with php enabled and set to input:

<?php
// Define segment one variable
$seg1 = $this->EE->uri->segment(1);

// If this is the homepage, embed the homepage template
if($seg1 == "") { ?>{embed="embeds/homepage"}<?php 
    } else {

    // IF NOT HOMEPAGE, DO THIS...
    if(substr($seg1, 0, 11) == "property-in") // if first 11 characters = 'property-in'
    {
        ?>{embed="embeds/property-in-town" url_title="<?php $seg1;?>"}<?php
    } else {
        ?>{embed="embeds/town-property" url_title="<?php $seg1;?>"}<?php
    }
}

So what I'd like to do is move this to a plugin and avoid having php turned on.

Cheers

Jim

4 Answers 4

6

You probably don't need to build your own plugin for this. If that logic is all you have to worry about then Switchee and a couple of regular expressions are all you need.

{exp:switchee variable="{segment_1}" parse="inward"}

    {!-- Seg 1 = empty. Embed homepage template. --}
    {case value="''"}
        {embed="embeds/homepage"}
    {/case}

    {!-- Seg 1 starts with 'property-in'. Embed property-in-town template. --}
    {case value="#^property-in#"}
        {embed="embeds/property-in-town" url_title="{segment_1}"}
    {/case}

    {!-- Seg 1 is something else. Embed town-property template. --}
    {case default="yes"}
        {embed="embeds/town-property" url_title="{segment_1}"}
    {/case}

{/exp:switchee}

You probably don't need to pass the url_title embed vars either, as you can always use {segment_1} within the embedded templates anyway.

3
  • Hi Dom Of course! It's obvious that this is the way to do it now that I see it in front of me. Thanks a lot for your help - I've already implemented it on the site and disabled php from the template. I can already see a slight performance boost! Commented Dec 20, 2012 at 12:49
  • 1
    No problem. As a rule of thumb if you have a slightly unusual requirement for template logic Stash and/or Switchee are the answer. I'd hate to build an EE site without them these days.
    – Dom Stubbs
    Commented Dec 20, 2012 at 13:45
  • Couldn't agree more with Dom's comment (above this one)
    – CreateSean
    Commented Dec 20, 2012 at 14:38
3

The Template class allows you to work with template data from inside a plugin, but I don't think it lets you do it in the way you're suggesting.

As far as I know, you can't directly ask a plugin to embed something, because it's working at a different level of the render stack than where you would normally call the {embed} tag. What you can do is use the template class to render HTML, depending on what's inside the plugin tags, but that's as close as you can get, I think.

If I read your code correctly, it seems you're trying to do some kind of URL routing. In which case, I'd hazard a guess you could probably achieve what you want by taking advantage of EE's built in routing. You could make a group called 'property-in', and then use the URL segments such as {segment_1} or {segment_2} to define which URL you want to get to.

for example: /property-in/town

If you really want to use the substring function, you'd be better off installing a plugin like phpstringfun, which allows you to call functions like substr() from inside EE without PHP enabled in your templates. That seems like it would be less overhead for what you seem to want to get done.

2

What I would be tempted to do is to slightly change the logic, keeping the embeds in the template - but using a plugin to do the conditionals:

$seg = $this->EE->uri->segment(1);

if ($seg=='')
{
    return 'home';
}
elseif (substr($seg,0,11)=='property-in')
{
    return 'in-town';
}
else
{
    return 'property';
}

Then just use something like switchee, or even a series of conditionals to display the appropriate embed:

{if "{exp:plugin}"=="home"}
    {embed="embeds/homepage"}
{/if}
{if "{exp:plugin}"=="in-town"}
    {embed="embeds/property-in-town"}
{/if}
{if "{exp:plugin}"=="property"}
    {embed="embeds/town-property"}
{/if}
1
  • Thanks Carl. I've actually created the plugin and got it working. However, since then I've seen Dom's reply and that's just simpler all around so have used his approach instead. Commented Dec 20, 2012 at 12:51
1

If you really want to 'embed' a template from a plugin, you need to create an instance of the Template library to do the heavy lifting.

Add this class at the bottom of your plugin file (outside of the plugin class) or create a different file for it and include it in the plugin file.

if ( ! class_exists('Inline_template'))
{
  if ( ! class_exists('EE_Template'))
  {
    require APPPATH.'libraries/template.php';
  }

  class Inline_template extends EE_Template
  {
    function fetch_template($template_group, $template, $show_default = TRUE, $site_id = '')
    {
        if ($template_group == '!inline')
        {
            return $template;
        }
        else    
        {
            return parent::fetch_template($template_group, $template, $show_default, $site_id);
        }
    }
  }  
}

And then in your plugin code itself:

$this->EE->TMPL->run_template_engine('embeds', 'homepage');
return $this->EE->TMPL->parse_variables_row($this->EE->TMPL->final_template, array(), true);

This should do the trick.

– Wouter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.