Zend - How to include file in view like masterpae asp.net? - php

I have structure folder like this:
http://nx9.upanh.com/b4.s37.d4/3fd103509d1ac25ece02096b5e25a5f0_55810779.capture.png
(I really sorry, I'm not enough reputation)
I want to include 2 files are header.phtml and footer.phtml from public/shared/ folder to view of default module, I use code below:
<?php echo $this->partial('public/shared/header.phtml'); ?>
<h3>This is index.phtml</h3>
<?php echo $this->partial('public/shared/footer.phtml'); ?>
But I get an error HTTP 500 (Internal Server Error). If I'm only using html code, there is no problem happen.
<h3>This is index.phtml</h3>
I can do like this in CodeIgniter Framework but I don't know how to do this in Zend Framework. Please help me! Thank so much.

Are you using ZF1 or 2 ? In ZF1, you will want to place the header and footer into the layout.phtml file located in application/layouts/scripts/layout.phtml.
The actual header.phtml and footer.phtml should be placed into the application/view/scripts folder, then you can do something like:
LAYOUT.PHTML:
<?php echo $this->partial('header.phtml') ?>
// this bit will render the action content
<?php print $this->layout()->content ?>
<?php echo $this->partial('footer.phtml') ?>

Related

Where to start when building a website from scratch using index.php

I have decided to build my site website from scratch, rather than using Wordpress, Magento or a bootstrap template.
I'm looking for a good guide to do so, Code Academy and w3schools are good for learning specific elements of HTML and CSS but I'm looking for a good guide for how to structure my site.
I am playing around with creating an index.php using e.g. to include all element of the page to make creating the individual pages of my site clean and more efficient that including the include for header and footer on each page.
One issue I am having is that I am struggling to understand how to include the different pages within the index.php. I have searched for this but I obviously am not finding the correct words to search for this as I'm struggling to find a decent answer. I think I need something along the lines of a wildcard so that I can say to call all html files within my pages folder so that all pages are using my index.php template.
Below is my index.php to help explain. Thanks in advance and apologies if this question is answered elsewhere on the site, I searched but did not find anyone else answering the same question!
<!DOCTYPE html>
<html>
<php include "head.html" ?>
<body>
<div class="container">
<php include "navigation.html" ?>
<div class="wrapper">
<php include pages/*.html ?>
</div>
</div>
</body>
<php include "footer.html" ?>
</html>
It looks to me like you are almost there. I am not sure of the syntax you are using works, but the code below is used in some of my sites to include a header:
<?php include('includes/header.php'); ?>
This means include the code in the file 'header.php' that is in the folder 'includes', which is on the same level as the file that is calling the code.
The result would be that the 2 files are merged in to one when the page is loaded, with the code from header.php being inserted in place of the line
<?php include('includes/header.php'); ?>
You need to pass variables in URL e.g index.php?page=home and then in your index.php file you shoud get that variable $page = $_GET['page']. Now in $page you have name of the file to include
<?php include($page.'.html') ?>
For now, I skipped security issues of that solution.

Joomla Component: include/require php in View

this is my first time making a component for Joomla 3.
I'm trying to convert a php website to a component.
Almost every view of my php website is built this way:
Header
Sidebar
Navigation bar
Actual content
Footer
Scripts
So basically it looks like this:
A view from php website
<?php require_once('includes/header.php'); ?>
<body>
<div">
<?php require_once('includes/sidebar.php') ?>
<div>
<?php require_once('includes/navbar.php') ?>
</div>
<div>
<!-- Content or something -->
</div>
<?php require_once('includes/footer.php') ?>
</div>
<?php require_once('includes/scripts.php') ?>
<script>
</script>
</body>
</html>
But in Joomla this is not possible. If I try to include or require a php file like above it just doesn't work.
View in component looks like this:
com_component
../views
../tmpl
default.php
view.html.php
../includes
header.php
footer.php
sidebar.php
scripts.php
navbar.php
Default.php is supposed to show a dashboard on the frontend.
This is what I'm trying to do in default.php:
Default.php
<?php include_once('../../includes/header.php') ?>
<body>
<?php include_once('../../includes/sidebar.php') ?>
<?php include_once('../../includes/navbar.php') ?>
<!-- Some content-->
<?php include_once('../../includes/footer.php') ?>
<!-- etc -->
What I've done
I've found some JDocument and JHTML functions which can add stylesheets and javascript to the template. But that is not what I'm looking for.
I used Joomla's addCustomTag function but it only shows a commented php line in the template.
Tried to make a String of it and passed it through a variable.
Questions
Is it possible to include php files in a template (default.php)?
If not, is there any other way to do it?
If yes, is that good practice in Joomla?
Thanks
To include files in the same directory as the given file, you can use:
include __DIR__ . '/../includes/header.php'
If you don't use DIR the current path is related to the main page (index.php in the site root) and not to the layout (default.php).
A more Joomla compatible solution is to put all your "sub-layouts" in the same folder with a conventional naming like this:
com_component/
views/
myview/
view.html.php
tmpl/
default.php
default_header.php
default_footer.php
....
Then you can use in your main layout the following code:
echo $this->loadTemplate('header');
...
echo $this->loadTemplate('footer');
You don't need a component to do any of this. I mean you can
do this but it should only be as an intermediary step since Joomla is designed to make all this easier by having APIs to include modules which is basically what all your pieces are.
You'll notice that in the joomla template file there are places where module positions are loaded. These are things like menus, footers, sidebars etc.
Depending on what they are you should really make each of these subsections a module. In some cases you can probably use existing modules already in Joomla or make a "custom html" module if you are just loading some html. In other cases makin a joomla module is really easy, just a few files, and you can put your php code there, really to start you can put the whole thing in the tmpl/default.php for the module or you can split it between the helper and the layout.
You'll be much happier in the long run if you take advantage of using a CMS rather than fighting it.
Components are only for the "body" area, what you need to do is make a template and then the modules.

Include external code on a web page ( php include() or get_header() )

I've started creating a custom html page for the first time and i've hit a bump. The code in the header will be the same in all my pages , so i want to put it in an external file (header.php) and just put 1 line of code in all my pages to link to it but it doesn't seem to be working .
Note - I want to include just basic html code ( not a function ) - i'm thinking that's why its not working but not sure.
HTML file :
<body>
<?php include('header.php') ?> // didn't work
<?php get_header(); ?> // didn't work
<div class="content"> </div>
</body>
PHP File :
<h1>TITLE</h1>
When i put the code directly on the page - it shows "TITLE"
When i try include or get_header , i get a blank page . I checked the codex and online posts but i've not been able to fix it .
Any help would be appreciated - Thanks
Try in your header.php:
<?php
echo "<h1>TITLE</h1>";
?>
Then where you want to insert the header:
<?php include '/header.php' ?>
The / character makes the path to header.php relative to your root.
<?php include("dyn_layout/cbside_php.php"); ?>
This works for me... just don't forget to rename your files to .php extension... including the file where the insertion is made.

Including a uri "/user/sidebar" page (Kohana Framework) in PHP

I would like to have the page on user/sidebar on the right in the template design.
Normally, I would include a php file. But I am using Kohana Framework so I have created a view and a controller for this sidebar, and exists on mysite.com/user/sidebar
Now how would i <?php include "/user/sidebar"; ?> correct? I get no such file og dir error for this. I tried full url, but allow_url_include=0
Just looking through the Kohana documentation...
It seems like you can include a "request" inside a view with the following command.
<?php echo Request::factory('user/sidebar')->execute() ?>
See this page for more info: http://kohanaframework.org/3.0/guide/kohana/mvc/views
AndrewR's comment is close. For Kohana 3.2, you'll want to load a view within a view and not a request within a view:
<?php echo View::factory('/user/sidebar'); ?>
or
<?php include Kohana::find_file('views', 'user/sidebar') ?>
Either is acceptable to do.

Dynamic header with PHP?

I know this is a basic PHP question, and I'm trying to learn the stuff. I very familiar with HTML, CSS and familiar with the CONCEPT of PHP, but not with specifics.
I have always partnered with a back end developer to accomplish this stuff and to set up wordpress sites, etc.
I'm building a very basic four or five page website (a showcase for the client's custom fishing rods: http://www.tuscaroratackle.com/index2.php). I want to call the page header (as in logo, navigation, etc., not as in the head element) dynamically from a php file, and same thing with the footer, so I don't have to rewrite all the markup on every page for these bits.
I don't intend to use a database for this site, I was just thinking I could call those two bits from another file, as you would in a wordpress setup with the header.php in the wp-content directory.
Is there an easy explanation on how to do this? (I get the basics, just looking for help on the more specific PHP calls that need to be made)
Or, if this is not an answer somebody could easy give, can you point me to a good resource to research it further?
Thx
You betcha - include and require -
using include
in your page:
<body>
<?php include 'header.php'; ?>
in your header.php
<div id="header">
<!-- content -->
<?php echo "run php stuff too"; ?>
</div>
would result in:
<body>
<div id="header">
<!-- content -->
run php stuff too
</div>
You should put the header html code in some file such as header.php and then include it with php like:
include ('header.php');
You should specify the correct path there, for example, if you put the header.php file in includes folder, you can include it like:
include ('inclues/header.php');
More Info:
http://php.net/include
Put in a separate file and use include, require or require_once.
Eg
require_once("path/to/myfile.php");
Look into PHP includes.
The way I normally do it is to create a file called includes.php with two functions header() and footer(), then call them on each page like such:
includes.php:
<?php
function header(){
echo '<div id="header">Welcome</div>';
}
function footer(){
echo '<div id="footer">Goodbye</div>';
}
?>
index.php:
<?php
include_once('includes.php');
header();
echo '<div id="content">Main page body</div>';
footer();
?>

Categories