Conditional PHP code on specific node?

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?
sandshakimiAsked:
Who is Participating?
 
nanharbisonConnect With a Mentor Commented:
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
 
Ray PaseurCommented:
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
 
sandshakimiAuthor Commented:
1) What text format should the block use?

2) $onePage is the variable for page title? or page node id?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Julian HansenCommented:
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
 
Ray PaseurCommented:
1) What text format should the block use?
Can you please clarify "text format?"
0
 
scifikillrCommented:
Keep in mind that PHP blocks cannot be cached.
0
 
Julian HansenCommented:
Keep in mind that PHP blocks cannot be cached.

He is talking about loading .js and .css files - which will be cached.
0
 
sandshakimiAuthor Commented:
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
 
Julian HansenConnect With a Mentor Commented:
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
 
Ray PaseurConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.