jump to navigation

Register head tags from helpers (2) March 28, 2006

Posted by rossoft in CakePHP.
trackback

Improved version of https://rossoft.wordpress.com/2006/03/23/register-javascript-css-files-from-helpers/ 

Some examples self-explanatory:

$head->register_js("test2");
$head->register_css("mycss2");
$head->register_css("mycss1",array('media'=>'print'));
$head->register_cssblock("    .modbox .el {display:none;}
                            .modbox .csl, .modbox .es {display:none;}
                            .modbox_e .el {display:none;}");
$head->register_jsblock('alert("hi!");');
$head->register_link(array(    'rel'=>'alternate',
                            'type'=>'text/xml',
                            'title'=>'General RSS 0.91',
                            'href'=>'http://www.elotrolado.net/temp/rss091.xml'));

$head->register_meta(array(    'http-equiv'=>'Content-Type',
                            'content'=>'text/html; charset=UTF-8'));

$head->register_raw('<script>alert("raw html");</script>');

Copy to /app/views/helpers/head.php

<?php
/**
 * Head Helper
 * Register <head> tags from helpers, then print them
 * in head through layout.
 * @author RosSoft
 * @license MIT
 * @version 0.2  
 */
class HeadHelper extends Helper 
{
    var $helpers=array('html','javascript');
    
    var $_library; //static array of items to be included            

    function __construct()
    {
           static $library=array();  //for php4 compat 
           $this->_library=& $library;
    }

    /**
     * Adds a css file to array 
     * @param string $file CSS file to be included
     * @param string $param Array of htmlAttributes
     */
    function register_css($file,$htmlAttributes=null)
    {
        $this->_register(array($file,'css',$htmlAttributes));
    }
    
    /**
     * Adds an inline css block to array 
     * @param string $css CSS tags to be included
     * @param string $param Array of htmlAttributes
     */
    function register_cssblock($css,$htmlAttributes=null)
    {
        $this->_register(array($css,'cssblock',$htmlAttributes));
    }
    
    
    /**
     * Adds a js file to array 
     * @param string $file CSS file to be included
     * @param string $param Array of htmlAttributes
     */
    function register_js($file)
    {
        $this->_register(array($file,'js'));
    }
    
    /**
     * Adds a javascript block to array 
     * @param string $javascript Javascript block to be included
     * @param string $param Array of htmlAttributes
     */
    function register_jsblock($javascript)
    {
        $this->_register(array($javascript,'jsblock'));
    }    
    
    /**
     * Adds a meta tag to array
     * @param array $htmlAttributes Array of html attributes of meta tag  
     */
    function register_meta($htmlAttributes)
    {
        $this->_register(array($htmlAttributes,'meta'));
    }
    
        
    /**
     * Adds a link tag to array
     * @param array $htmlAttributes Array of html attributes of meta tag  
     */
    function register_link($htmlAttributes)
    {
        $this->_register(array($htmlAttributes,'link'));
    }          
    
    /**
     * Adds a raw sequence of html tags to array
     * @param string $raw Sequence of html tags
     */
    function register_raw($raw)
    {
        $this->_register(array($raw,'raw'));
    }

    /**
     * Prints the html for all of the items registered
     * @return string 
     */           
    function print_registered()
    {
        foreach ($this->_library as $l)
        {
            switch ($l[1])
            {
                case 'css':
                    echo $this->html->css($l[0],'stylesheet',$l[2]);    
                    break;
                case 'js':
                    echo $this->javascript->link($l[0]);
                    break;
                case 'jsblock':
                    echo $this->javascript->codeBlock($l[0]);
                    break;
                case 'meta':
                    echo "<meta " . $this->_parseAttributes($l[0]) . " />";
                    break;
                case 'link':
                    echo "<link " . $this->_parseAttributes($l[0]) . " />";
                    break;
                case 'raw':
                    echo $l[0];
                    break;
                case 'cssblock':
                    echo '<style type="text/css" ' .  $this->_parseAttributes($l[2]) . " ><!--{$l[0]}--></style>";
                    break; 
                default:
                    die('Internal error on HeadHelper: Unknown type registered.');
            }
        }             
    }
    
    
    /**
     * Adds the item in the array if it doesn't already exist
     * @param array $item Item to be added
     * @access private
     */
    function _register($item)
    {
        if (! in_array($item,$this->_library))
        {
            $this->_library[]=$item;
        }                    
    }                                           
    
    
    
    /**
     * This is a copy of the same function in HtmlHelper
     * Returns a space-delimited string with items of the $options array. If a
     * key of $options array happens to be one of:
     *    + 'compact'
     *    + 'checked'
     *    + 'declare'
     *    + 'readonly'
     *    + 'disabled'
     *    + 'selected'
     *    + 'defer'
     *    + 'ismap'
     *    + 'nohref'
     *    + 'noshade'
     *    + 'nowrap'
     *    + 'multiple'
     *    + 'noresize'
     *
     * And its value is one of:
     *    + 1
     *    + true
     *    + 'true'
     *
     * Then the value will be reset to be identical with key's name.
     * If the value is not one of these 3, the parameter is not output.
     *
     * @param  array  $options      Array of options.
     * @param  array  $exclude      Array of options to be excluded.
     * @param  string $insertBefore String to be inserted before options.
     * @param  string $insertAfter  String to be inserted ater options.
     * @return string
     */
    function _parseAttributes($options, $exclude = null, $insertBefore = ' ',
    $insertAfter = null)
    {
        $minimizedAttributes = array(
        'compact',
        'checked',
        'declare',
        'readonly',
        'disabled',
        'selected',
        'defer',
        'ismap',
        'nohref',
        'noshade',
        'nowrap',
        'multiple',
        'noresize');

        if (!is_array($exclude))
        {
            $exclude = array();
        }

        if (is_array($options))
        {
            $out = array();

            foreach ($options as $key => $value)
            {
                if (!in_array($key, $exclude))
                {
                    if (in_array($key, $minimizedAttributes) && ($value === 1 ||
                    $value === true || $value === 'true' || in_array($value,
                    $minimizedAttributes)))
                    {
                        $value = $key;
                    }
                    elseif (in_array($key, $minimizedAttributes))
                    {
                        continue;
                    }
                    $out[] = "{$key}=\"{$value}\"";
                }
            }
            $out = join(' ', $out);
            return $out? $insertBefore.$out.$insertAfter: null;
        }
        else
        {
            return $options? $insertBefore.$options.$insertAfter: null;
        }
    }
    

}
           
?>
Advertisements

Comments»

1. Naonak - April 21, 2006

Bugs with php 4 :

Warning: in_array(): Wrong datatype for second argument in /home/iciservi/www/iciservices/app/views/helpers/head.php on line 147

And

Warning: Invalid argument supplied for foreach() in /home/iciservi/www/iciservices/app/views/helpers/head.php on line 108

2. Nao - April 21, 2006

I find : Hack method “__construct” for php 4 don’t work in helpers

So instead of method __construct :

function HeadHelper()
{
static $library = array(); //for php4 compat
$this->_library=& $library;
}

3. rossoft - May 16, 2006

I’ve talked with Nao and __construct() works in PHP4.

Note for readers: Don’t forget to include the HeadHelper in your AppController, and put this in your HEAD section of your layout:

if (isset($head)) echo $head->print_registered();

4. RosSoft » combobox helper (Editable Selectbox) - May 22, 2006

[…] 1. Copy this to /webroot/js/combobox/ 2. Copy the helpers HeadHelper, UtilHelper from this blog to helpers/ (search the blog) 3. Copy that code to helpers/combobox.php […]

5. rossoft - May 25, 2006

If you haven’t readed the previous version, then put echo $head->print_registered() at of your layout

6. tariquesani - June 30, 2006

Thanks for all the hard work. I however like the syntax of previous version better rather than register_css register_js how about doing away with the register_ part and have just ‘css’ and ‘js’ etc

and the print_registered can be changed to something similarly shorter

7. rossoft - June 30, 2006

@tariquesani: thanks, that’s a good feedback

8. dave - July 13, 2006

i’d like to note that :

$head->register_meta

is also great for :
meta keywords
meta description
meta robots

Two questions, I always get an empty js tag in my html source, is that normal or am i missing something?
Is there anyway to clean up how the html is printed? all my meta tags end up inline, i’d like one per line.

9. edevil - July 15, 2006

Hi .. i was wondering if its possible to redirect to another page after a specific amount of time with this headHelper (without using javascript).

10. John Zimmerman - July 16, 2006

try the meta refresh tag using the register_meta

http://en.wikipedia.org/wiki/META_refresh

11. george - August 11, 2006

i´m also getting an empty

12. george - August 19, 2006

my last post was cut off, the issue is the same as mentioned by dave with an empty js tag.
i want to suggest further to put a newline \n after each echoed tag in print registered.

13. RosSoft » Updated packages in cakeforge - August 23, 2006

[…] Hi! I’ve been out because my internet connection at home was down Now I return with some improvements in the HeadHelper and CJS Templates. They’re in cakeforge (CJS Templates version 2.1) […]

14. RosSoft » Updated packages in cakeforge - August 23, 2006

[…] Hi! I’ve been out because my internet connection at home was down Now I return with some improvements in the HeadHelper and CJS Templates. They’re in cakeforge (CJS Templates version 2.1) […]

15. Tapter - September 26, 2006

Around line 210 in HeadHelper I changed the if to:
if ($this->cache)
{
if (count($cached[‘jsblock’]) > 0)
//print cached jsblock
echo $this->_codeBlock(implode(“\n”, $cached[‘jsblock’])) . “\n”;
}
to avoid empty javascript blocks

16. Jippi - October 30, 2006

I have changed a few things in the HeadHelper – I needed to be able to have default css – but also custom css so i could make my own custom files without having to overwrite the old

http://bin.cakephp.org/saved/215

17. yyxveexops - March 18, 2007

naked european men pictures

18. Dani - May 2, 2007

nice post, well done!

19. Constant - June 1, 2007

Hi,

Version 0.5 of HeadHelper state that it require version > 0.20 of UtilHelper but on this blog I can only find version 0.1 ?

Where can I found it ?

Thanks

Constant

20. Charles - June 25, 2007

Great Post, explained in plain english, special thanks to Rossoft…..

21. Andrew Assarattanakul - June 26, 2007
22. JsCalendar Helper para CakePHP « Lo de Miguel - September 10, 2007

[…] en el layout. Googleando encontré que alguien ya había hecho algo para resolver este problema aquí. Hay que tocar el layout pero esto provee un Helper bastante bonito para lo que […]

23. Juma Gull Arab - October 25, 2007

Simple Question,
How to add or minus two numbers in text box,
For Example I wanna to have two different text field and calculate that and show the Result in one text field.
text1 10 or any numbers var x
text2 20 or any numbers var y
Show result here text3 or Result 30.
that to make event by button of just focus.

Thanks in advance

24. wrolireAronna - November 25, 2009

I really enjoyed reading your article, keep up writing such exciting posts.

25. Carl Jarrel - March 17, 2010

Enjoyed reading the article keep up the good work.

26. pervach - January 24, 2013

Дорогие дамы и господа, а также великий и добрый админ!!!
Не ругайтесь пожалуйста за резмещенную здесь ссылку и не удаляйте
сколько сможете. За это вам огромное спасибо и удачи !!!!
спасибо!
По крайней мере это честно! Извините, за потраченное время!
перевозки крупногабаритного груза

27. zfishkinet - February 9, 2013
28. xpmasterqx - May 20, 2013
29. Aaneclolla - June 4, 2013
30. SmefeUnline - June 18, 2013

Posiadamy w palecie wysokiej jakości skomentowanie. W ofercie mamy lampki nocne, żyrandole kryształowe, lampy stojące, lampy nowe, lampy sufitowe i dodatkowo lampy wiszące. Proponowane przez nas iluminacja wyprosi w rozmaitych wersjach stylistycznych. Oferujemy algorytmy tradycyjne, stylizowane jak również nowoczesne. Poza tym syte oferowane na krzyż naszą firmę podświetlenie ucieka w dużej ilości wersjach kolorystycznych. Począwszy od momentu drobniusieńkich bieli jak również odcieni kremowych. przez barwy ziemi takie w charakterze zielenie, brązy jak również szarości. Aż platforma obywatelska zdecydowane i zuchowate od początku do końca nasycone kolory, takie na kształt niebieski, czerwony bądź oranż. Oprócz tego jest możliwość wyłonienia barwy światła. Dostępne bawią odcienie ciepłe, nowiuteńkie jak i również bezstronne. Na ktoś kupiony w naszym sklepiku artykuł pożyczamy pewności. Zakup oferowanego w poprzek nas oświetlenie gwarancja wieloletniego przeznaczenia oraz dzisiejszego designu oraz wygodnych rozstrzygnięć funkcjonalnych. Kreślarze zadbali o to, ażeby oferowane z wykorzystaniem nas światło ustanowiło się bez zarzutu, oraz do tego obstawało funkcjonalne. Ciepło zapraszamy aż do dokonnia kupna w naszym sklepiku. Zachęcamy!

31. akseleratcx - July 19, 2013

http://games-catalog.ru Каталог торрент игр

32. Branden - August 9, 2013

Marvelous, what a website it is! This weblog presents useful information to us, keep it up.

33. Jody - August 9, 2013

Hello! This is kind of off topic but I need some advice from an established blog. Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about setting up my own but I’m not sure where to begin. Do you have any tips or suggestions? Cheers

34. valpfalge - September 7, 2013

Hello

35. HotminGP - September 16, 2013

1

36. Gugeligue - October 1, 2013

Afian

37. alariablitips - December 31, 2013

Как практика показывает, каталоги уже не вариант, доски объявлений дают мало толку! Покупка ссылок на биржах удовольствие дорогое!
Мы предлогаем наростить массу вашего сайта в поисковых системах, путем наращиванием обратных ссылок!
Осуществляем раскрутку вашего сайта, либо любого объявления, услуге по профилям, форумам, блогам, гостевым!
___________________________________
Чем мы лучше???
-Низкие цены!
-Акции и постоянные скидки!
-Партнерская программа 10%
-Скидки постоянным клиентам до 20%
-Огромный вариант тарифов и возможностей!
_______________________________________
Раскрутим:
-Сайты(личные, компании, фирм)
-объявления о ваших услугах, либо не услугах!
Консултация!
_______________________________________
Примерные тарифы:
10 000 профилей на сайтах(ссылка вашего сайта внутри профилям) Всего 400 руб!!!
200 000 профилей на сайтах(ссылка вашего сайта внутри профилям) Всего 4800 руб!!!

Солянка 10 000 сайтов(ссылка вашего сайта в профилях, каталогах, блогах, гостевых, форумах) Всего 350 руб!!!
Солянка 600000 сайтов(ссылка вашего сайта в профилях, каталогах, блогах, гостевых, форумах) Всего 8000 руб!!!
_______________________________________
Также имеется огромное количество комплексных тарифов на любой вкус, цвет и возможность!
-Гарантия!
-Отчет!

Контакты для связи:

ICQ 648486829
ICQ 623315134
email: progonseomail@gmail.com

38. orarobroag - September 17, 2014

believes typeface ckqra popover callbacks moriarty positivity

39. natbous - September 17, 2014

asanet sjmcdermid historyof baldwin codes wepdwukmte comgapi

40. mivyOrgade - September 17, 2014

kaaf autop told onformsubmit riding controlling shift

41. mivyOrgade - September 17, 2014

agohours nowminute contentskip doctors visitorid turkish armv

42. VuncFubs - September 17, 2014

elitnoteli.ru useour galaxy header scroll

43. Saraneadia - November 6, 2014

Hi,

Today I want share with you my favorite service to send anonymous emails with attachments,
Test it for free from here : anonymous email with attachments
Have fun, and give me your feedback !

Regards,
Sarah

44. AaronivMi - February 13, 2015

Not far off on March 8th . Are invited to share what everyone gives
your loved ones, those with whom working mothers , girlfriends , in general, our
beloveds . Girls also join . What do you most
I want to on your holiday ? What do expect from their men ?

45. Enriquecymn - March 30, 2015

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: