I am using zend and not using AJAX in my site. Therefore I have to include all my CSS and JS files in head section of every phtml page.
<html>
<head>
<link rel="stylesheet" type="text/css" href="mycssfile.css" />
<script type="text/javascript" src="myjsfile1.js"></script>
<script type="text/javascript" src="myjsfile2.js"></script>
.........
</head>
<body>
.........
</body>
</html>
Whenever I add any new CSS/JS file, I have to include it everywhere. Is there a good way that I can include all my CSS and JS file in a file/place and use that reference everywhere. May be there is a good way in zend.
I usually put my static suff in the bootstrap of the application in the _initViewHelpers method :
$this->bootstrap(array('frontcontroller', 'view'));
$frontController = $this->getResource('frontcontroller');
$view = $this->getResource('view');
$view->headLink()->appendStylesheet($view->assetUrl('/css/reset.css'))
->appendStylesheet($view->assetUrl('/css/clearfix.css'))
->appendStylesheet($view->assetUrl('/css/screen.css'));
$view->headScript()->appendFile($view->assetUrl('/js/jquery.1.4.3.min.js', 'text/javascript'))
->appendFile($view->assetUrl('/js/jquery.corner.js', 'text/javascript'));
And then from your layout file you call them through
echo $this->headLink()->setIndent();
echo $this->headStyle()->setIndent();
This site might also help you with bootstrap and application optimization
http://joegornick.com/2009/11/18/zend-framework-best-practices-part-1-getting-started/
In normal PHP, you would just put the common bits in another template and include it:
<?php include 'common/header_assets.php'; ?>
common/header_assets.php:
<link rel="stylesheet" type="text/css" href="main.css" />
<script type="text/javascript" src="myjsfile1.js"></script>
<script type="text/javascript" src="myjsfile1.js"></script>
With the Zend Framework, you would want to create a layout for all of your templates to share that would have these common bits:
http://zendframework.com/manual/en/learning.quickstart.create-layout.html
Related
I am using the slim framework to create a website and have views and twig in my project. In my pages, I use php to help facilitate what html is rendered in my webpage. An example is below
<html>
<head>
<title>I ain't afraid of no bugs!</title>
<link rel="shortcut icon" type="image/x-icon" href="../_images/_logos/bug-hunter-icon.ico" />
<link rel="stylesheet" type="text/css" href="../_css/home.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script>
<script type="text/javascript" src="_javascript/login.js"></script>
<script type="text/javascript" src="_javascript/sign_up.js"></script>
</head>
<body>
<?php require 'header_login.php' ?> //problem
<!--div banner, content-area, footer -->
</body>
</html>
and then I render this page by
$app->render('home.php');
However, the html in header_login.php is not loaded onto the page. Instead when I inspect the element, the page looks like
What I do not understand is why the code I am linking to is not being displayed there. The code being imported is a simple navigation bar. But even if I put echo "lala" on the pages, nothing php is displayed.
Read Twig documentation:
http://twig.sensiolabs.org/doc/tags/include.html
{% include 'header_login.php' %}
Or see SlimPHP - PHP view
https://github.com/slimphp/PHP-View
You can see the difference with the Twig View
https://github.com/slimphp/Twig-View
If the header_login.php file is in the same directory as home.php, you should be able to change it to
<?php require __DIR__ . '/header_login.php' ?>
This tells PHP to load it from the same directory as the current file, rather than the directory of the script being executed.
I'm trying to use bootstrap on a codeigniter web but it looks like the bootstrap files are not found
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="<?php echo base_url('application/bootstrap/css/bootstrap.min.css');?>" rel="stylesheet">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="<?php echo base_url('application/bootstrap/js/bootstrap.min.js');?>"></script>
I've checked the paths generated by base_url() and it are correct.
move bootstrap folder on root location, because CI is understanding application folder as controller although application folder is restricted from client request by /application/.htaccess file
|-
|-application
|-bootstrap
//and use path as
<?php echo base_url('bootstrap/css/bootstrap.min.css');?>
Here's the best guide I've found so far:
How to include bootstrap in CI
In short:
Download bootstrap
Put it in an assets folder on same level as application folder
Also put jquery
Include css in header and jquery in footer of your templates:
<html>
<head>
<title>CodeIgniter Tutorial</title>
<link rel="stylesheet" href="<?php echo base_url("assets/css/bootstrap.css"); ?>" />
</head>
<body>
<h1>H_E_A_D_E_R</h1>
and :
<hr/>
<em>© 2016</em>
<script type="text/javascript" src="<?php echo base_url("assets/js/jquery-3.1.1.js"); ?>"></script>
<script type="text/javascript" src="<?php echo base_url("assets/js/bootstrap.js"); ?>"></script>
</body>
</html>
Don't forget to include the CodeIgniter URL helper in your controller
$this->load->helper('url');
Just sharing over here so that you no need to combine folders manually. I used this package offered from github regularly to commit new projects. Of course you will have to modify the application/config/config.php to get things work !
https://github.com/sjlu/CodeIgniter-Bootstrap
Cheers !
Create a folder(assets) in Codeigniter root directly (Same level to application folder)
Paste your bootstrap files into that folder
add link code in your view file
<link type="text/css" href="<?= base_url(); ?>assets/bootstrap.min.css" rel="stylesheet">
add this line in your controller file application/cotrollers/test.php
<?php
class Test extends CI_Controller {
public function index()
{
$this->load->helper('url'); // Load url links
$this->load->view('index'); //Load view page
}
}
?>
Now you can use bootstrap classes in your view
Download the Bootstrap file from the internet if you want to work offline
Unzip and rename and copy to root folder. If you put it in application folder, .htaccess will not allow it to be accessed.
Add the link like the following line of code:
<link href="<?php echo base_url('assets/css/bootstrap.min.css');?> rel="stylesheet">
If you use the Ci4 , add the folder 'bootstrap' in the default folder 'public'.
Thanks
I am new in PHP programming but have some experience using .NET MVC 4.
In .NET, we can specify the HTML page which will act as main layout that share the references (e.g Bootstrap, CSSs, Javascripts etc.etc) which normally being called shared view "_Layout". In PHP, is this possible and if yes, how can I achieve that?
thanks a lot
example:
Below reference will be used for all views in the project if specified;
<head>
<script type="text/javascript" src="~/Scripts/jquery-2.1.0.min.js" ></script>
<script type="text/javascript" src="~/Scripts/jquery-ui-1.10.4.min.js"></script>
<script type="text/javascript" src="~/Scripts/bootstrap.min.js"></script>
<link href="#Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" type="text/css" />
</head>
So, I'm trying to build a web site based on my own MVC-like PHP model.
let's say that every view consists of at least 2 parts:
let those be 'template.php' and 'mypage.php' (the second part depends on the specific page, the first part is common).
template.php has the following code (in simplified version):
<html>
<title>Sometitle</title>
<link rel='stylesheet' href='/css/gen.css' type='text/css' />
<script type='text/javascript' src='/js/gen.js'></script>
<?php echo $other_external_files; ?>
</head>
<body>
<div class="some_header_and other stuff">...</div>
<div class="workfield">
<?php include_once mypage.php; ?>
</div>
</body>
</html>
The problem that 'mypage.php' (as well as some other pages) has some specific .css and .js files that should be included.
The question is: Which is the best option to include these files (how and where)?
Here are my thoughts:
I could define the variable $other_external_files in a model or controller, but don't really want to do that, because .css file is normally part of the view and I could hardcode it in 'mypage.php', if only it would not need to stay in <head>.
I understand that .css and .js files can be dynamically included with JavaScript, but I'd like to avoid this solution if there's no strong need in it.
I could define all these variables in the corresponding models. The problem here is, that from the very beginning I've been trying to structure my models based on the content (thus, I have universal models (files and Classes) like 'Users', 'Shop_items' etc, that don't necessarily correspond to every view file (it means I basically don't have any mypage_model.php, just 'items_model', 'users_model' etc.). Defining title of the page in a model will force me to add lots of model files.
I could possibly auto-define the variable $other_external_files in the core View class, like this:
-
function __construct($action){
$this->action = $action.'.php';
$this->css_file = 'css/'.$action.'.css';
}
function generate($view_lvl, $data = null){
$other_external_files = $this->css_file;
require_once $action;
}
However, this approach seems to be limited, because possibly there can be a situation, when I'd like to include several files with quite different names.
My personal choose for now is the 3rd., because it allows not only to append css and js, but also easily define title of the document and other possible variables... but before multiplying my files, I'd like to consult experienced programmers you are if it's really the right way.
Thanks a lot and sorry for a long question. Any help is very much appreciated!
I usually don't go for this kind of formation. Experience has proven to me that if I separate each view/page as a single file, it'll be more useful. I always use your strategy for pages that they're more template like and when it comes to page's distinct properties like title I make it so strict to not get confused. For instance, I get those properties from a single place like a database table of pages.
For resources like JavaScript or CSS files I include them exactly like in a normal page in every view. I think it's better to not populate them in a variable like $other_external_files; and instead put them differently in each separated view.
For example, this is my about.php view:
<html>
<head>
<title>About</title>
<link rel='stylesheet' href='<?php echo ABS_PATH; ?>/css/extra.css' type='text/css' />
<link rel='stylesheet' href='<?php echo ABS_PATH; ?>/css/extra2.css' type='text/css' />
<script type='text/javascript' src='<?php echo ABS_PATH; ?>/js/extra.js'></script>
</head>
<body>
</body>
</html>
And this is my contact.php view:
<html>
<head>
<title>Contact</title>
<link rel='stylesheet' href='<?php echo ABS_PATH; ?>/css/extra3.css' type='text/css' />
<script type='text/javascript' src='<?php echo ABS_PATH; ?>/js/extra.js'></script>
<script type='text/javascript' src='<?php echo ABS_PATH; ?>/js/extra2.js'></script>
</head>
<body>
</body>
</html>
For my very dynamic page:
<html>
<head>
<title><?php echo $TITLE; ?></title>
<link rel='stylesheet' href='<?php echo ABS_PATH; ?>/css/extra3.css' type='text/css' />
<script type='text/javascript' src='<?php echo ABS_PATH; ?>/js/extra.js'></script>
<script type='text/javascript' src='<?php echo ABS_PATH; ?>/js/extra2.js'></script>
</head>
<body>
</body>
</html>
If you look at my dynamic page you see that I pass actual data to it. This means I get relevant data from my controller and then I pass it through.
I have a file called "header.php" that I am including on every page on my site and this file contains links to other files on my sever like css files, jquery plugins, etc.. and right now I am using absolute paths for those links so they will work with files that are not in the same directory as the header.php file and this works, but as you can see in the example below, things start to get really hard to manage if you your header.php file contains lot's of links (which mine does) so I would like to know if there are any other alternatives to using absolute paths in the header.php file like I have done here.
header.php
<?
$base_url = "http://example.com";
?>
<html>
<head>
<title> <? echo($title); ?> </title>
<link rel="stylesheet" type="text/css" href="<? echo($base_url); ?>/styles/some_css_file.css" media="all"/>
<link rel="stylesheet" type="text/css" href="<? echo($base_url); ?>styles/another_css_file.css" media="all"/>
<link rel="stylesheet" type="text/css" href="<? echo($base_url); ?>styles/another_css_file.css" media="all"/>
...
...
...
<script type="text/javascript" src="<? echo($base_url); ?>/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<? echo($base_url); ?>/scripts/some_jquery_plugin.js"></script>
<script type="text/javascript" src="<? echo($base_url); ?>/scripts/another_jquery_plugin.js"></script>
...
...
...
</head>
Some file which includes header.php
<?
$title = "some page title";
include("header.php");
?>
<body>
PAGE CONTENTS
</body>
</html>
Use the HTML base tag to define the base of your site.
<base href="<?php echo $base_url; ?>" />
<link rel="style.css" />
Absolute path is the way to go here. But, if you have a lot of links to print, it can be simplified by storing the paths in an array and looping through the array.
$base = "http://www.example.com/"
$links = array( "styles/file1.css", "styles/file2.css", ... );
foreach ( $links as $link ) {
echo '<link rel="stylesheet" href="' . $base . $link . '" type="text/css" media="all" />';
}
As a side note, what you have isn't great practice. It would be better and more efficient for you to try and combine some of those files as this will reduce the amount of HTTP requests the browser has to make to your server. http://developer.yahoo.com/performance/rules.html#num_http
I had the same doubt in one of my projects and i've done as the code bellow. As the paths start with "/", the file will be found based in the root directory and in this case isn't necessary to specify the domain, it will turn the things easier for maintenace and prevent problems if you will do rewrite of URLs using (mod_rewrite). Hope it can help you too!
<html>
<head>
<title> <? echo($title); ?> </title>
<link rel="stylesheet" type="text/css" href="/styles/some_css_file.css" media="all"/>
<link rel="stylesheet" type="text/css" href="/styles/another_css_file.css" media="all"/>
<link rel="stylesheet" type="text/css" href="/styles/another_css_file.css" media="all"/>
<script type="text/javascript" src="/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/scripts/some_jquery_plugin.js"></script>
<script type="text/javascript" src="/scripts/another_jquery_plugin.js"></script>
</head>
</html>
This is not a direct answer to your question, but it may help you.
If you are including that many css and JS files you should look into using Minify. Minify will take a bunch of css/js files and compress/combine them into one file, which will greatly speed up the loading of your site.
Using this could also help with your path concern, as you can specify directories and groups very easily.
You can find information about it here: Minify