Adding Javascript to one page only in the header.php

Permalink
Hi there,
So I am not the greatest at really the coding side of things but have muddled through relatively well.

I tried to write my own bit of PHP but it's very late here and my head is spinning.

What I would like to do is add a load of JQuery/Javascript to a single page.

How I thought would do this is create an argument... if the CollectionHandle is equal to "portfolio" put the JS in.

so I wrote:
<?php if(is_object($c->getCollectionHandle == 'portfolio') ?>
<script type="text/javascript">
</script>
<?php endif; ?>


Could anyone tell me where I have gone wrong? IS this the best way of doing it?

Many thanks in advance

dancer
View Replies: View Best Answer
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Do you mean you want to add this to just one page of your site, or you actually have a concrete5 "single_page"? I think you're saying the former, and I think your code is on the right track (although you've got some typos / php errors in it -- you didn't close both parentheses, and you need a colon after the last parentheses). Here's how I'd do it:

Put this inside the <head> of your page type template (might be in elements/header.php, or maybe not, depending on the theme you're using):
<?php if ($c->getCollectionHandle() == 'portfolio'):?>
  <script type="text/javascript" src="<?php echo $this->getThemePath(); ?>/myjsfile.js"></script>
<?php endif; ?>


(Note that it might be '/portfolio' instead of 'portfolio' -- with a preceding slash -- but I'm not sure off the top of my head).
dancer replied on at Permalink Reply
dancer
Thanks so much Jordan. Spot on...

I get really frustrated with all the PHP stuff, and am desperately trying to learn (merely a designer here). I have a list of logic/argument things that help me add functionality to a website.

I have one more question if you could be so kind :)

I add a load of page lists to one page but want to give each page list <ul> a unique class name and each list item therein a unique id from whereever it is from.
eg

<ul class="online">
<li id="online1">Something digital 1</li>
<li id="online2">Something digital 2</li>
</ul>
<ul class="offline">
<li id="offline1">Something print 1</li>
<li id="offline2">Something print 2</li>
</ul>


Here is my attempt but, lo and behold it doesn't work :)

<?php
$parent = Page::getByID($c->getCollectionParentID()); echo $parent->getCollectionName();
?>
<ul class="<?php echo $parent ?>"
<?php $uniqueid = 0;
$uniqueid++; ?>
<li id="<?php echo $parent.$uniqueid ?>


I suppose I should always have a go...
jordanlev replied on at Permalink Reply
jordanlev
Hi Alistair,
If you're trying to learn php, I actually would suggest you learn it outside of the concrete5 system, because otherwise you're actually having to learn two very complicated things at once. Of course sometimes it's only motivating to learn when there are real-world goals, so maybe not :)
(I guess the point is not to put down your own abilities if you can't figure some of this out -- you're taking on a pretty difficult task here!)

ANYway... I'd be happy to help you with this but I need to know if you're just trying to output a bunch of links on your page template, or are you trying to create a custom template for the Page List block?
If you're just outputting things on your page template, you don't need any of that code you're putting in there, just write out the <ul>'s and <li>'s and put in whatever class and id names you want.

If you're trying to make a custom template for the page list block, then it's going to be more... complicated (but certainly doable).
dancer replied on at Permalink Reply
dancer
Hey Jordan

I understand what you mean about PHP... I guess the only time I really use it is when I build with C5...

Yes I am making a custom template.. which I have built so far.. through picking apart pages and trial and error.

In my site map I have 3 areas/categories with pages(or projects) in each

Then on the main portfolio page I have 3 different pages lists on the same page. Online, offine, brand. Instead of creating 3 custom templates I thought that I could have one that places the a class i.e the parent of the respective list on each page list.

Now within that I need a unique ID for each list item this is so I can use some JQuery to filter the results.

Here is my custom template for the page list so far

<?php 
   defined('C5_EXECUTE') or die(_("Access Denied."));
   $textHelper = Loader::helper("text"); 
   $imgHelper = Loader::helper('image'); 
   if (count($cArray) > 0) { ?>
   <ul class="portfolio-page-list">
   <?php 
   for ($i = 0; $i < count($cArray); $i++ ) {
      $cobj = $cArray[$i]; 
      $title = $cobj->getCollectionName(); 
      $uniqueid = $cobj->getCollectionID();
      ?>
      <?php if($cobj->getAttribute('thumbnail')): ?>
      <li class="ic_container">
         <a href="<?php echo $nh->getLinkToCollection($cobj)?>" style="background-image: url(<?php echo $imgHelper->getThumbnail($cobj->getAttribute('thumbnail'), 334, 104)->src ?>);">


Thank you so much for you sttention this far

Alistair
jordanlev replied on at Permalink Reply
jordanlev
Got it.

Here are the issues with that other code you pasted in before (which I mention as a learning exercise, not to criticize you for it).

First of all, you don't need to worry about the $uniqueid = 0 or $uniqueid++ things, because in the page list template you're already using the page id (otherwise known as collection id or "cid") for your $uniqueid. I'm guessing you figured that out already because that thing isn't normally in the template so it must have been added by you.


Another issue: you are setting the $parent variable to the parent page, which is good. And right after that you have some code to get the name of the parent page ($parent->getCollectionName()) which is also good. BUT you are only "echo"ing the parent name (which means output it to the browser as text or html) in that second part of the line, which you don't really want to do right there because it's not inside your <ul class="..."> tag yet.
A few lines down you are "echo"ing the $parent, but this isn't going to output the name of the page. Instead it's going to output a billion lines of jibberish -- basically every single piece of data associated with a $page, of which there is tons.

Solution: don't ever echo $parent. Instead, echo $parent->getCollectionName();
Advanced Solution: typing out "$parent->getCollectionName()" every time is annoying and probably offends your designer's sense of economy, so what you can do is just below the "$parent = Page::getByID..." line, add this:
$parent_name = $parent->getCollectionName();

and now wherever you want the parent name, do this:
<?php echo $parent_name; ?>

ahh... much better (that is one of the reasons programmers assign things to variables all the time -- to make code easier to read)


The other issue with your pasted example from before is that you are missing a closing ">" after the "<ul" tag (confusing, I know, because that <?php echo ... ?> thing makes it hard to see -- a good text editor that does syntax highlighting can help with this).


So anyway, I think the code you want for your template is this:
<?php 
   defined('C5_EXECUTE') or die(_("Access Denied."));
   $textHelper = Loader::helper("text"); 
   $imgHelper = Loader::helper('image'); 
   $parent = Page::getByID($c->getCollectionParentID());
   $parent_name = $parent->getCollectionHandle();
   $parent_name = str_replace('_', ' ', $parent_name); //Replace spaces in the parent's page name with underscores, so we can safely use it as a class name
   if (count($cArray) > 0) { ?>
   <ul class="portfolio-page-list <?php echo $parent_name; ?>">
   <?php 
   for ($i = 0; $i < count($cArray); $i++ ) {
      $cobj = $cArray[$i]; 
      $title = $cobj->getCollectionName(); 
      $uniqueid = $cobj->getCollectionID();
      ?>


Note that I also added a function which replaces spaces in the page name with underscores (because otherwise if you had a page named "About Us", it would come out as two separate CSS classes -- ".About" and ".Us").
dancer replied on at Permalink Reply
dancer
Wow. I see it all now.

I think I have the right idea I just need to learn how to declare and then deploy with the correct syntax of course.

I am getting an error though:
Fatal error: Call to a member function getCollectionParentID() on a non-object in /Applications/MAMP/htdocs/concrete_adance/blocks/page_list/templates/portfolio_list_caption.php on line 6


Line 6 is

$parent = Page::getByID($c->getCollectionParentID());
jordanlev replied on at Permalink Reply
jordanlev
Yes, that happens sometimes (I haven't quite figured out yet why it works sometimes and not others). To fix it, add this line before the "$parent = Page::getByID(..." line:
$c = Page::getCurrentPage();
dancer replied on at Permalink Reply 1 Attachment
dancer
How strange.

So I think we are nearly there... but I have noticed that the parent of the actual page where these lists are featured therefore they are all being called "home"

A screenshot of my site map is as attached. The pages lists with this custom template are on the Portfolio page and I have 3 page lists here. I would like the

$parent_name


to be the Online, print, digital. Not Home.

Sorry to keep coming back Jordan
jordanlev replied on at Permalink Reply
jordanlev
Oops... didn't catch that.
So the problem with that is "$c" is the page that the page_list template is on (which is the portfolio page), and so the parent page of the page that it's on is "home" (because the portfolio page is under "Home" in the sitemap).

What you want to do instead of getting the "current page's parent", is get "the top-level page that the page_list is showing". So instead of these 2 lines of code:
$c = Page::getCurrentPage();
$parent = Page::getByID($c->getCollectionParentID());

you want this one line:
$parent = Page::getByID($controller->cParentID);

(cParentID is the name of the field that holds the page id of the page you selected to list pages under when adding/editing the Page List block)

And no need to apologize -- I'm just hacking away on another concrete5 project right now so it's all in my brain anyway :)
dancer replied on at Permalink Reply
dancer
Oh man... but this has given rise to another issue though.

Basically when you land on the portfolio page you get ALL projects under every category. These are displayed by a page list that all use the same page type.

I have 3 buttons an the top that filter the results.. so when I click print, all other thumbnails disappear.

The problem is that when they are ALL displayed the <ul> & more importantly the <il> dose not feature the parent and id.

This is the only way the JQuery can filter the results by checking the if the ids are the same.

I may be going way beyond my remit of asking you this and if I can paypal you some $$ then send me your details

:)
jordanlev replied on at Permalink Reply
jordanlev
Let's take this conversation offline. I'll PM you shortly...