0

I can't find a way to define a variable in a EE 2.10.1 Template file. I need to define a variable gv_desc only in some templates.

There are some references but no one works:

{global_var:gv_desc="bla bla"}

{assign_variable:gv_desc="12"}

To use in template:

<meta name='description' content='{if
io_desc}{gv_desc}{if:else}{meta_description}{/if}' />

2 Answers 2

1

There are two ways you can do this within EE, and others that use add-ons that extend EE's capabilities.

The easiest way is probably to define your variable as a global variable - which you do from the Template Manager page. Instructions on how to do this here. It is straightforward - you go to the Template Variable page in the Control Panel, create a new template variable, give it a value (in the Control Panel interface) and then you can access this value in templates by simply requesting the value using a tag.

The second option is to use a Layout Variable - this allows you to create a variable that you can assign a value to during template processing. You define your variable using a simple tag based system

{layout:set name="someName" value="someValue}

or

{layout:set name="someName"}someValue{/layout:set}

You can then access the value elsewhere by simply calling the value using a tag

{layout:someName}

The only complication with layout variables is that they only get created when a template layout is passed on to a second layout template - you define the layout variables in one template, and then can access them in any other template that follows that one in the layout chain. Sounds complicated, but once you get the hang of it it is pretty easy going.

To go down the add-on route, you might want to check out CE-Variables which provides powerful and easy to use variable features, but is EE2 only. There are others, including Stash and Low Variables - but these are typically both more powerful and harder to use in practice (because they do much more than just offer local variables) - though both Stash and Low Variables do work with EE3 and onwards.

HTH

1
  • Thanks, now the varible works with CE-Variables, but I don't know how to use it in the IF: <meta name='description' content='{if exp:ce_vars:gv_desc}{exp:ce_vars:gv_desc}{if:else}{meta_description}{/if}' />
    – P.O.W.
    Dec 12, 2018 at 17:44
0

I used CE-Variables as suggested by @jcogs.eecms to handle variables.

Than I modified the plugin because any method like IF, IS_EMPTY, NOT_EMPTY returns an encoded variable instead the clean content.

public function not_empty_alt()
    {
        $tagdata = $this->EE->TMPL->tagdata;

        $name = $this->EE->TMPL->fetch_param('name');
        $alt  = $this->EE->TMPL->fetch_param('alt');

        if ( ! isset ( $this->EE->config->_global_vars[$name] ) )
        {
            return $alt; // $this->EE->TMPL->no_results();
        }

        $temp = $this->EE->config->_global_vars[$name];

        return empty( $temp ) ? $alt : $temp;
    }

In the template file:

To output gv_desc var:

{exp:ce_vars:set gv_desc="Lorem Ipsum"}

{exp:ce_vars:not_empty_io_alt name="gv_desc" alt="Nope"} 

To output the alternative:

{exp:ce_vars:set gv_desc=""} <!-- or nothing -->

{exp:ce_vars:not_empty_io_alt name="gv_desc" alt="Nope"} 
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.