Forum

Thread tagged as: Question, Problem, Discussion

Perch Global Var

Hi All

Is there a way to set a global variable using PHP that can then be used in any template...

what i want to do is use <perch:if exists="" /> to display tab or accorian on mobile

the site has been built and full with content so its too late to change form perch_content to perch_content_custom

Any ideas?

Anthony Elleray

Anthony Elleray 2 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Is there a way to set a global variable using PHP that can then be used in any template...

Yes, use PerchSystem::set_var()

the site has been built and full with content so its too late to change form perch_content to perch_content_custom

Then the templates have already been evaluated and compiled to HTML, so no variables at runtime will affect them.

Hi Drew

Issue is when i do this

PerchSystem::set_var('device', $device);
perch_content('content');

the variable does not appear when i use <perch:showall />

but i cant use perch_content_custom as the client needs to choose from 3 templates that all use blocks

Drew McLellan

Drew McLellan 2638 points
Perch Support

the variable does not appear when i use <perch:showall />

As I said, the templates have already been evaluated and compiled to HTML, so no variables at runtime will affect them.

but i cant use perch_content_custom as the client needs to choose from 3 templates that all use blocks

I'm not sure I follow. perch_content_custom() and perch_content() will both render blocks.

So to make the variable appear i need to use perch_content_custom()

but to use perch_content_custom() i then need perch_content_create()

issue i then have is a template needs to be specified when using perch_content_create() the user cant choose

Drew McLellan

Drew McLellan 2638 points
Perch Support

issue i then have is a template needs to be specified when using perch_content_create() the user can't choose

That's no the case. Or at least, that's no supposed to be the case.

You can replace this:

perch_content('content');

with this:

perch_content_create('content')
perch_content_custom('content', []);

Hi Drew

i just tried that so i have the following

require_once ($_SERVER['DOCUMENT_ROOT'].'/perch/functions/mobile_detect/mobile_detect.php'); 

$detect = new Mobile_Detect;
// Any mobile device (phones or tablets).

if ( $detect->isMobile() ) {
    $device = "mobile";
} else {
    $device = "desktop";
}


PerchSystem::set_var('device', $device);
perch_content_create('content');

perch_layout('global/header');

perch_content_custom('content', []);

echo '<pre>';
print_r(PerchSystem::get_vars());
echo '</pre>';


perch_layout('global/footer'); 

But when i create a new page there is no region

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is this Perch or Runway?

runway

Drew McLellan

Drew McLellan 2638 points
Perch Support

If you turn on debug and load the new page, what does it output?

Time    Δ   Debug Message - Perch Runway 3.0.14
0.002   0   [55] SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate, pr.routeOrder, s.settingValue AS siteOffline FROM perch3_pages p LEFT JOIN perch3_page_routes pr ON p.pageID=pr.pageID LEFT JOIN perch3_settings s ON s.settingID='siteOffline' UNION SELECT NULL AS pagePath, pr2.routePattern, pr2.routeRegExp, pr2.templatePath AS pageTemplate, pr2.routeOrder, NULL AS siteOffline FROM perch3_page_routes pr2 WHERE templateID!=0 ORDER BY routeOrder ASC, pagePath ASC
0.0087  0.0067  Matched page: /contact2222, so not using routes.
0.0089  0.0002  Using master page: /templates/pages/default.php
0.0097  0.0009  [1] SELECT * FROM perch3_members_sessions WHERE sessionID='7b03e3ae1f4566a22590ab6ffdf42ac76181397b' AND sessionHttpFootprint='3c19797de22e4f42db10a5014920d480b7a5110e' AND sessionExpires>'2018-03-28 08:21:31' LIMIT 1
0.0109  0.0012  User is logged in
0.0113  0.0003  [1] SELECT * FROM perch3_pages WHERE pagePath='/contact2222' LIMIT 1
0.0131  0.0019  [1] SELECT * FROM perch3_shop_cart WHERE cartID=2933
0.0138  0.0007  [1] SELECT * FROM perch3_shop_cart WHERE cartID=2933
0.0144  0.0005  [nil] SELECT * FROM perch3_shop_sales WHERE saleFrom<='2018-03-28 08:21:00' AND saleTo>'2018-03-28 08:21:00' AND saleActive=1 AND saleDeleted IS NULL ORDER BY saleOrder ASC
0.0151  0.0008  [43] SELECT DISTINCT settingID, settingValue FROM perch3_settings WHERE userID=0
0.0381  0.023   Using template: /templates/pages/attributes/default.html
0.0385  0.0003  Using sub-template: /templates/pages/attributes/seo.html
0.0386  0.0001  Using sub-template: /templates/pages/attributes/body_attributes.html
0.0386  0.0001  Using sub-template: /templates/pages/attributes/facebook.html
0.0387  0.0001  Using sub-template: /templates/pages/attributes/twitter.html
0.0408  0.0021  Using template: /templates/pages/attributes/body_attributes.html
0.0411  0.0003  Using template: /templates/pages/attributes/body_attributes.html
0.0414  0.0003  Using template: /templates/pages/attributes/body_attributes.html
0.0417  0.0003  [56] SELECT * FROM perch3_pages WHERE pageNew=0 AND pageHidden=0 ORDER BY pageTreePosition ASC
0.0438  0.002   [1] SELECT pageTreePosition FROM perch3_pages WHERE pagePath='/contact2222' LIMIT 1
0.0444  0.0006  [1] SELECT pageID FROM perch3_pages WHERE pageTreePosition IN ('000-020', '000') ORDER BY pageTreePosition DESC
0.0454  0.001   Using template: /templates/navigation/mobile-level-1.html
0.0455  0.0001  Using template: /templates/navigation/mobile-level-2.html
0.0455  0.0001  [8] Using template: /templates/navigation/mobile-level-3.html
0.0471  0.0016  [3] Using template: /templates/navigation/mobile-level-3.html
0.0477  0.0006  [3] Using template: /templates/navigation/mobile-level-3.html
0.0492  0.0015  [7] Using template: /templates/navigation/mobile-level-2.html
0.0508  0.0015  Using template: /templates/navigation/mobile-level-2.html
0.0508  0.0001  [2] Using template: /templates/navigation/mobile-level-3.html
0.0517  0.0009  [1] Using template: /templates/navigation/mobile-level-2.html
0.0521  0.0004  Using template: /templates/navigation/mobile-level-2.html
0.0522  0.0001  [2] Using template: /templates/navigation/mobile-level-3.html
0.053   0.0008  [1] Using template: /templates/navigation/mobile-level-2.html
0.0533  0.0004  [19] Using template: /templates/navigation/mobile-level-2.html
0.0621  0.0088  [56] SELECT * FROM perch3_pages WHERE pageNew=0 AND pageHidden=0 ORDER BY pageTreePosition ASC
0.0642  0.002   [1] SELECT pageTreePosition FROM perch3_pages WHERE pagePath='/contact2222' LIMIT 1
0.0648  0.0006  [1] SELECT pageID FROM perch3_pages WHERE pageTreePosition IN ('000-020', '000') ORDER BY pageTreePosition DESC
0.0656  0.0008  Using template: /templates/navigation/main-menu-level-1.html
0.0657  0.0001  Using template: /templates/navigation/main-menu-level-2.html
0.0657  0.0001  [8] Using template: /templates/navigation/main-menu-level-3.html
0.0674  0.0016  [3] Using template: /templates/navigation/main-menu-level-3.html
0.068   0.0006  [3] Using template: /templates/navigation/main-menu-level-3.html
0.0696  0.0016  [7] Using template: /templates/navigation/main-menu-level-2.html
0.0715  0.0019  Using template: /templates/navigation/main-menu-level-2.html
0.0715  0.0001  [2] Using template: /templates/navigation/main-menu-level-3.html
0.0726  0.0011  [1] Using template: /templates/navigation/main-menu-level-2.html
0.073   0.0004  Using template: /templates/navigation/main-menu-level-2.html
0.0731  0.0001  [2] Using template: /templates/navigation/main-menu-level-3.html
0.0739  0.0008  [1] Using template: /templates/navigation/main-menu-level-2.html
0.0743  0.0004  [19] Using template: /templates/navigation/main-menu-level-2.html
0.0754  0.0011  Using sub-template: /templates/logo.html
0.1276  0.0522  [nil] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch3_content_regions WHERE regionKey='content' AND (regionPage='/contact2222' OR regionPage='*')
0.1286  0.001   No matching content regions found. Check region name (content) and page path options.
0.1287  0.0001  [nil] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch3_content_index idx JOIN perch3_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch3_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE idx.regionID IS NULL AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID ORDER BY sortval ASC
0.1296  0.001   Template file not found: /var/www/landandwave/perch/templates/content/.html
0.1358  0.0061  Using template: /templates/mailchimp/forms/subscribe.html
0.1371  0.0014  Using template: /templates/pages/attributes/body_attributes.html
0.1374  0.0003  Using template: /templates/pages/attributes/body_attributes.html
0.1378  0.0004  [250] SELECT country, countryID FROM perch3_shop_countries WHERE countryActive=1 ORDER BY country ASC
0.1402  0.0024  Using template: /templates/shop/checkout/customer_create_passwordless.html
0.1436  0.0033  Request time: 0.1435
0.1436  0   Process time: 0.1427
0.1436  0   Memory: 1.4421

I think the main issues are

0.1286 0.001 No matching content regions found. Check region name (content) and page path options.

0.1296 0.001 Template file not found: /var/www/landandwave/perch/templates/content/.html

Hi Drew any update on this?

is it a perch issue or am i just doing something that's not possible..

Drew McLellan

Drew McLellan 2638 points
Perch Support

As yet, I can't see why that isn't working. Have you tried an isolated example? You obviously have a lot going on on that page.

Hi Drew

i just tried with a new template and a new page

<?php
PerchSystem::set_var('device', 'Mobile');

perch_content_create('content');

perch_layout('global/header');

perch_content_custom('content', []);

echo '<pre>';
print_r(PerchSystem::get_vars());
echo '</pre>';

perch_layout('global/footer'); 

And the same issue no region

Even tried without the header and footer and no luck

PerchSystem::set_var('device', 'Mobile');

perch_content_create('content');

perch_content_custom('content', []);

echo '<pre>';
print_r(PerchSystem::get_vars());
echo '</pre>';

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, getting the same here.

Do this instead:

perch_content('content', false);  
perch_content_custom('content', []); 

Hi Drew

That sort of works....

it now makes the region but i get duplicate content on the page

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sorry, my typo:

perch_content('content', true); 

That worked perfectly

Thanks very much just saved my life :)