Solved

Conditional PHP code on specific node?

Posted on 2013-06-05
10
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 110

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 56

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 110

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
 
LVL 56

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 56

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 110

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

697 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