Joomla Component: include/require php in View - php

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.

Related

What is the best and most reliable way to call other templates into page.php in Wordpress?

My Wordpress website uses two push menus, both of which I initially had situated in the header.php file. It wasn't until I started needing to use a lot of PHP code in one of these menus that I decided to have them in separate files and call them into the page structure via PHP commands. Ive been wondering what would be the best way of going about this and would appreciate any advice on whether I'm doing it correctly. Below is the code from the simpler of the two menus – I've saved this as mobile-menu.php.
<div class="mobile-menu">
<nav class="main-menu" id="mobile">
<?php wp_nav_menu( array( 'theme_location' => 'header' ) ); ?>
</nav>
</div>
This isn't a particularly long list of code, but the other menu (shopping-basket.php)is a lot longer as it uses PHP from a plugin I've installed, and I don't want masses of code in my header.
I've then called both menus into my page.php using "include" commands, like I have with the header and footer – for example:
<?php get_header(); ?>
<?php include("mobile-menu.php"); ?>
<?php include("shopping-basket.php"); ?>
Is this recommended? It seems to work okay, but I've noticed that despite saving the files as PHP templates with a .php extension, they're showing up as HTML files when I have a look in my FTP account. Why would this be?
Any advice or info would be appreciated as I can't really find any concrete info on this online and have got to the stage that I'm at by searching through forums etc.
You would be best off using get_template_part() instead of include().
Load a template part into a template
https://developer.wordpress.org/reference/functions/get_template_part/
You should put the contents of mobile-menu.php into templates/menu-mobile.php (Note that I've reordered the name for least- to most- specific) and templates/shoppingbasket.php. Call them as so:
get_template_part('templates/menu','mobile');
get_template_part('templates/shoppingbasket');

How to include header/footer/etc.php with content in between for each page?

I'm looking for an object-oriented solution or possibly a framework that already exists to do what I'm hoping to do for a somewhat small back-end/admin project.
For instance, the system is to manage Projects. So there will be a header/footer/menu on every page. Two pages, for instance, will be projects.php and notes.php.
To include the header and footer on these pages I would normally do:
//header.php
<!-- Header Content Here -->
include menu.php
//projects.php
include header.php
<!-- My Projects Content Here -->
include footer.php
//notes.php
include header.php
<!-- My Projects Content Here -->
include footer.php
and have my links to each page just be '/projects/' or '/notes/'
I've also tried:
//index.php
include header.php
include projects.php
include notes.php
include footer.php
//projects.php
if(isset($_GET['projects']) {
<!-- My Projects Content Here -->
//notes.php
if(isset($_GET['notes']) {
<!-- My Notes Content Here -->
and have the links in my menu be '/index.php?projects' and '/index.php?notes'.
Both are frustrating and don't seem very fluid to me. I would like to upgrade my tactics here but am unsure the best way to go about it.
What's the best way? Is there a Framework that is lightweight and can manage these for me? I would like to keep the links at '/projects/' and '/notes/' but just be able to create an object that calls the content from projects.php and places it in automatically on that link click.
TLDR; - I don't want to have to place header/footer/etc.php as an 'include X' into every PHP page template file manually. I would like for it to know that every page needs it unless otherwise assigned.
Create a file called something like page.php:
if(!empty($_GET['page'])) {
// Could add check to see if file exists
$page = $_GET['page']; // Being "notes" or "projects"
include 'header.php'; // Assuming this includes <head>, header, menu, functions etc.
include $page.'.php'; // Makes notes.php or projects.php
include 'footer.php';
} else {
echo 'Someting went wrong';
}
For links in menu it should be page.php?page=notes or page.php?page=projects
Every file? Are you really sure you want to do that? It might cause data crash.
But if you insist, look for your php.ini file and find auto_prepend_file
http://php.net/manual/en/ini.core.php#ini.auto-prepend-file
auto_prepend_file header.php // For Example
Now that you did that if you don't remove header.php from every program (or make sure include_once is used) then you might have php errors flying around.

Including a body file (that is much like a platform MySQL / PHP / JS) into a wordpress template

I’ve built an SEO platform that Works with MySQL database and outputs / Inputs to a PHP table.
I’ve made a template in wordpress that has the header and footer from the original theme, and left the body empty so I can call the application / platform.
<?php
/*
Template name: Keywords
*/
?>
<?php get_header(); ?>
<?php
include('/body_call/index.php');
?>
<?php get_footer(); ?>
I’ve tried to place the folder with all the files (body_call) in several places and called the index.php folder hoping that it fills the center of the template.
The index folder is something like this:
<html>
<div id="container">
<body id="body">
<?php include('body_call/body.php'); ?>
</body>
</div>
</html>
I know the problem is the calling part because the platform works well if it’s by its self…
And it's probably some worpress specific code that I'm missing.
Can anyone help?
Thanks,
Miguel
When you include a file from another file you are actually just including it in the file. That means you are actually in first file folder and not second. So if second file needs to include a file wich is stored somewhere else it has to be included by navigating from first file folder to it's actual path. It sounds a little tricky. So to nvigate to upper level if need you should use ../.
Anyway to avoid any confusion i would suggest to use absolute path so where ever the file is called from it will always be able to charge any file from anywhere.
Have a look at include documentation
an example could be
include(/home/user/body_call/index.php');

Codeigniter - Templating with separate templates

I wish to have a codeigniter application with 3 templates.
a template for displaying a login view, or an error view.
a template with header body navigation
a template with header body sidebar footer
now I can build the codeigniter application, but I can't find a simple template system to accomplish this task. There are many recommendations for libraries available, but they lack implementation details.
Suggestions and guidance would be appreciated.
You can user CodeIgniter Template.
http://williamsconcepts.com/ci/codeigniter/libraries/template/index.html
Download here the library and also you have a full documentation.
With this library, you can use more than one template, and you can manage it easy and separate in groups.
It's quite simple once you get your head around it.
You need to create a view file for each of the things you've listed above, so you'll want something like this (folder/file wise):
views/page-login.php
views/page-error.php
views/header.php
views/footer.php
Then from your controller you will load one of the page views.
Within the page view you can then load the elements you require, so header and footer with the page specific code between them.
So for example your login page could be:
<?php $this->load->view('header'); ?>
<h1>Login</h1>
<p>Please login to my website.</p>
<?php $this->load->view('footer'); ?>
This is fairly simple.
A quick example:
In your controller you can put
//a test variable
$data["foo"] = 'bar';
$data["page"] = 'a_page'; // this will make sure it loads the views/pages/a_page.php in your template
$this->load->view('templates/login',$data);
And in your views/template/login.php you can put:
<!-- your login template html -->
<html>
...
<!-- include the view you want inside your login template -->
<?php $this->load->view('pages/'.$page);?><!-- As you can see it loads /views/pages/a_page.php -->
<?php echo $foo;?> //This will echo Bar
</html>
or:
<?php $this->load->view('template/login_header');?>
<?php $this->load->view('pages/'.$page);?>
<?php $this->load->view('template/login_footer');?>
views/pages/a_page.php will also know $foo.
This loads another view (views/pages/a_page.php) in your template.
This way you can create all the templates you want, and include your view in those templates.
TIP: Handling headers & footers this way get's unmanageable pretty quickly. And it's still better to use template libraries. Try Phil sturgeon's library

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