addHeaderItem in block template
Permalink
I'm trying to include different css file in block template depending on a certain condition. I've tried to do it with
$controler->addHeaderItem(html->css($cssURL))
and with instantiating a view, and calling addHeaderItem on the view (I even set the collection object for the view) but it does not work. I can just do
echo html->css($cssURL)
but I would rather add it to the header. I'm writing a template for autonav block. Is it possible to use addHeaderItem in custom block template? Do I have to modify autonav controller to achieve this?
$controler->addHeaderItem(html->css($cssURL))
and with instantiating a view, and calling addHeaderItem on the view (I even set the collection object for the view) but it does not work. I can just do
echo html->css($cssURL)
but I would rather add it to the header. I'm writing a template for autonav block. Is it possible to use addHeaderItem in custom block template? Do I have to modify autonav controller to achieve this?
you can find an example here:http://www.codeblog.ch/2009/04/concrete5-drop-down-menu/...
there I'm not using a css folder butz rather a file called view.css which works as well if you just have a single css
there I'm not using a css folder butz rather a file called view.css which works as well if you just have a single css
my problem is this:
I need this for aliases. I want a different style for alias depending on where it's positioned in the site map (if it's under the autonav root or not). I know that addHeaderItem works from controller (I could add on_page_view() function to autonav controller) but I hoped that it won’t be necessary to modify the controller. If it's not possible to solve this through template using php, I'll try with javascript dynamically loading css file...
If it's not already possible to use addHeaderItem in template, are you planning on making it possible?
if ($condition) { //include first.css } else { //include second.css }
I need this for aliases. I want a different style for alias depending on where it's positioned in the site map (if it's under the autonav root or not). I know that addHeaderItem works from controller (I could add on_page_view() function to autonav controller) but I hoped that it won’t be necessary to modify the controller. If it's not possible to solve this through template using php, I'll try with javascript dynamically loading css file...
If it's not already possible to use addHeaderItem in template, are you planning on making it possible?
First of all - I'm not a member of the core team. I can't plan anything about the official Concrete5 release. I've got my own spin off version, but that's completely different story..
Why don't you generate proper classes in your autonav template? There you know the level and position of an element?
Why don't you generate proper classes in your autonav template? There you know the level and position of an element?
.level_2 li { bla } .root li { blu }
but I need a bit different style depending on a condition and a css would bee smaller (simpler to maintain) if I could split it in two. I'll try with javascript and dynamically loading proper css and if that won't work I'll try to solve it with one css...
Thx for your time!
Thx for your time!
You can do something like this:
It is not verry clean but it can solve your problem.
<div class="
<?php if ($condition) { $class= 'class1'; } else { $class= 'class2'; } ?> <div class="<?php echo $class;?>"> ... </div>
It is not verry clean but it can solve your problem.
<div class="
Sorry for refreshing this question but I do have the same problem. I understand that it's working with an additional folder like you explained. But why isn't it possible to do this in the controller of a block:
$html = Loader::helper('html');
$css = $html->css('someStyle.css');
View::addHeaderItem($css);
$html = Loader::helper('html');
$css = $html->css('someStyle.css');
View::addHeaderItem($css);
Hi Remo,
I have tried this.. worked for one block(slideshow). Trying the same on the Autonav block.. it loads the css/js in the backend(i have the block in my scrapbook) but it fails to load the css/js on the actual page.
I am a bit lost here and dont know where to look for a solution.
Yash
I have tried this.. worked for one block(slideshow). Trying the same on the Autonav block.. it loads the css/js in the backend(i have the block in my scrapbook) but it fails to load the css/js on the actual page.
I am a bit lost here and dont know where to look for a solution.
Yash
Same here. It's just not working. Neither putting js in seperate folder nor via addHeaderItem in on_start function. Can somebody please share a functional version of addHeaderItem in Block controller!
Hey,
firstly, I always put this in on_page_view, not on_start, so that might solve the issue right there (haven't tested though).
If it doesn't, could you paste the code from your on_page_function and also give a link to your site please. If you can't provide a link, view the source and see if the function has outputted anything at all. You may find it's just outputting the script tag with the wrong path to your script.
Jon
firstly, I always put this in on_page_view, not on_start, so that might solve the issue right there (haven't tested though).
If it doesn't, could you paste the code from your on_page_function and also give a link to your site please. If you can't provide a link, view the source and see if the function has outputted anything at all. You may find it's just outputting the script tag with the wrong path to your script.
Jon
I already switched to on_page_view and found out that addHeaderItem only works on blocks added via the C5 interface. My block is hard coded into the site. what can i do about that?
Ahh, that will be your issue then.
Unfortunately, by the time the blocks controller has run, if it is hardcoded onto the page, then it's too late to add the CSS into the header.
This leaves you with 3 options. (I'm in a 'list' mood today...)
1) If the autnav is on every page, just add the css to your main css file. this will have the added benefit of speeding up your page loads.
2) If you really want a separate css file for maintenance, just call it from your sites header.php file.
3) Lastly, you can use jQuery in your autonav view.php to dynamically load in your css file correctly, but this would be a last resort for me... If you really wanna try it, then let me know and I'll add the code to do it.
Unfortunately, by the time the blocks controller has run, if it is hardcoded onto the page, then it's too late to add the CSS into the header.
This leaves you with 3 options. (I'm in a 'list' mood today...)
1) If the autnav is on every page, just add the css to your main css file. this will have the added benefit of speeding up your page loads.
2) If you really want a separate css file for maintenance, just call it from your sites header.php file.
3) Lastly, you can use jQuery in your autonav view.php to dynamically load in your css file correctly, but this would be a last resort for me... If you really wanna try it, then let me know and I'll add the code to do it.
Thank you very much for your effort. Finally your statement that its to late for adding files is what counts. Basicly it's a JS file I want to add that is special to a block hard coded into the site. but now i know WHY its not working, thanks again
Hallo Remo
I made a new block template for an image block and put it in the root: /blocks/image/templates/
and there I created a folder with my template "Zoomen". In there I created a folder named js and there I thrown the whole fancybox folder which contains all the javascript, css and images. It just doesn't work...
I just wanted to create a custom template for a standard image block which includes fancybox. how would I do that? thank you!
I made a new block template for an image block and put it in the root: /blocks/image/templates/
and there I created a folder with my template "Zoomen". In there I created a folder named js and there I thrown the whole fancybox folder which contains all the javascript, css and images. It just doesn't work...
I just wanted to create a custom template for a standard image block which includes fancybox. how would I do that? thank you!
The folder name for the template should be all lowercase, so it would be this:
And you can't just dump all the fancybox stuff in a "js" folder because fancybox relies on CSS and images as well. So put the fancybox js in the js folder but also create a "css" folder and put the css in there. *AND* create an "images" folder and put all the images there. THEN go into the css file and change all of the image url's so the path is appropriate for the new directory structure (so the url path should be something like url(../images/myimagename.jpg)
You also might want to check out the free "Zoom Image Dual" addon in the marketplace which I think does what you want to do.
/blocks/images/templates/zoomen/
And you can't just dump all the fancybox stuff in a "js" folder because fancybox relies on CSS and images as well. So put the fancybox js in the js folder but also create a "css" folder and put the css in there. *AND* create an "images" folder and put all the images there. THEN go into the css file and change all of the image url's so the path is appropriate for the new directory structure (so the url path should be something like url(../images/myimagename.jpg)
You also might want to check out the free "Zoom Image Dual" addon in the marketplace which I think does what you want to do.
I installed 'zoom image' and 'zoom image dual' which do what I want. thank you - thank you
- create a file called view.php and put your code there
- create a folder called css underneath mytemplate and save all the css files there
c5 automatically includes these files (works with js as well). there's a post from andrew which explains it with a few more details...