ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Template madness - am I crazy for doing this?

August 12, 2008 9:06am

Subscribe [2]
  • #1 / Aug 12, 2008 9:06am

    yehosef

    17 posts

    The last few weeks I’ve been playing with EE to get the hang of it. One of the things that I noticed that I didn’t like was copying html tags across different templates. I realize that the easy way around this is to have a header and footer subtemplate that I include, but for a few reasons I wanted a different approach. 

    I came up with something using hidden templates and it seems a little unconventional. I’m getting ready to start heavy development and I don’t want to be painting myself into a corner with my approach. The main problem I see with the approach might be the overhead of including a few extra templates.  I would appreciate any feedback.

    first - I have an “inc” template group with two templates .index and .main_template ( I don’t use the index - its just a “no access” page - I probably could use index instead of .index - this is just the way I did it.)

    inc/.index looks like:

    <?
    global $IN;
    
    if (isset($IN->SEGS[1])&&$IN->SEGS[1]) {
      $seg1 = $IN->SEGS[1];
      $seg2 = (isset($IN->SEGS[2])&&$IN->SEGS[2]) ? $IN->SEGS[2] : 'index';
    } else {
      $seg1 = 'site';
      $seg2 = 'index';
    }
    
    $template = $seg1.'/.'.$seg2;
    echo '{embed="inc/.main_template" my_template ="'.$template.'"}';
    ?>

    Basically this is a “router” to send stuff where I want - this is a very rough first draft.  A next addition is allow me to have simple url redirection like url=”/about_us.html”  would display the entry url_title=“about_us” in a static_content blog.  I know there is a pages modules - but it’s not working the way I wanted - this gives me full control.

    next is inc/.main_template

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html >
    
    <head>
    <title>my title.. needs work</title>
    <meta http-equiv="Content-Type" content="text/html; charset={charset}" />
    
    <link rel="stylesheet" href="/css/my.css" media="screen" />
    [removed][removed] <!-- required -->
    </head>
    <body>
    <div id="mycoolheader">Mysite</div>
    
    <div id="mainbody">
    <!-- the magic -->
    {embed="{embed:my_template}"}
    </div>
    
    </body>
    </html>


    Then I create my normal template groups - eg site.

    site/index is

    {embed="inc/.index"}

    and I have a inc/.index

    <div>this is the home page</div>
    
    {embed="inc/.search_box"}

    and an inc/.other

    here is some other content

    and now if I access mysite.com/site/index - I get my “home page” or if I go to /site/other
    I get the page with the other content.

    That’s it. 

    The nice thing is that it lets me make new templates with no effort and have them all look the same.  With a little tweaking, the “router” (inc/.index) could be made to allow you to choose other layouts besides .main_template

    Does this seems like a good or bad idea?

  • #2 / Aug 12, 2008 9:57am

    Boyink!

    5011 posts

    It’s a familiar concept - but (if I’m understanding it completely) could be done with all native EE code using segment variables and conditionals.

    So an index template along the lines of:

    {if segment_2==""}
        {embed="embeds/.main_template"}
    {if:else}
        {embed="embeds/.other_template}
    {/if}

    The index template serves as router - and could be extended as need be, looking for specific values in the proper segments and loading the correct template.

  • #3 / Aug 12, 2008 10:05am

    yehosef

    17 posts

    My first cut of the inc/.index page, very similar to what you have, was done in pure EE tags.  In the end I decided that it would be simpler, probably faster, and certainly more powerful to do it in php.

  • #4 / Aug 12, 2008 10:14am

    Boyink!

    5011 posts

    Certainly if you’re that familiar with PHP - then carry on…;)

    I favor EE code because I’m not a PHP progammer.

    Looking back at your template code - the one other approach that might be worthwhile is further use of embed variables with embedded templates.

    For example—I always have one “html_header” template that contains my doctype and stylesheet links.  But it also includes the page title which needs to change, so I use embed variables to send the template the page title I want for a specific use.

    You can stack embedded templates - so you could, in your main template, pull out the header HTML and embed that as well.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases