SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Location
    Porto,Portugal
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamicly include scripts/stylesheets in php

    Hi.
    I am working in a website.
    I have the main index php page and then depending of the querystring the content of various pages is included in the main content div.


    In the index file i have included the main css and script files but i have some specific files that will only be included in certain pages.

    Currently i am treating every module like a normal page (with head tag where i include the css and scripts for that module.)

    Is this correct or there is the a best way?
    If i look in view source in browser my website has 2 or more head tags (the main one from and the specific pages header.)

    Itīs possible to inject the links into the head of then main php file?


    PS: Itīs stupid that i canīt write index dot php because the forum considers it a link

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,044
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    This works well for me.

    Views implements this interface (short version):
    PHP Code:
    interface Viewable {
        public function 
    css(Viewable $view=null);
        public function 
    javascript(Viewable $view=null);
        public function 
    render();

    Base abstract view class:
    PHP Code:
    <?php
    abstract class AppView implements Viewable {

        public function 
    css(Viewable $pView=null) {
            if(!
    is_null($pView)) {
                
    $pView->css();
            }
        }
        
        public function 
    javascript(Viewable $pView=null) {
            if(!
    is_null($pView)) {
                
    $pView->javascript();
            }
        }
        
        public function 
    render() { }

    ?>
    Short example of a Layout:
    PHP Code:
    <?php
    class DefaultLayout extends AppView {

        private 
    $_view;
        
        public function 
    __construct(Viewable $view) {
        
            
    $this->_view $view;
        
        }

        public function 
    css(Viewable $pView=null) {
        
            
    // layout css files go here ?>
            
            <link rel="stylesheet" href="/include/css/tags.css" type="text/css">
            <link rel="stylesheet" href="/include/css/base.css" type="text/css">
            <link rel="stylesheet" href="/include/css/pages.css" type="text/css">
        
        <?php }
        
        public function 
    javascript(Viewable $pView=null) {
        
            
    // layout js scripts go here ?>
            
            <script type="text/javascript">
            var hash = [];
        
            window['onload'] = function(pEvt) {
        
                 for(var x in hash) {
             
                     hash[x]();
             
                 }
        
            }
        </script>
        
        <?php }
        
        public function 
    render() { ?>
        
            <html>
                <head>
                    <?php $this->_view->css($this); ?>
                    <?php $this->_view->javascript($this); ?>
                </head>
                <body>
                    <?php $this->_view->render(); ?>
                </body>
            </html>
        
        
        <?php }

    }
    ?>
    Example of a "page" view:

    PHP Code:
    <?php
    class HomeView extends AppView {

        public function 
    css(Viewable $pView=null) {
        
            
    parent::css($pView); ?>
            <link rel="stylesheet" href="/include/css/home.css" type="text/css">
        
        <?php }
        
        public function 
    javascript(Viewable $pView=null) {
            
            
    // layout js 
            
    parent::javascript($pView);    
            
            
    // page js here ?>
            <script type="text/javascript">
            hash.push(function() { setTimout(function() { alert('hello'); },5000); });
            </script>
            <?php
        
        
    }
        
        public function 
    render() {
        
            echo 
    '<p>welcome</p>';
        
        }

    ?>
    usage:
    PHP Code:
    $p = new DefaultLayout(new HomeView()); 

    example output:
    HTML Code:
    <html>
    	<head>
    						
    		<link rel="stylesheet" href="/include/css/tags.css" type="text/css">
    		<link rel="stylesheet" href="/include/css/base.css" type="text/css">
    		<link rel="stylesheet" href="/include/css/pages.css" type="text/css">
    		<link rel="stylesheet" href="/include/css/home.css" type="text/css">
    	
    							
    		<script type="text/javascript">
    		var hash = [];
    	
    		window['onload'] = function(pEvt) {
    	
    		 	for(var x in hash) {
    		 
    		 		hash[x]();
    		 
    		 	}
    	
    		}
    		</script>
    		<script type="text/javascript">
    		hash.push(function() { setTimout(function() { alert('hello'); },5000); });
    		</script>
    
    	</head>
    	<body>
    		<p>welcome</p>			
    	</body>
    </html>
    This also makes it possible to easily create dynamic presentation and behavior based on information obtained from the model which is separated from the structure of the document.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •