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.

EE Variables not working - Am I doing something wrong?

March 25, 2009 3:42pm

Subscribe [4]
  • #1 / Mar 25, 2009 3:42pm

    kevincannon

    22 posts

    I’m trying to use EE variables to set the body class for different pages, but I’m using templates for the header & footer so it doesn’t work as expect. Here’s my code. Any idea why it might not be working?

    index.php - the template file for the homepage

    {assign_variable:my_weblog="default_site"}
    {assign_variable:my_template_group="site"}
    
    
    {assign_variable:body_class="homepage"}
    
    
    {embed="includes/html_header"}
    {embed="includes/content_header"}


    content_header.php

    </head>
    
    <body>
    <div id="wrapper" class="{body_class}">
      <div id="logo">
        <h1><a href="/" title="Back to Sweet Media Homepage">/site_images/sweet_logo.png</a></h1>
      </div>

    However, in the generated code you just see the text {body_class} as is and it hasn’t been parsed.

    I must be doing something wrong.

    Any ideas?

  • #2 / Mar 25, 2009 4:42pm

    Ingmar

    29245 posts

    It’s a matter of scope. Your variables are not automatically available in your sub templates, you actually need to pass them on explicitly, as it were:

    {assign_variable:body_class="homepage"}
    {embed="includes/content_header" body_class="{body_class}"}

    content_header.php

    <div id="wrapper" class="{embed:body_class}">
  • #3 / Mar 25, 2009 4:45pm

    kevincannon

    22 posts

    Ahhh, thanks for the explanation.

    Is there a way to assign global variables, so I don’t have to do that so explicitly.

    Or, can I use PHP variables (or will they suffer from the same scope issue?)

    Thanks!

  • #4 / Mar 25, 2009 4:49pm

    Ingmar

    29245 posts

    I am afraid there is not, and PHP variables would probably suffer the same fate. Although you could actually try globals with PHP, it might even work 😊

  • #5 / Mar 25, 2009 5:10pm

    kevincannon

    22 posts

    Thanks - I’ll go with your first method so. 😊

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

ExpressionEngine News!

#eecms, #events, #releases