Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Using logic in templates

edited February 2013 in Design

Is there documentation about the syntax of variables, if statements, etc to use in templates?

I'm trying to tweak the default template that comes with elefantcms so that if the page title is the same as the site name, it doesn't show both in the title. However, this isn't working (still showing both):

<title>{{ conf('General', 'site_name') }} {% if window_title != conf('General', 'site_name') %} - {{ window_title|none }} {% end %} </title>

Comments

  • Here are the pages in the docs on template syntax:

    http://www.elefantcms.com/wiki/Templates http://api.elefantcms.com/visor/lib/Template

    It needs a couple updates, but it's got some info and examples in there. I'm planning to review all the docs soon to update them for Elefant 2.

    The processing for if statements is very simplistic. In this case, it looks like the ( is tripping it up and causing it to not prefix the window_title with $data->. The processing for if statements occurs between lines 627-646 of lib/Template.php.

    Basically, it does this:

    1. Translates $_SERVER.REQUEST_URI to $_SERVER['REQUEST_URI']
    2. Translates i18n.language into $GLOBALS['i18n']->language
    3. If :: and ( are not found, prepend the value with $data->

    In #3, the thinking is that these are either referring to static methods, so you wouldn't want the $data-> prefix, functions that also shouldn't have $data-> prefixed, or consider them complex statements that are essentially PHP code.

    So in this case you'll have to say this instead:

    <title>{{ conf('General', 'site_name') }}
    {% if $data->window_title != conf('General', 'site_name') %} - {{ window_title|none }}{% end %}
    

    Another tip is when you run into trouble with a template, open up the compiled version in the cache/ folder. The compiled tags are very readable PHP :) Here's a comparison in my current default layout with the above change:

    <head>
        <title>{{ conf('General', 'site_name') }}
        {% if $data->window_title != conf('General', 'site_name') %} - {{ window_title|none }}{% end %}</title>
        <meta charset="{{ i18n.charset }}" />
        {% if detect('mobile') %}
            <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
            <link rel="stylesheet" type="text/css" href="/css/mobile.css" />
        {% else %}
            <link rel="stylesheet" type="text/css" href="/css/reset.css" />
            <link rel="stylesheet" type="text/css" href="/css/text.css" />
            <link rel="stylesheet" type="text/css" href="/css/960.css" />
            <link rel="stylesheet" type="text/css" href="/css/style.css" />
        {% end %}
        {! admin/head !}
        {{ head|none }}
    </head>
    

    And compiled:

    <head>
        <title><?php echo Template::sanitize (conf('General', 'site_name'), 'UTF-8'); ?>
        <?php if ($data->window_title != conf('General', 'site_name')) { ?> - <?php echo $data->window_title; ?><?php } ?></title>
        <meta charset="<?php echo Template::sanitize ($GLOBALS['i18n']->charset, 'UTF-8'); ?>" />
        <?php if (detect('mobile')) { ?>
            <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
            <link rel="stylesheet" type="text/css" href="/css/mobile.css" />
        <?php } else { ?>
            <link rel="stylesheet" type="text/css" href="/css/reset.css" />
            <link rel="stylesheet" type="text/css" href="/css/text.css" />
            <link rel="stylesheet" type="text/css" href="/css/960.css" />
            <link rel="stylesheet" type="text/css" href="/css/style.css" />
        <?php } ?>
        <?php echo $this->controller->run ('admin/head', array ()); ?>
        <?php echo $data->head; ?>
    </head>
    
  • Thank you! It works. :) And thanks for all the information.

Sign In or Register to comment.