Defining PHP function for use in stylesheet
Posted: 26 March 2008 06:33 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  290
Joined  02-02-2007

Hi,

Docs page “Linking to Stylesheets” says:

you can allow your stylesheets to run through the full template parser so tags and PHP can be used. To do so, just use a normal path variable to access your stylesheet. If you specify a template this way make sure that it is set to “CSS Stylesheet” as the Template Type.

Having done this I defined PHP function in stylesheet template and made several calls to it from the same template.

Now I am observing several strange effects:

1) if PHP function is defined in stylesheet after loading a page I get PHP error “Call to undefined function”;

2) if PHP function is defined in include file and called from stylesheet, the page loads, but is unstyled; if I load stylesheet alone, I find in it PHP error “Call to undefined function”;

3) if PHP function is defined BOTH in in stylesheet and in include file, everything works as expected.

This seems very strange to me. PHP should work in cases (1) and (2) and it should not work in case (3), but now it is vice versa.

How should be PHP function to be defined for use in stylesheets?

I use ExpressionEngine 1.6.1 build 20071204 on EngineHosting.

Thanks.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 26 March 2008 07:04 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23547
Joined  05-20-2002

Does seem weird- can you paste your code?  And is php being parsed input or output?

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 26 March 2008 07:45 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  290
Joined  02-02-2007

Here is top part of the page template:

<?php
include 'php_scripts/browser_sniffing.php';
?>

{embed
="embeds/doctype"}

<head>

{embed="embeds/meta_content_type"}

<title>Abbreviations and contracted references</title>

<
link rel="stylesheet" type="text/css" media="screen" href="{path=stylesheets/css_ebook_screen}" />

Here is contents of browser_sniffing.php.

Here is one of the calls to PHP function from stylesheet (in the same stylesheet there is   the same function definition as in browser_sniffing.php file):

<?php

$a_browser_data
= browser_sniffing('full');

if (
$a_browser_data[0] != 'ie' )
{
    
echo '#text td.linenumber {' . "\n";
        echo
'display: table-cell;' . "\n";
        echo
'}' . "\n";
}
else {
      
echo '#text td.linenumber {' . "\n";
      echo
'display: block;' . "\n";
      echo
'}' . "\n";
}

?>

As I said in previous post, everything works if there is both include statement and function definition in stylesheet; if there is only include or only function definition in stylesheet I get PHP error.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 26 March 2008 07:57 AM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23547
Joined  05-20-2002

Ah- ok, templates are parsed separately.  So if you include a php file (or have php in a template)- even if you embed another template, the embedded template doesn’t have access to any of the variables/functions/etc in that index template.  So yep- this would be expected behavior.  You don’t need the include in the index template- unless you’re using it there as well.  But if you call the function in the css template?  You’ll need the php included there as well.

Also- I suspect there’s a pure css way to do this- might broach it in the design forum.

Anyhoo- that make sense?

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 26 March 2008 08:54 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  290
Joined  02-02-2007

templates are parsed separately.
[...]
You don’t need the include in the index template- unless you’re using it there as well.

That means that I am wrong supposing that in case (2) PHP should work.

The main question remains: why PHP does not work in case (1) - both when PHP function is defined in stylesheet template an when there is include in in stylesheet template?

And why PHP starts to work in case (3) -  since templates are parsed separately, include in main template should have no effect on PHP in stylesheet template, but it seems that it does have effect.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 01 April 2008 10:44 AM   [ Ignore ]   [ # 5 ]  
Moderator
Avatar
RankRankRankRankRankRankRankRank
Total Posts:  32921
Joined  05-14-2004

Hi, Laisvunas,

I’d recommend some reduction testing.  for instance, create a template group called “blog” and create a template in that called test, put this in it:

<html>
<
head>
<
title>page title</title>
<
link rel="stylesheet" type="text/css" media="screen" href="{path=blog/style}" />
</
head>
<
body>
<
p>Just some content</p>
</
body>
</
html>

Now create a template called style, with PHP set on Output, and put this in it:

<?php
function fav_color() {
     
return 'red';
}
?>

body {
     background
: <?php echo fav_color();?>;
}

does that work for you?

 Signature 
Profile
MSG
 
 
Posted: 01 April 2008 11:10 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  290
Joined  02-02-2007

Hi Lisa,

Thanks for suggestion. I tried your code and it works correctly.

It means that behavior I described in this thread is not a general, but quite specific problem, somehow related with some other code I use in my templates.

Still I am confused what might cause such weird behavior and what might be a proper solution. A solution I adopted is this: I wrapped the function into a plugin and wrapped in a plugin it works correctly in my stylesheet templates.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 02 April 2008 09:31 AM   [ Ignore ]   [ # 7 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23547
Joined  05-20-2002

Agreed- it’s weird.  I’d say #2 would be the proper approach.  But the optimum would be to go the plugin route.  If you like, I can bump it up to the crew.  They might spot the reason for the oddness.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 04 April 2008 07:56 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  290
Joined  02-02-2007

Hi Robin,

Of course it would be good to know reasons for this behavior. But as yet I even do not know if I would be able to reproduce it starting from blank web and stylesheet templates and reduce to something more definite. I will investigate this behavior more closely in following days and post the results in this thread if I found anything.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 06 April 2008 09:42 AM   [ Ignore ]   [ # 9 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23547
Joined  05-20-2002

Sounds good- let us know if you can reproduce consistently.  Does strike me as odd.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 11 July 2008 08:45 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  44
Joined  09-08-2002

Laisvunas:

  I stumbled on your post while browsing the forum and was wondering if it would serve you better to create a plug-in that sets a global variable.  This way you can access what you need or call the method from anywhere within any template.

  I had a simular problem in accessing an html manipulating function http://expressionengine.com/forums/viewthread/84813/. The end solution took care of my scoping problem.

  Hope this is of some help, or at least food for thought.

Herb

 Signature 

Everybody is entitled to my opinion!

Profile
 
 
Posted: 14 July 2008 02:31 PM   [ Ignore ]   [ # 11 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15489
Joined  05-15-2004

Thanks for your contribution, Herb. Closing this old thread ...

 Signature 

Everything will be good in the end. If it’s not good, it’s not the end.

Profile
MSG
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 65087 Total Logged-in Users: 40
Total Topics: 82226 Total Anonymous Users: 22
Total Replies: 441925 Total Guests: 214
Total Posts: 524151    
Members ( View Memberlist )