Using a variable inside an included file from function - php

I'd like to have a header.php file throughout my site. I currently have the following:
header.php
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="<?php if(isset($depth)){echo $depth;};?>css/style.css">
functions.php
function include_layout_template($template="", $depth="")
{
global $depth;
include(SITE_ROOT.DS.'public'.DS.'layouts'.DS.$template);
}
index.php
<?php include_layout_template('header.php', "../"); ?>
But $depth dissappears, I can't even echo $depth; its just blank. How can I get the depth variable for use in header.php?

You have to rename depth variable in function call
function include_layout_template($template="", $my_depth="")
{
global $depth;
//if need $depth = $mydepth

Your $depth variable is disappearing, because it is passed as parameter first but then defined to use the global parameter.
I will explain with an example:
$global = "../../"; //the variable outside
function include_layout_template($template="", $depth="")
{
global $depth; //This will NEVER be the parameter passed to the function
include(SITE_ROOT.DS.'public'.DS.'layouts'.DS.$template);
}
include_layout_template("header.php", "../");
To solve, simply modify the function parameter other than the depth itself.
function include_layout_template($template="", $cDepth="") { }

Related

Pass variable to _preprocess_node function for use in node template

I've been racking my brains over this one but I'll do my best to describe the problem as best as possible. I have a custom function written within template.php, with a bunch of conditionals. When a condition is true, I would like to assign a value to a variable, and then pass that variable intro a node preprocess function that allows that variables to be rendered on a node template.
The function containing the condition:
function _mytheme_date_repeat_string($vars) {
$exdate_pos = strpos($rrule['WKST'], 'EXDATE:');
if($exdate_pos > 0) {
$vars['testvar'] = 'abc123';
}
}
The preprocess function that I would like to render the variable in for node template use:
function mytheme_preprocess_node(&$vars, $hook) {
$vars['new_variable'] = $testvar;
}
Intended usage in node.tpl.php:
<?php print $new_variable; ?>
I'm not great with PHP, but I know enough about programming to know that variable scope might be an issue here. What would be the best way to implement this? Any guidance is greatly appreciated.
Thanks,
Mark.
If it is not called, your _mytheme_date_repeat_string() function will never be executed. Preprocess functions (ie. any function starting mytheme_preprocess_, are called automatically by Drupal's theme system.
What you need is either move the code of _mytheme_date_repeat_string() in mytheme_preprocess_node() or refactor it and call it.
function _mytheme_date_repeat_string($rrule) {
$exdate_pos = strpos($rrule['WKST'], 'EXDATE:');
if($exdate_pos > 0) {
return 'abc123';
}
else {
return NULL;
}
}
/**
* Prepares variables for node templates.
*/
function mytheme_preprocess_node(&$variables, $hook) {
// Get $rrule from somewhere
$rrule = ... ;
$testvar = _mytheme_date_repeat_string($rrule);
if ($testvar) {
$variables['new_variable'] = $testvar;
}
}
You code does not show where the $rrule calue comes from. I assume you would get it for $variables['node'].

Call variable correct and display PHP generated HTML in the_content()

I’m creating a plugin called woomps. In the main php file i do a calculation when template redirect.
add_action('template_redirect','woomps_loop');
This calculates x: (it’s actually a big calculation).
function woomps_loop() {
$x = 10;
Return $x
}
I want to show this variable based on a shortcode. Therefore, in my main php file I include the frontend.php through this code.
function woomps_scripts() {
include 'frontend.php';
}
add_action('wp_enqueue_scripts','woomps_scripts');
Where the frontend.php has this code
function woomps_subscription_slider (){
//How do I call the variable from woomps_loop() without running all the code again.
echo "<div>\n";
echo "<p>\n";
echo $x;
echo "</p>\n";
echo "</div>\n";
}
add_shortcode("woomps-subscription-slider", "woomps_subscription_slider");
The shortcode is added to one of my pages so it displays, but……
How do I call the $x variable form woomps_loop() without running the code again?
This <div> will come before all other content generated in the_content(). Why is that?
global $x; //Had to define it global before setting it, i dident understand this before #cameronjonesweb exmample.
$x = $total_qty;
Then reference it like this:
function woomps_subscription_slider ($x){
global $x_subs;
$content = <<<EOD
<div>
<p>
{$x_subs}
</p>
</div>
EOD;
return $content;
} //end woomps_subscription_slider
It worked. Try this EOD to return multiple lines of HTML, and it does get placed inside the_content() loop
Okay. Thank you!
1) Store it as a global variable. So change your woomps_loop function to
global $x;
function woomps_loop() {
global $x;
$x = 10;
return $x
}
(PS you probably won't need to return $x here now)
Then also define $x in your shortcode function
function woomps_subscription_slider (){
global $x;
echo "<div>\n";
//... rest of it carries on
2) Because you're echoing. Return the value instead.

PHP Vars From Included Bootstrap Not Showing Up in View

I have created my own little PHP framework for fun, however, I am having trouble passing variables from bootstrap to the views....
if I put an echo,print_r,var_dump my target variable in the bootstrap, the output is displayed in the browser before the tag... yet the target var in bootstrap.php is not available in the view, it is coming up as "" even though at the top of the page it is being output correctly....
Somethings I noticed from similar questions:
- The target variable is not being over written
- The include target path is correct and the file exists
- The file is only being included one time (include_once is only fired once)
Any ideas are greatly appreciated, I am pulling my hair out over here lol...
Source Code
https://gist.github.com/jeffreyroberts/f330ad4a164adda221aa
If you just want to display your site name, I think you can use a constant like that :
define('SITE_NAME', "Jeff's Site");
And then display it in your index.tpl :
<?php echo SITE_NAME; ?>
Or, you can send your variables to the view by extending a little bit your JLR_Core_Views :
class JLR_Core_Views
{
private $data;
public function loadView($templatePath, $data = array())
{
$this->data = $data;
$templatePath = JLR_ROOT . '/webroot/' . $templateName . '.tpl';
if(file_exists($templatePath)) {
// Yes, I know about the vuln here, this is just an example;
ob_start();
include_once $templatePath;
return ob_get_clean();
}
}
function __get($name)
{
return (isset($this->data[$name]))
? $this->data[$name]
: null;
}
}
Then, you can call your template like that :
$view = new JLR_Core_Views();
$view->loadView("index", array("sitename" => "Jeff's Site"));
And here is your index.tpl :
<?php echo $this->siteName; ?>
Below is another example of what you can do.
First, you create this class in order to store all the variables you want :
<?php
class JLR_Repository {
private static $data = array();
public function set($name, $value) {
self::$data[$name] = $value;
}
public function get($name) {
return (isset(self::$data[$name]))
? self::$data[$name]
: null;
}
}
?>
Then, when you want to store something in it :
JLR_Repository::set("sitename", "Jeff's Site");
And in your index.tpl :
<?php echo JLR_Repository::get("sitename"); ?>
try using the 'global' keyword - http://php.net/manual/en/language.variables.scope.php

Codeigniter vars inside a view functions

I've a function inside a view (I've nested code so there is no alternative).
My problem is made because i want to add some vars inside the function.
I can't access to the var inside the function.
<div>
<?php _list($data); ?>
</div>
<?php
echo $pre; // Perfect, it works
function _list($data) {
global $pre;
foreach ($data as $row) {
echo $pre." ".$row['title']; // output ' title' without $pre var
if (isset($row['childrens']) && is_array($row['childrens'])) _list($row['childrens']);
}
}
?>
Simple... just define the function like this:
function _list($data, $pre=NULL)
then inside the function, you could check if the $pre is NULL then search for it somewhere else... using the global statement in functions is not desirable.
On the other hand you can define('pre',$pre); and use the pre constant created in your function... again not desirable but it would work for your example.
Later edit: DEFINE YOUR FUNCTIONS IN HELPERS
i am not sure why i forgot to suggest that in the first place
define function in view is weird. using global variable make it worse.
maybe you should avoid the global function with this:
<div>
<?php
foreach($data as $row){
_list($pre, $row);
}
?>
</div>
<?php
function _list($pre, $row) {
echo $pre." ".$row['title'];
if (isset($row['childrens']) && is_array($row['childrens'])){
foreach($row['childrens'] as $child){
_list($pre, $child);
}
}
}
?>
btw, define function in helpers would be better
http://ellislab.com/codeigniter/user-guide/general/helpers.html
whsh they help

PHP Function Call Returns Fatal Error

For some reason my PHP function call <? r(); ?> returns a fatal error. Any help?
<?php
//$r is previously assigned
function r() {
echo ( $r );
};
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="<? r(); ?>rs/css/master.css">
</head>
Defining a function changes the scope, where $r won't be inside that function. Try sending in $r with the function in this manner:
<?php
function r($r) {
echo ( $r );
}
?>
<link type="text/css" rel="stylesheet" href="<?php r($r); ?>rs/css/master.css">
or defining global $r; at the beginning of the function (not preferred).
Also, you shouldn't use <? to open PHP. If all the function does is echo the value of $r, it would make more sense to just do this:
<link type="text/css" rel="stylesheet" href="<?php echo ( $r ); ?>rs/css/master.css">
If you want to refer to a global object from inside an object, you need to explicitly declare it as global:
function r() {
global $r;
echo ( $r );
}
But in general, referring to global variables in this way is bad style. You should consider passing in prerequisites as function arguments:
function r($r) {
echo ( $r );
}
...
r($r);

Categories