Solved

Conditional PHP code on specific node?

Posted on 2013-06-05
10
267 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 51

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 51

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 51

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

11 Experts available now in Live!

Get 1:1 Help Now