$this->inc() or include() in Themes keeping variables global

Permalink
Hi there I am looking to make some themes for concrete5
with some user options in the back-end in order for this to work I need some variables to be global throughout my theme I can do this by keeping adding them to the header and including this with the php include function

As this doesn't follow concrete5 recommendation to use inc() I was wondering if they was anther way of achieving this ?

and if not will this affect my theme being approved or not ?

thanks a lot

GB

Gingebean
View Replies: View Best Answer
bpbuild replied on at Permalink Reply
Could you elaborate on what you mean by variables? What is it you are trying to maintain across the site?
bpbuild replied on at Permalink Reply
Also, unless you are submitting your theme to be used by the community, as long as you have the basic requirements of a theme established, you do the approving.
mkly replied on at Permalink Reply
mkly
Hello,
I'm curious why you need some globals everywhere in your theme, but you can pass anything you want with $this->inc etc.

For example if you have a theme and you are doing
$this->inc('elements/header.php');

You can pass in values with
$this->inc('elements/header.php', array(
  'foo' => $foo,
  'bar' => $bar
));

Those key value pairs will get extracted into available variables in the elements/header.php file

Best Wishes,
Mike
smcstewart replied on at Permalink Reply
smcstewart
Great work, perfect thanks!
bbeng89 replied on at Permalink Reply
bbeng89
I'm not sure if this will meet your needs exactly, but one thing you can do is create page type controllers where you could define your variables. So for instance, if you had a page type left_sidebar.php you would create a page_types folder in your root controllers folder (/controllers/page_types) then you would create a php file left_sidebar.php in the new page_types directory (/controllers/page_types/left_sidebar.php). In this file you can define your controller and in the view() function set variables that will be available in your theme. For example:

<?php
class LeftSidebarPageTypeController extends Controller{
   public function view(){
      $this->set('testVariable', 'Value for testVariable');
   }
}


Then in your view you can access the $testVariable variable, like so:

<?php echo $testVariable; ?>


For more info on page type controllers you can check outhttp://www.concrete5.org/documentation/developers/pages/mvc-approac...
Gingebean replied on at Permalink Reply
Gingebean
Hi thanks for answers guys,
yes bpbuild I plan on submitting themes to marketplace I am still at the early stages so I want to be sure the method I am using will be accepted

For an example I will have default content to guide user how to add blocks in the right places this will added in my theme and hard coded .if the variable $hideThemeGuides is true then these will be hidden

I will have other options that can be changed in the backend too and will effect whole theme
thanks mike for your answer I have already explored this avenue but as Jordan said here

http://www.concrete5.org/community/forums/customizing_c5/variables-...

"Note that with Concrete5's $this->inc() function, there are two separate things you need to worry about here: one is sending variables TO an element, and another is getting variables back OUT of the element. As @Mainio suggests, you can pass variables TO an element by sending them as an array argument in the 2nd argument of the $this->inc() function. *BUT* concrete5 provides no way that I know of to get variables OUT of an element -- so you need to use php's "include" or "require" instead of $this->inc()"

As you mentioned the variables can be sent to header and footer etc but I can "get" them from my other page types
with a simple include I can have a few variables accessible through out my theme.
thanks bbeng89 your way will probably work to but I will need a controller for every page type( i have quite a few) all setting the same variables and and I would have to send the vars to the footer and header inc

The include function seams to be simplest and most easy to maintain
unless I and I probably am missing something

What exactly are the objections for using this

thanks once again for you comments

GB
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
For $hideThemeGuides and other configuration 'global' information, you can either set/get the configuration values via the config table or set them as global constant define() in a package on_start handler.

See

http://www.concrete5.org/documentation/how-tos/developers/use-the-c...


PS.
Use of include() will not get approved in an addon.
JohntheFish replied on at Permalink Reply
JohntheFish
To get info in and out of an element, you can pass an object and manipulate object properties. However, doing that would suggest you shouldn't be using an element. Elements should generally be used as write only display components.

Consider Libraries, Models or Helpers if you need to interact to/from a component.
Gingebean replied on at Permalink Reply
Gingebean
Thanks a lot JohntheFish I will adapt my code to use Config();
I think it the best suited solution

but Just out of curiosity why would the include() not get approved

thanks again
JohntheFish replied on at Permalink Reply
JohntheFish
The concrete5 methods for linking to and including files protects namespaces, allow overrides (to various degrees), and adjusts contexts for other things like the base of css and script files.

In many cases include() would work, especially if it is just you using it from one place. Until some minor factor you never considered jumps out and bites you.

There is a howto by Goutnet listing everything rejected by the automated checks in the PRB. The list is not definitive. In general, if there is a c5 helper or class for doing something, you need to document a very good reason for doing it differently.