T3 Community Discussion & Support

Let's build the best free Joomla template framework, together!
  1. JamesOfJoomla
  2. General Discussion
  3. Thursday, January 15 2015, 06:30 PM
  4.  Subscribe via email
I have a lot of backend PHP code that I am using on a template I defined, so I want to hide away most of it in a block. But when I do this, the variables I declared and assigned in the block are not accessible in the template file, even though the template file is properly calling in the block.

As as an example, let's say I have a very basic template file being used, home1.php. home1.php includes this code:
<?php $this->loadBlock ('setup') ?>


In the blocks folder, I have a file called setup.php. In this file, let's say I just have some very simple PHP code, like
$x = 5;


But then in home1.php, if I do this:
echo $x;


It won't work, because home1.php doesn't have access to the variable $x from setup.php. The actual code is much more complex of course, but if I can figure out how to access PHP variables in blocks from the templates that call them, that would be very helpful. Or, maybe there is another way I should be doing this? I just don't like having 300 lines of PHP code at the beginning of my template file, but I don't see a way around it unless I can put it in a block. Do I just need to use the PHP require function to call an outside PHP file or something?

Thank you for any help.
Comment
There are no comments made yet.
Accepted Answer
JamesOfJoomla Accepted Answer Pending Moderation
0
Votes
Undo
You can try using "sourcerer" component. With this you only need to put "{source} here your code {/source}" in any article (or empty module), and the code is executed. I use that to insert PHP, Javascript or HTML custom codes anywhere.


Thanks darkaht. I use the sourcerer component already, and it is indeed great, but I was hoping for a way to have the code in actual PHP files. Sourcerer stores PHP code in the Joomla database as parts of modules or articles, which is not what I was really going for. Maybe I'm trying to turn T3 into something that it's not.

I was just able to use the PHP require function to accomplish what I was needing. It works fine, I was just wondering if there was a more "proper" and orthodox method of doing this in T3.

So I basically just put all the PHP setup code I need in a folder I put here: t3_blank/tpls/includes and put the php file setup.php in there and then in my template I simply have the line of code:
<?php require('includes/setup.php'); ?>


It seems to work just fine. Thanks!
Comment
There are no comments made yet.
  1. more than a month ago
  2. General Discussion
  3. # Permalink
darkaht Accepted Answer Pending Moderation
0
Votes
Undo
You can try using "sourcerer" component. With this you only need to put "{source} here your code {/source}" in any article (or empty module), and the code is executed. I use that to insert PHP, Javascript or HTML custom codes anywhere.
Comment
There are no comments made yet.
  1. more than a month ago
  2. General Discussion
  3. # 1
Gardner Luna Accepted Answer Pending Moderation
0
Votes
Undo
Thanks Darkaht for quick answer.

@James, please try and let me if it works or not. If not, I will have closer check for you.

Gardner.
Comment
There are no comments made yet.
  1. more than a month ago
  2. General Discussion
  3. # 2
  • Page :
  • 1


There are no replies made for this post yet.
However, you are not allowed to reply to this post.