Solved

Conditional PHP code on specific node?

Posted on 2013-06-05
10
271 Views
Last Modified: 2013-06-21
I've got a JS slideshow I'm displaying only on one page. It has it's own .js and .css files associated with it and I don't need to have those files across the entire site.

Should I use a conditional PHP statement in a block for that node? Like an if else statement?

What's the code I use?
0
Comment
Question by:sandshakimi
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39222150
Yes.  There is no "extra credit" for publishing data that is not used, so I would not put links to the JS and CSS in pages that have no use for them.  You can use an if() statement to wrap the code block something like this...

if ($onePage)
{
    require_once('onePage.js);
    require_once('onePage.css');
}

Open in new window

HTH, ~Ray
0
 

Author Comment

by:sandshakimi
ID: 39222260
1) What text format should the block use?

2) $onePage is the variable for page title? or page node id?
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39222292
There are a couple of arguments either way.

The browser should cache the files anyway so even if you do download them on the first page there is only one hit and then you run off cache (unless you specifically code it not to).

I would change Rays example a little bit because it is a bit confusing

In your head section of your page
<head>
<title>...</title>
<?php if ($onepage) : ?>
<link rel="stylesheet" href="onePage.css" />
<script type="text/javascript" src="onePage.js"></script>
<?php endif; ?>
</head>

Open in new window


But it depends on how you have structured your page. My pages are in a framework so I have a method I call in the controller of the page being loaded called head() in which I can add scripts and stylesheets - which the framework then outputs in the right format and in the right place.

The above will work though - Ray's example includes the .js and .css directly into the page instead of linking to them - which is what I think the requirement was.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39222316
1) What text format should the block use?
Can you please clarify "text format?"
0
 
LVL 9

Expert Comment

by:scifikillr
ID: 39222346
Keep in mind that PHP blocks cannot be cached.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39222503
Keep in mind that PHP blocks cannot be cached.

He is talking about loading .js and .css files - which will be cached.
0
 

Author Comment

by:sandshakimi
ID: 39222522
So it looks like I have the option of 1) putting the link to the .js an .css in the head section or 2) putting all the .js code and .css code directly into the page via a block.

Is that right? Is one better than the other?

And what I meant by asking about text format is the drop down menu of the block body: full html, plain text, etc.
0
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 167 total points
ID: 39222585
I would go with head section (at least for the style) validation fails for style elements in the body of the html document.

Javascript can pretty much go anywhere if you don't mind a messy output with script all in different places.

My preference is keep the js and css in separate files and load in the head.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 39222726
During development I like to have the JS and CSS directly in the head of the document.  It's easier to have all the parts in one place.  Once I am ready to deploy, I would move these pieces to separate files.

Still maybe not understanding the part about text format.  If you want to render a web page, you need HTML.  HTML is semantic markup -- that is, it attaches meaning to page elements.  The elements may be plain text, images, hyperlinks, etc.  CSS is stylistic markup.  It uses ID and CLASS attributes in the HTML to make a visual statement.
0
 
LVL 17

Accepted Solution

by:
nanharbison earned 167 total points
ID: 39225274
You use the PHP format when adding javascript or PHP to a page.You could add the javascript to the page content of that particular page, or the html code to include the js files.
Of course, you don't want to add the include them in the template of the site because that calls them on every page.
You have to be VERY careful adding PHP snippets to pages. If you make a mistake, it can cause random error messages on your site which seem unrelated to what is causing the problem.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now