0

Hello Friends I am a newbie programmer and very very new to PHP. I am seeking help in writing a small php Function. The situation is like this:

I am running Joomla and I have a template which is in BLOCKS for example:

header.php
topmodules.php
mainbody.php
bottommodules.php
footer.php

All my blocks are placed in a directory (/layouts/blocks). All these blocks need to be joined in the main Index.php file.

The function I know is something like this:

<?php
function get_header(){
  require_once(TEMPLATEPATH.'/layouts/blocks/header.php');
}
?>

And then call it like:

<?php get_header(); ?>

But that is not very professional and also I will have to write a function for every file moreover this can also be accomplished by just using

<?php require(YOURBASEPATH . DS .'layouts'. DS .'blocks'. DS . "header.php"); ?>

But what I am looking for is to have a single function /class which can get that PHP file from that directory, just by passing the name of the file so that I can add some more blocks to that directory in future without re-writing the function and simply call them like:

<?php $this->getBlock('header') ?>
<?php $this->getBlock('topmodules') ?>
<?php $this->getBlock('mainbody') ?>
<?php $this->getBlock('bottommodules') ?>
<?php $this->getBlock('footer') ?>

Kindly help.

2 Answers 2

1

You just need to add a parameter to the function you have:

function getBlock($filename){
  require_once(YOURBASEPATH . DS . 'layouts' .DS . 'blocks'. DS . $filename .'.php');
}
Sign up to request clarification or add additional context in comments.

9 Comments

That did not work it gives a message:Fatal error: Call to undefined method JDocumentHTML::getBlock() in E:\xampp\htdocs\cms\templates\joomla_j15\index.php on line 11
@Vikram well, you will need to embed the function in the JDocumentHTML class if you want to use $this->
This is how I called that in my index.php <?php $this->getBlock('header') ?> and also tried <?php getBlock('header') ?> but both did not work.
@Vikram the latter should work, you need to define and call it just as you did for get_header()
Hmmm that is a bit latin and german to me. As I mentioned in my question I am very very new to programming and PHP both. Hence may I request you to elaborate a bit on the process?
|
1
function get_header_improved($s){
  require_once(TEMPLATEPATH.'/layouts/blocks/' . $s . '.php');
}

<?php get_header_improved('header') ?>
<?php get_header_improved('topmodules') ?>
<?php get_header_improved('mainbody') ?>
<?php get_header_improved('bottommodules') ?>
<?php get_header_improved('footer') ?>

I not tried, but this should work.

1 Comment

Iused function get_header_improved($s){ require_once(TEMPLATEPATH.'/layouts/blocks/' . $s . '.php'); } in my functions.php and this is how I called it in the index.php file <?php get_header_improved('header') ?> but no luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.