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.

Dynamic Header

June 17, 2010 6:37pm

Subscribe [2]
  • #1 / Jun 17, 2010 6:37pm

    twhed

    6 posts

    I’m looking to convert parts of an existing site that’s currently built with PHP and Smarty to a CMS. So, I’m checking out EE to see if it fits my needs.

    For those that know Smarty, in the PHP files we currently do something like this:

    $styles[] = 'css1.css';
    $styles[] = 'css2.css';
    $doc->assign('styles', $styles);
    $scripts[] = 'script1.js';
    $scripts[] = 'script2.js';
    $doc->assign('scripts', $scripts);
    $doc->display('some_page.tpl');

    Within some_page.tpl, I’ll call header.tpl, and in <head> section it’ll step through those 2 css files and link to them, and then the 2 js files and link to them.

    So far the only solution I’ve come up with in EE is this:

    in header template:

    <html>
    <head>
    <?php global $add_styles; ?>
    {embed="styles/base_css" add="<?php echo $add_styles; ?>"}
    {embed="scripts/base_js"}
    </head>
    <body>

    in page template:

    <?php
    global $add_styles;
    $add_styles = "external/blog|external/index|external/alt_terms";
    ?>
    {embed="site/header"}

    in base_css:

    {if "{embed:add}" != ""}
    <?php
    $splitcontents = explode('|', '{embed:add}');
    foreach($splitcontents as $file)
    {
        echo '<link href="/css/'.$file.'.css?v={site_version}" rel="stylesheet" type="text/css" />'."\n";
    }
    ?>
    {/if}

    This is dirty and makes me feel dirty!

    Is there a better way to accomplish this without a bunch of php, ideally something built into EE that I can set in the page template and step through in header or base_css?  We’ll be keeping our css and js files in svn repo as we’re not moving everything over to the CMS and want them easily accessible by the rest of the site.

    Thanks for any help!

    -Tommy

  • #2 / Jun 18, 2010 1:33pm

    lebisol

    2234 posts

    You can use segment conditions to detect URL and then just embed the css file of choice.
    eg.

    <a href="http://www.example.com/index.php/books">http://www.example.com/index.php/books</a>
    {if segment_1 =="books"}
    embed css_v1…do…print header
    {/if}
    {if segment_1=="homes}
    embed css_v2…do…print header
    {/if}
  • #3 / Jun 18, 2010 6:31pm

    twhed

    6 posts

    You can use segment conditions to detect URL and then just embed the css file of choice.
    eg.

    <a href="http://www.example.com/index.php/books">http://www.example.com/index.php/books</a>
    {if segment_1 =="books"}
    embed css_v1…do…print header
    {/if}
    {if segment_1=="homes}
    embed css_v2…do…print header
    {/if}

    Also dirty but not as bad as having to put PHP into my HTML files.

    Is there no way to define a variable/array in a parent file and access it in a child file using EE code?  That’s essentially what I’m trying to do.  Seems like something EE should be able to do but I just haven’t come across it in the documentation yet.

  • #4 / Jun 18, 2010 7:51pm

    lebisol

    2234 posts

    It is not really dirty as it is a legit EE condition. 😊
    I have not see array variables per say but there is an extension for better handling of global variables that EE does have.
    But still sooner or later you will have to use some logic of “if this then that” to call for those variables or simply hardcode them into template…similarly how you would embed a snip of code.
    Nothing wrong with

    {if segment_1!="blog"}
    {embed="site_css"}
    {if:else}
    {embed="blog_css"}
    {/if}

    Check out some of those links on Devot-ee article that will lead to some docs especially parse order of embeds.
    Hope this helps…

  • #5 / Jun 18, 2010 9:05pm

    twhed

    6 posts

    Oh sorry, didn’t mean that conditionals are dirty, just meant that putting those conditionals there I’d consider dirty =P Say I have 10 pages, each using a different css or js file, rather than having a bunch of conditionals in the header file, I’d rather keep the page specific data in each page.  It’ll keep the header file clean and since they’re associated with the pages in the end, I’d want to keep them there.

    Say I’d want to include jquery on only some pages, I’d definitely use a conditional in the header then (and we currently do just that, though even then we set a var to include jquery in the php file and then the header tpl checks if that var is true… so still need to be able to set a var in parent and check in child, ideally without using PHP).

    The conditionals work but I’d still like a better solution.  I’ve been looking at CMSes for a couple days now and think EE’s going to be my choice, so really want to figure out how to do this well.  I’ll take a look at those links you gave me, thanks 😊

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

ExpressionEngine News!

#eecms, #events, #releases