6

Is it possible to define some container template that might contain my <html> declaration, then inside it it'll just embed other templates depending on the page? Right now, I'm having to declare my <html> tags in every page, which is not good. I want something like this:

// main template
<html>
    {embed="common/.head"}
    <body>
         {embed="common/.header"}

         // TODO: put some logic here to decide what to embed

         {embed="common/.footer"}
         {embed="common/.scripts"}
    </body>
</html>

8 Answers 8

18

Well, my first question would be, why don't you have that <html> tag in your common/.head embed? Why is it separate?

If you wish to have a template master page, take a look at the following links:

http://johndwells.com/blog/homegrown-plugin-to-create-template-partials-for-expressionengine

http://eeinsider.com/articles/template-partials-using-stash/

As with anything in EE, there are many ways to tackle this, however, i'm confident that after taking a look at those links, you should be able to decide on the right approach for you :)

3
  • 1
    The reason <html> is not part of the .head and the </html> is not part of the .footer or .scripts is that I am trying to impose a coding convention that each template must contain HTML tag pairs -- meaning, don't split them up. From a maintenance perspective, I see this as being easier. Will check out your links. Thanks. Commented Nov 26, 2012 at 0:54
  • In that case, definitely check out the links John gave you and use Stash.
    – Tyssen
    Commented Nov 26, 2012 at 1:11
  • @StackOverflowNewbie Ah right ok, yeah, i used to have two embeds at the bottom, one with the common footer code and then the last one just had </body></html> so that you could insert javascript at the bottom of the page on a per template basis by putting it inbetween {embed="embeds/footer"}and{embed="embed/footer_end"} Commented Nov 26, 2012 at 9:26
3

As you can see there are TONS of different ways to build with ExpressionEngine. I typically setup {embed}s like others said. I shy away from using snippets but I have built sites with snippets for my doc_head, doc_end and the like. You're better off creating a template for each of your pages as it gives you a LOT more flexibility down the road if something changes or you want to do more with a particular page. True it's not a big deal to pull that page out of that particular template, but I try to build for flexibility. IF you build each project with that in mind you'll be in great shape.

2

In my projects over the past few years I've used conditionals more extensively to reduce the number of templates I was using in a project.

For speed of processing, I've done this with a combination of if-else statements and using the plugin Switchee to allow switch/case control structures. I also try to use Snippets, rather than Embeds, because Embeds involve more processing (though there are specific circumstances in which you're almost forced to use Embeds).

Based on the wording of your question, I'm not sure if reducing your templates down to a single common template is what you're after. If you want to do this, in theory it's possible to chop the number down to just a single common template that's loaded up with conditionals. In practice I think it's better to reduce a project to a small, manageable number of templates that do distinct things, or display data distinct ways (e.g. at least one for a listing of a channel's entries, and one for an entry's details).

1

Personally I would shy away from using embeds to store content that you'll need to access across multiple pages (unless you need to pass a variable). The problem with embeds is they are a single database call for each embed template you reference in the page. If you have five embeds, that's five round-trips to the database. Snippets and global variables are loaded with one call at the beginning of every request. If you don't need to pass variables, then you snippets/global variables are ideal.

A solution I've started to use is utilizing SnippetSync which allows me to save snippets and global variables as files. This allows for easy editing and version control without sacrificing performance.

So your main template will look like this:

Header Snippet - site_header.html :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Site</title>
...
</head>
<body>
<!-- end site_header -->

Footer Snippet - site_footer.html :

<!-- start site_footer -->
</body>
</html>

Main Template:

{site_header}
<!--my content -->
 ...
<!--/my content -->
{site_footer}

EE's User Guide has a good run-down on Snippets and how they're different than Global Variables & Embeds.

1
  • Not a good idea to split opening/closing tags in separate templates. Commented Nov 29, 2012 at 22:01
0

Yes, that's entirely possible by using embeds:

Header Template

<html>
    <head>
    ...
    </head>
    <body>
       // header & menu can go here..

Footer Template

       // footer & scripts can go here..
    </body>
</html>

Main Template

{embed="common/header"}
    // content goes here
{embed="common/footer"}
1
  • It's not a good idea to split opening/closing tags in separate templates. Commented Nov 29, 2012 at 21:59
0

Depending on how complicated the content you are still deciding about is, the Pages Module might also do what you want.

0

One of my favortite ways of building templates is to pass variables in the embeds

// main template
<html>
    {embed="common/.head" {exp:channel:entries  limit="1"  rdf="off"} page_title="{title}"  meta-description="{summary}" meta-keywords="{title}"{/exp:channel:entries}}
    <body>
         {embed="common/.header"}

         // TODO: put some logic here to decide what to embed

         {embed="common/.footer"}
         {embed="common/.scripts"}
    </body>
</html>
1
  • That's not hugely efficient though, why not just wrap the page in {exp:channel:entries} and then use snippets?
    – Tom Davies
    Commented Nov 26, 2012 at 14:28
0

Quite frankly all the native approaches feel completely bonkers when you compare them to the elegance of using Stash or any of its predecessors (LG Replace/NSM Transplant/String/MX Jumper).

Use Stash.

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.