Get custom var inside templates

Permalink
Hi,

I would like to know how i can get custom vars inside templates (pages type / blocks)

For example, actually i include an element in a page type. In this included element, i have set a var ($myVar = 'hello';)

But in page type $myVar is not reconized...

So, i have set in included file :
$this->controller->set('myVar', 'Hello');

and get var in page type :

$this->controller->getvar('myVar');


Same problem to passe var in blocks views/templates.

Have a solution for that (global vars or other) ?

Thanks

orange73
 
ScottC replied on at Permalink Reply
ScottC
these need to be implicitly set from a controller

I have no idea what you are trying to accomplish.

but if you are doing $this->controller->set that means you are trying to pass a variable => value to the controller only to read it right back.

other places to define variables are in a package's on_start method, in config.php (for common defined values) or in a package's controller(via on_start there) or in a block's on_start method or view method.

It seems by virtue of your question that you want to include as in something like an element, so in that case assuming say_hi is an element you'd do:
Loader::element('say_hi',array('myVar'=>'hello')); //and assuming your element had print $myVar it'd show up assuming that element was off of ~siteroot/elements/say_hi.php
orange73 replied on at Permalink Reply
orange73
Imagine 2 files :
- test.php
- global.php

In global.php, i includes test.php with
$this->inc('elements/test.php');


In test.php i define a var
$myVar = 'Hello';


In global.php :
echo $myVar;

not working !
ijessup replied on at Permalink Reply
ijessup
Sounds like a scope issue.

PHP Variable Scope:http://php.net/manual/en/language.variables.scope.php...
jordanlev replied on at Permalink Reply
jordanlev
Hi orange73,
ijessup is correct about why this doesn't work. And I agree with Scott that there is probably a better way to achieve whatever it is you're trying to do. Concrete5 uses the MVC pattern, which states that you should not generally set variables in your views, but rather in controllers. You will probably be better off setting variables from your controller, then the view file and any elements you include will have access to it.

If you can provide more details about what you're trying to do exactly, we can probably be of more help.