jQuery suggestion
Permalink
Hi,
I am in the process of porting my finished theme into C5. It will have around 10 pages, and I'm pretty sure all of them could use one template, the default one.
My concern is jQuery used on these pages, it varies from page to page, different scripts need to be called and so on.
How should i deal with this if i want to use just one template file?
Can jQuery be added through blocks, so i can add what i want to page i want?
I guess that in this situation neither one header.php or footer.php works. Do i need different versions of header or footer for each page?
Thanks,
Mirko
I am in the process of porting my finished theme into C5. It will have around 10 pages, and I'm pretty sure all of them could use one template, the default one.
My concern is jQuery used on these pages, it varies from page to page, different scripts need to be called and so on.
How should i deal with this if i want to use just one template file?
Can jQuery be added through blocks, so i can add what i want to page i want?
I guess that in this situation neither one header.php or footer.php works. Do i need different versions of header or footer for each page?
Thanks,
Mirko
Yeah, i know that. But the thing is that i call different scripts for different pages.
For example page2 calls 5 scripts, page 8 calls just one script.
How do i deal with that? My HTML structure is mostly the same, so i could just use default.php for every page and populate content.
But the problem is that each page needs different scripts called.
For example page2 calls 5 scripts, page 8 calls just one script.
How do i deal with that? My HTML structure is mostly the same, so i could just use default.php for every page and populate content.
But the problem is that each page needs different scripts called.
Have you tried adding those scripts on "Header extra content"?
Edit -> Properties -> Custom Attributes -> Header Extra Content
I haven't personally tried it but it should work.
Edit -> Properties -> Custom Attributes -> Header Extra Content
I haven't personally tried it but it should work.
Does this work for each page separately? It only adds to the header for the selected page?
Thanks
Thanks
Yes, it's a page by page property.
Thank you very much, will look into this ASAP.
jQuery is automatically included by C5 when the dashboard toolbar is showing.
Otherwise, it is loaded at the request of particular themes or addons.
(So it is possible to, without explicitly loading jQuery, to have code that does not work when the user is not logged in)
For your theme, you should load jQuery using addHeaderItem and the html helper. This will ensure only the c5 copy of jQuery is loaded and that no matter how many components request it, it is loaded only once.
An alternative would be to use my (free) block
http://www.concrete5.org/marketplace/addons/load-jquery-ui/...
in a global area.
Code in this block also shows how to use the addheaderItem etc.
Otherwise, it is loaded at the request of particular themes or addons.
(So it is possible to, without explicitly loading jQuery, to have code that does not work when the user is not logged in)
For your theme, you should load jQuery using addHeaderItem and the html helper. This will ensure only the c5 copy of jQuery is loaded and that no matter how many components request it, it is loaded only once.
An alternative would be to use my (free) block
http://www.concrete5.org/marketplace/addons/load-jquery-ui/...
in a global area.
Code in this block also shows how to use the addheaderItem etc.
I think you haven't understood me. I don't have problems with loading jQuery, i am trying to find a way to call different scripts for each page. And each page is based on the same template-default.php
I can't populate header.php with 40 scripts, they need to be called by each page, depending on needs.
Hope this clarifies a bit.
I can't populate header.php with 40 scripts, they need to be called by each page, depending on needs.
Hope this clarifies a bit.
With 40+ scripts, it may be more efficient to aggregate and minimise them all into one file and load that for all pages. That way you get the best cache advantage. Its what ccm_app_base does for dashboard scripts.
Otherwise, as suggested by @pmarques, though I also have never used that method.
Otherwise, as suggested by @pmarques, though I also have never used that method.
I was thinking of doing something like that, but i need to make sure there are no conflicts between scripts.
My other alternative is to create a template for each page and header for each page.
Can i have header2.php, header3.php in C5?
My other alternative is to create a template for each page and header for each page.
Can i have header2.php, header3.php in C5?
Hi,
I think there might be some confusion here about where JS code should go -- in the theme templates vs. on a per-page basis vs. in the content that requires it itself (e.g. the blocks that get put on a page).
You say that there's really only one layout that all of your pages need, but there are different javascript requirements on each page? If that's true, then I'm guessing you should associate your javascript with the *content* of the pages (that is, the blocks themselves), not with the theme template.
Does this make sense? Or am I not understanding your requirement? If you could explain the overall picture a little more (for example, explain what a couple of your site pages do/show, and which ones need which JS files and for what reason), then I think someone would be able to give you a more thorough answer.
HTH
I think there might be some confusion here about where JS code should go -- in the theme templates vs. on a per-page basis vs. in the content that requires it itself (e.g. the blocks that get put on a page).
You say that there's really only one layout that all of your pages need, but there are different javascript requirements on each page? If that's true, then I'm guessing you should associate your javascript with the *content* of the pages (that is, the blocks themselves), not with the theme template.
Does this make sense? Or am I not understanding your requirement? If you could explain the overall picture a little more (for example, explain what a couple of your site pages do/show, and which ones need which JS files and for what reason), then I think someone would be able to give you a more thorough answer.
HTH
I had started writing a reply about on_page_view event handlers originally, then read again and saw the original post was about a theme and rewrote it.
Hi Jordan,yes you are right. Each of my pages can be created from one template(default.php), but the problem is that each has it's own JavaScript requirements.
On one page it is video player, on the other is slideshow code and so on. The answer provided by @pmarques is great.
Edit -> Properties -> Custom Attributes -> Header Extra Content
I just add all the scripts needed for each page through Header Extra Content. I am even not calling any scripts from header.php after seeing this sweet thing.
I go to each page's properties and add the relevant JavaScript code, it is great and clean solution!!
On one page it is video player, on the other is slideshow code and so on. The answer provided by @pmarques is great.
Edit -> Properties -> Custom Attributes -> Header Extra Content
I just add all the scripts needed for each page through Header Extra Content. I am even not calling any scripts from header.php after seeing this sweet thing.
I go to each page's properties and add the relevant JavaScript code, it is great and clean solution!!
Okay, I'm glad you found a working solution.
Do keep in mind for future reference, though, that if you want to be able to move around content from one page to another on your site (or add video players to new pages, etc.), the better long-term solution would be to make sure the video player block (or the slideshow block, etc.) was outputting the proper javascript itself. But if you don't need this complexity then by all means you should stick with the simpler solution you have going.
Do keep in mind for future reference, though, that if you want to be able to move around content from one page to another on your site (or add video players to new pages, etc.), the better long-term solution would be to make sure the video player block (or the slideshow block, etc.) was outputting the proper javascript itself. But if you don't need this complexity then by all means you should stick with the simpler solution you have going.
Concrete5 automatically includes jQuery on all pages.